| | |
UDP packet
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2007
Posts: 63
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
Views: 1685 | Replies: 0
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree webservices windows





