UDP packet

Reply

Join Date: Aug 2007
Posts: 60
Reputation: alsoumhi is an unknown quantity at this point 
Solved Threads: 0
alsoumhi alsoumhi is offline Offline
Junior Poster in Training

UDP packet

 
0
  #1
Sep 29th, 2008
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();
}


}
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC