| | |
UDP packet
![]() |
•
•
Join Date: Aug 2007
Posts: 60
Reputation:
Solved Threads: 0
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();
}
}
}
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();
}
}
}
![]() |
Similar Threads
- RAW Packet Forwarding (C++)
- UDP Packets (Windows NT / 2000 / XP)
- messenger problem (Windows NT / 2000 / XP)
- Windows NT2000 randomly restarting self (Windows NT / 2000 / XP)
- >>>>>NEED HELP<<<< (Windows NT / 2000 / XP)
- redirected homepage in IE (Web Browsers)
- Multiple IE starting (Web Browsers)
- IE6 startup page changed involuntarily - (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: How to Sort an array of different objects.
- Next Thread: please enlighten me on what i need to do to get this right
| Thread Tools | Search this Thread |
addball android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) button card class classes client code collision columns component constructor crashcourse css database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image integration intellij j2me java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle phone physics plazmic print problem program programming project radio scanner server service set sharepoint smart sms smsspam software sql subclass support swing textfield threads tree trolltech unlimited utility windows





