alsoumhi 0 Junior Poster in Training

Hi gys I hope you all fine
I just have a question that I would send two UDP packets from the client to the server and the server will reply back the both messages to the client , my problem is that my program only sends one packet to the server not the both and thereby the server reply by sending one packet although I send the two packets to server why ???

this is my CLIENT PROGRAM:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class NewClient {


    public NewClient() {
    }


     static final int BUFFERSIZE = 255;
    public static void main(String[] args) {
        try
        {
            Scanner scan =new Scanner(System.in);
            System.out.println("please input f1 massages");
            String message1 = scan.nextLine();
            System.out.println("please input f2 massages");
            String message2 = scan.nextLine();

            byte data1[] = message1.getBytes();
            byte data2[] = message2.getBytes();
            // create sendPacket
            DatagramSocket socket=new DatagramSocket(2222);



              DatagramPacket sendPacket1 = new DatagramPacket( data1,data1.length, InetAddress.getLocalHost(), 1111 );
              socket.send( sendPacket1 ); // send packet1
              System.out.println("your message1: "+message1+" has been sent to the server"  );
              DatagramPacket receivePacket1= new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE);
              socket.receive( receivePacket1 ); // wait for packet
              String s1 = new String(receivePacket1.getData());
              System.out.println("we have received a message it is "+s1+"");
              System.out.println("I learn something "  );/*this step has not been reached why!!!!**/

              DatagramPacket sendPacket2 = new DatagramPacket( data2,data2.length, InetAddress.getLocalHost(), 1111 );
              socket.send( sendPacket2 ); // send packet1
              System.out.println("your message2: "+message2+" has been sent to the server"  );
               DatagramPacket receivePacket2 =new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE);
              socket.receive( receivePacket2 ); // wait for packet
                System.out.println("we have received a message it is "+new String(receivePacket2.getData())  );

        }
        catch ( Exception ioException ) {
                  System.out.println( ioException.toString() + "\n" );
                  ioException.printStackTrace();
               }

        System.out.println("Bye bye");
    }     

}

the server program:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class NewSErver {

    public NewSErver() {
    }

     static final int BUFFERSIZE = 255;
    public static void main(String[] args) throws IOException{

            byte data[] = new byte[ 100 ];
            DatagramSocket socket = new DatagramSocket(1111);
            DatagramPacket receivePacket1 = new DatagramPacket( new byte[BUFFERSIZE], BUFFERSIZE );
            DatagramPacket receivePacket2 = new DatagramPacket( new byte[BUFFERSIZE], BUFFERSIZE );
      try
       {
            String m = "ali";
            byte[] ali = m.getBytes();


       DatagramPacket sendPacket1 = new DatagramPacket(ali, ali.length, InetAddress.getLocalHost(), 2222 );
                socket.send(sendPacket1);
        socket.receive( receivePacket1 );
        System.out.println("first message received is "+new String( receivePacket1.getData()));
         socket.send( receivePacket1 );

        socket.receive( receivePacket2 );
        System.out.println("second message received is "+new String( receivePacket2.getData()));
         socket.send( receivePacket2 );

       }
       catch( IOException ioException ) {
            System.out.println( ioException.toString() + "\n" );
            ioException.printStackTrace();
         }


    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.