Results 1 to 3 of 3
  1. #1
    devilj is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Need som help with UDP code

    Hello everyone,

    I am new to the forum and was wondering if anyone had some insight into a problem I am having with some code?
    I am creating a UDP server/client that seems to work network wise, however when a second message is sent to the server, the original message still is in the buffer. For instance, when I start with a fresh launch of the server and client I can enter "Hello world" in the client and it shows on the server as "Hello World". However if I send through "nice" as a second transmission, the server reports "niceo world" It seems that some sort of buffer is not getting cleared out. Is there a way to do this?

    Here is my server code:
    Java Code:
    import java.io.*;
    import java.net.*;
    
    class UDPServer
    {
       public static void main(String args[]) throws Exception
          {
             DatagramSocket serverSocket = new DatagramSocket(7777);
                byte[] receiveData = new byte[1024];
                byte[] sendData = new byte[1024];
    	    byte[] killByte = new byte[0];
                while(true)
                   {
                      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                      serverSocket.receive(receivePacket);
                      String sentence = new String( receivePacket.getData());
                      System.out.println("RECEIVED: " + sentence);
                      InetAddress IPAddress = receivePacket.getAddress();
                      int port = receivePacket.getPort();
                      String capitalizedSentence = sentence.toUpperCase();
                      sendData = capitalizedSentence.getBytes();
                      DatagramPacket sendPacket =
                      new DatagramPacket(sendData, sendData.length, IPAddress, port);
                      serverSocket.send(sendPacket);
    		  receivePacket.setData(killByte);
                   }
          }
    }
    Here is my client code:
    Java Code:
    import java.io.*;
    import java.net.*;
    
    class UDPClient3
    {
       public static void main(String args[]) throws Exception
       {
          BufferedReader inFromUser =
             new BufferedReader(new InputStreamReader(System.in));
          DatagramSocket clientSocket = new DatagramSocket();
          
          byte[] sendData = new byte[1024];
          byte[] receiveData = new byte[1024];
          byte[] b = new byte[] {(byte)65, (byte)101, (byte)23, (byte)253};
          InetAddress IPAddress = InetAddress.getByAddress(b);
          System.out.println("Enter Text:");
          String sentence = inFromUser.readLine();
          sendData = sentence.getBytes();
          DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 7777);
          clientSocket.send(sendPacket);
          DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
          clientSocket.receive(receivePacket);
          String modifiedSentence = new String(receivePacket.getData());
          System.out.println("FROM SERVER:" + modifiedSentence);
          clientSocket.close();
       }
    }
    The problem continues until I relaunch the server, so it must be on the server side.

    Any help would be much appreciated

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Use the flush method.

  3. #3
    devilj is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Sorry Junky,

    I am very newby...

    Do I flush the datagrampacket or am I flusking the socket itself....

Similar Threads

  1. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  2. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •