1

I've written two programs. Now each program uses threading to send and receive packets at the same time. Whenever I send packets from the server to the client, the message at the client ends gets received in an infinite loop. I.e; I've added a print statement that prints the message sent and this goes forever in an infinite loop. I want to make it so that it receives the message, and then be able to write back to the server and exit whenever the user wants to.

I've tried using socket.close(), but this makes it so that the client receives the message and I can only write back to the server once. After I send it, I can't send anymore. I want to make it so that I can write back more than once.

Can anyone please point me in the right direction?

My code is as follows;

public class UDPThreadClient extends Thread {

public static int port1;

//Create threaded server
UDPThreadClient (int port1) {
    System.out.println ("Starting threaded client");
    start();
}

public void run() {

    int port = port1;

    try {
        DatagramSocket serverSocket = new DatagramSocket(port1);
           byte[] receiveData = new byte[1024];
           byte[] sendData = new byte[1024];

           while (true) { 
                 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                 serverSocket.receive(receivePacket);
                 String sentence = new String( receivePacket.getData());
                 SocketAddress address = receivePacket.getSocketAddress();


                 System.out.println("RECEIVED from " + address + " : " + 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);
                 //serverSocket.close();
           }  
    } catch (IOException e) {
        System.out.println (e.getMessage());
    }

}

//Create client
public static void main(String[] args) {

    int port = Integer.parseInt(args[0]);
    port1 = Integer.parseInt(args[1]);
    new UDPThreadClient (port1);
    try {

          BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

          DatagramSocket clientSocket = new DatagramSocket();
          InetAddress IPAddress = InetAddress.getByName("localhost");

          byte[] sendData = new byte[1024];
          byte[] receiveData = new byte[1024];

          String sentence = inFromUser.readLine();

          sendData = sentence.getBytes();

          DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);

          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();
    } catch (IOException e) {
        System.out.println (e.getMessage());
    }
}
   }

and

public class UDPThreadServer extends Thread {

public static int port1;

//Create threaded client
UDPThreadServer () {

    System.out.println ("Starting threaded server");
    start();


}

public void run() {

    try {
        DatagramSocket clientSocket = new DatagramSocket();

            BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in));
            Scanner in = new Scanner (inFromUser);
            InetAddress IPAddress = InetAddress.getByName("localhost");
        byte[] sendData = new byte [1024];
        byte[] receiveData = new byte [1024];

        while (in.hasNextLine()) {
        String sentence = in.nextLine();
        //inFromUser.readLine();
        sendData = sentence.getBytes();

        DatagramPacket sendPacket = new DatagramPacket (sendData, sendData.length, IPAddress, port1);
        clientSocket.send(sendPacket);

        DatagramPacket receivePacket = new DatagramPacket (receiveData, receiveData.length);
        clientSocket.receive (receivePacket);

        String modSentence = new String (receivePacket.getData());
        System.out.println ("FROM SERVER: " + modSentence);
        }
        //clientSocket.close();
    } catch (IOException e) {
        System.out.println (e.getMessage());
    }

}

//Create server
public static void main(String[] args) {

    int port = Integer.parseInt(args[0]);
    port1 = Integer.parseInt(args[1]);
    new UDPThreadServer ();
    try {
        DatagramSocket serverSocket = new DatagramSocket (port);
        byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];

        while (true) {
            DatagramPacket receivePacket = new DatagramPacket (receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            String sentence = new String(receivePacket.getData());
            SocketAddress address = receivePacket.getSocketAddress();
            System.out.println ("Received from " + address + " : " + sentence);

            InetAddress IPAddress = receivePacket.getAddress();
            String capSentence = sentence.toUpperCase();
            sendData = capSentence.getBytes();
            DatagramPacket sendPacket = new DatagramPacket (sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);
            //serverSocket.close();
        }

    } catch (IOException e) {
        System.out.println (e.getMessage());
    }
}

}

Thanks.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Triple777er
  • 621
  • 3
  • 17
  • 30

3 Answers3

1

Looking at UDPClientServer:

When you create the Datagram packet, you give it the port to send it to, not the port that you are sending it from.

When I ran your code, nothing happened. The server is waiting on port port, while the client sends to port port1. If you instead send to port port (not accessible from the main method, but changing it to a field instead of local method would fix that, then the infinite looping occurs because the server sends a packet to the same port it is listening on. There's your problem. Do you supply the same numbers as the first and second arguments to your program?

From the server, you can use receivePacket.getPort() to get the port where the packet came from.

EDIT:

Your two classes have much repetition, which is probably a source of confusion. One class has a main which starts a client then creates a server type loop tester. The other class sets up a Server then creates a client type tester.

Below is only the class you've named UDPThreadServer with comments showing changes to make the server 'work' with the testing code in the main method. Note that the server should send to a port that it is not listening to. You are also reading port values from command line arguments. I just made up some numbers for the ports and stuck them in as constants.

public class UDPThreadServer extends Thread
{

  public static int port1;

  UDPThreadServer()
  {
    //server or client?  it's hard to say.  you call the socket a clientSocket.
    System.out.println("Starting threaded server");
    start();
  }

  public void run()
  {
    try
    {
      // Here client(?) is set up with empty constructor.
      // It is a mystery what port it will get.
      DatagramSocket clientSocket = new DatagramSocket();

      BufferedReader inFromUser =
          new BufferedReader(new InputStreamReader(System.in));
      Scanner in = new Scanner(inFromUser);
      InetAddress IPAddress = InetAddress.getByName("localhost");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];

      while (in.hasNextLine())
      {
        String sentence = in.nextLine();
        // inFromUser.readLine();
        sendData = sentence.getBytes();

        // sending to port1?  that must be the server.
        DatagramPacket sendPacket =
            new DatagramPacket(sendData, sendData.length, IPAddress, port1);
        clientSocket.send(sendPacket);

        DatagramPacket receivePacket =
            new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);

        String modSentence = new String(receivePacket.getData());
        System.out.println("FROM SERVER: " + modSentence);
      }
      // clientSocket.close();
    } catch (IOException e)
    {
      System.out.println(e.getMessage());
    }
  }

  // Create server
  public static void main(String[] args)
  {

    // int port = Integer.parseInt(args[0]);
    int port = 1927; // or whatever
    // port1 = Integer.parseInt(args[1]);
    port1 = 1928;
    new UDPThreadServer();
    try
    {
      // server resides on port1?  if client sends to port 1, then this is so.
      DatagramSocket serverSocket = new DatagramSocket(port1);
      byte[] receiveData = new byte[1024];
      byte[] sendData = new byte[1024];

      while (true)
      {
        DatagramPacket receivePacket =
            new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        String sentence = new String(receivePacket.getData());
        SocketAddress address = receivePacket.getSocketAddress();
        System.out.println("Received from " + address + " : " + sentence);

        InetAddress IPAddress = receivePacket.getAddress();
        String capSentence = sentence.toUpperCase();
        sendData = capSentence.getBytes();

        // where did you get the info from?  Client is set up with an empty constructor, so it is a mystery.
        port = receivePacket.getPort();

        DatagramPacket sendPacket =
            new DatagramPacket(sendData, sendData.length, IPAddress, port);
        serverSocket.send(sendPacket);
        // serverSocket.close();
      }

    } catch (IOException e)
    {
      System.out.println(e.getMessage());
    }
  }
}
Atreys
  • 3,741
  • 1
  • 17
  • 27
  • Thank Atreys, when I try to print the value of getPort(), I get -1. When I actually remove the while(true), the while loop stops. However, I'm still unable to send more than once. I do understand what you said and I'm just a bit confused at the moment because everything seems to work right without the while loop, just the fact that I can't send more than once. – Triple777er May 31 '11 at 11:36
  • @Triple777er, I edited my answer with changes made to UDPThreadServer to get it to work with the main method. – Atreys May 31 '11 at 14:28
  • Thanks a lot Atreys, that helped me solve my problem. Thanks for taking the time to help me. Much appreciated. – Triple777er Jun 01 '11 at 05:27
0

Don't close the socket.

If that doesn't answer your question you need to clarify it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I haven't closed the socket. Whenever I send a message from the server, for e.g; "hello", at the receiving end, it prints hello in an infinite loop. – Triple777er May 31 '11 at 10:12
  • @Triple777er I suspect you're sending the reply back to yourself instead of to the sender. The reply-address is already in the received DatagramPacket: just put the reply data in there and send it. – user207421 May 31 '11 at 12:34
0

while(true) is an infinite loop. Do you quit it in any case?

mico
  • 12,730
  • 12
  • 59
  • 99
  • Setala, Yes I tried removing while (true) and it stops the infinite loop. However, I'm not able to write any data back to the server. I'm only able to write it once. – Triple777er May 31 '11 at 10:41