The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

udp chat program in java

I have to create a peer to peer chat program in java using UDP for my networking class. The purpose of the program is to simulate a conversation between bots, by reading from a text file and sending random lines. Basically, I have a program with a client class and a server class. I run the program twice, the client from the first program sends to the server of the second program, and the client of the second program sends to the server of the first program.

I also have to implement 20% packet loss and if a packet is “lost” then the client has to resend it.
So the way that I am attempting to handle the packet loss is : I have two boolean fields in the Client class “did_I_Receive_The_Packet” and “should_I_Re_Send”. In the server class I choose a random number between 1-10, if the number is 1 or 2 then that means the packet was lost and I set the didIReceicePacket boolean in the client to false. If that boolean is true, then the client sends packets with a random line from the file, if it is false, then the client sends a packet with a string, “$”. In the server class, if a packet has the “$”, then the client boolean shouldIResend is set to true. In the client class if that boolean is true, then the client is supposed to resend the previous packet.

I'm using thread.sleep() so get the programs to send every 5 seconds so that I'm not being bombared by messages. I'm not sure if that is the correct way to do it or if there is a better way.

Currently, when I run the program twice, they are able to send messages to each other, but the packet loss and resending part isn’t working correctly. When the packets are “lost” i’m getting the system.out message that I created for that part, but the resending a lost packet part doesn’t seem to be working.
Also, when the programs are sending lines from the text file, sometimes the lines come out messed up. My text file contains 10 lines, "ten, nine, eight,..."and so on, on seperate lines. For example, instead of nine, it prints ninen, or twoee, instead of two.

I would greatly appreciate any help or advice on how to fix these issues. Thanks!

Here is sample output from when I ran the programs
program#1
run:
FROM USER1: six
FROM USER2: one
FROM USER1: six
FROM USER2: seven
FROM USER1: eight
FROM USER2: seven
FROM USER1: two
FROM USER2: packet was lost
FROM USER2: eight
FROM USER1: seven
FROM USER2: tenht
FROM USER1: six
FROM USER2: packet was lost
FROM USER2: twoht
FROM USER1: nine
FROM USER2: three
FROM USER1: three
FROM USER2: $hree
FROM USER1: one
FROM USER2: $hree
FROM USER1: two
FROM USER2: tenee

program#2
run:
FROM USER2: six
FROM USER1: one
FROM USER2: six
FROM USER1: seven
FROM USER2: eight
FROM USER1: seven
FROM USER2: twoht
FROM USER1: eight
FROM USER2: $woht
FROM USER1: eight
FROM USER2: seven
FROM USER1: ten
FROM USER2: sixen
FROM USER1: one
FROM USER2: $ixen
FROM USER1: two
FROM USER2: ninen
FROM USER1: three
FROM USER2: packet was lost
FROM USER2: packet was lost
FROM USER2: twoee
FROM USER1: ten