Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Topics

Recent Status Updates

View All Updates

- - - - -

UDP chat program in java

udp java networking client

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 akusa


    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 22 February 2014 - 03:34 AM

Attached File  1.txt   511bytes   241 downloadsAttached File  2.txt   3.37KB   224 downloadsAttached File  3.txt   2.78KB   224 downloadsAttached File  output.txt   1.9KB   202 downloads

Hello friends! 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!


I attached all my code, because it looked really bad and hard to read when I pasted into the code tags.

Edited by Roger, 23 February 2014 - 09:38 AM.
edited topic tags

#2 BlackRabbit


    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 22 February 2014 - 06:34 AM

Well, about the packet loss: 1) are you catching all the exceptions?

2) are you using message sequence numbers to control they arrived correctly?


Remember that in UDP the transmission can arrive non-sequentially, meaning message number 4 can arrive before message number 3.


About the prints,There's clearly a garbage issue. either in the variable you use to read the files or in the one you use to send (more likely). I advise that before every send you clean up the byte[] variable you use to transmit the message.

#3 akusa


    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 22 February 2014 - 01:12 PM

How do I clean up the byte variable?



edit: I tried sendData = new byte[1024]; and that seemed to work. Thank you for helping me fix that issue.

Edited by akusa, 22 February 2014 - 02:17 PM.

#4 Norm


    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts

Posted 22 February 2014 - 02:58 PM

Also posted at: http://www.javaprogr...ogram-java.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download