Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

UDP packet loss

843790Nov 24 2007 — edited Nov 25 2007
Hi I have a program which sends 2000 packets to achieve 1000Mbps sending rate with UDP protocol.. but the client reads only 2 packets. The code is
Server:

import java.io.*;
import java.net.*;
import java.nio.*;

public class Server {

public static void main(String[] args){
try{

DatagramSocket uss = new DatagramSocket(9000);
System.out.println("THE SEND BUFFER SIZE: "+uss.getSendBufferSize());
System.out.println("THE RECEIVE BUFFER SIZE: "+uss.getReceiveBufferSize());
uss.setSendBufferSize(100000);
uss.setReceiveBufferSize(100000);
System.out.println("THE SEND BUFFER SIZE: "+uss.getSendBufferSize());
System.out.println("THE RECEIVE BUFFER SIZE: "+uss.getReceiveBufferSize());
byte[] b = new byte[256];
DatagramPacket p1 = new DatagramPacket(b,256);
uss.receive(p1);
System.out.println("Received start from client");

SocketAddress sa = p1.getSocketAddress();
int port = p1.getPort();
InetAddress ca = p1.getAddress();

UDPPacket up = new UDPPacket();
byte[] tempByte = null;
InetAddress udpClientAddress = p1.getAddress();
int udpClientPort = p1.getPort();

int currentSize = 0;
int countSend=0;
long nano1 = System.nanoTime();
for(int i=0;i<1000*2;i++){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
up.change();
up.setPacketNum(countSend);
oos.writeUnshared(up);
oos.flush();
tempByte = baos.toByteArray();
DatagramPacket packet = new DatagramPacket(tempByte,tempByte.length,udpClientAddress,udpClientPort);
uss.send(packet);
countSend++;


}
long nano2 = System.nanoTime();
System.out.println("Time: "+(nano2-nano1));
System.out.println("Sent: "+countSend);

}catch(Exception ex){ex.printStackTrace();}

}

}

Client:

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

public class Client implements Runnable {

public Client(){
}

public void run(){
try{
DatagramSocket socket1 = new DatagramSocket();
System.out.println("SEND BUFFER SIZE: "+socket1.getSendBufferSize());
System.out.println("RECEIVE BUFFER SIZE:"+socket1.getReceiveBufferSize());
socket1.setReceiveBufferSize(100000);
socket1.setSendBufferSize(100000);
System.out.println("SEND BUFFER SIZE: "+socket1.getSendBufferSize());
System.out.println("RECEIVE BUFFER SIZE:"+socket1.getReceiveBufferSize());
InetAddress host = InetAddress.getByName("localhost");
String s = new String("start");
byte[] b = s.getBytes();
int portNum = 9000;
DatagramPacket p = new DatagramPacket(b,b.length,host,portNum);
Thread.sleep(4000);
socket1.send(p);
Thread.sleep(2000);

byte[] buf = new byte[65535];
DatagramPacket p2 = new DatagramPacket(buf,buf.length);
int count = 0;
socket1.setSoTimeout(500);

while(true){
try{
socket1.receive(p2);
++count;
}catch(Exception ex){ break;}
}
System.out.println("Received :"+count);
}catch(Exception ex){ex.printStackTrace();}

}

public static void main(String[] args){
Client c = new Client();
Thread t = new Thread(c);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}

}
Can someone please tell me what I am doing wrong.. Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 23 2007
Added on Nov 24 2007
3 comments
178 views