Need help receiving UDP packets -- 10% not good enough
From: John Lindwall (jlindwall_nospam_at_nospam_yahoo.com)
Date: 04/10/04
- Previous message: Ishan Tigunait: "Re: How to create a UDP, broadcast socket with "On-Receive" notifications for WinCE?"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 09 Apr 2004 17:30:35 -0700
I need some help with receiving UDP packets on a Pocket PC. I have a
simple program that receives UDP packets in a tight loop. I use
PocketConsole to print a message when a packet arrives. When I run it I
only get 10-12 packets received out of 100 sent. I know that UDP is not
guaranteed delivery buit this seems ridiculously poor. I suspect my
code is flawed or my network/Pocket PC configuration is not correct.
If I run my ping utility that come with my Axim X3 (or the ping in
VxUtil) and all ping packets are sent/received properly. It seems like
my dorky little program should be more reliable.
FYI: I also wrote a Java version of this program using the Ewe VM and
get similar (un)reliability with receiving UDP.
I'm attaching the C# code that receives the UDP packets, and if it
matters, the java code that generates the packets.
The method of interest is StartReceiveFrom() in the file Class1.cs, near
the bottom of the file. I pasted it below also.
The program creates a thread that executes this StartReceiveFrom()
method. This method blocks waiting for a UDP Packet. I run a java
program to generate 10-byte packets to test it with, which contain a
packet number (1,2,3,...) in byte 0. This code attached prints the
packet number found at byte 0 of the received data and goes back to
waiting for another packet.
If you have a code that does what I am trying to do please share the joy! :)
(I'll attach that (UGLY) java packet generator program in case that
helps you too)
THANKS IN ADVANCE!
John Lindwall
----------- C# CODE THAT RECEIVES UDP
using System;
using System.Data;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SmartDeviceApplication2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
Console.WriteLine("Sleeping for 10 secs...");
System.Threading.Thread.Sleep(10000);
Console.WriteLine(networkAddressInfo());
new UdpServer();
}
private static string networkAddressInfo()
{
string Addresses=string.Empty;
string HostName=string.Empty;
string ppp_peer=string.Empty;
HostName=System.Net.Dns.GetHostName();
System.Net.IPHostEntry ipHost=System.Net.Dns.GetHostByName(HostName);
Addresses="IP Addresses:\r\n";
for ( int loop=0 ; loop < ipHost.AddressList.Length; loop++ )
{
Addresses+=ipHost.AddressList.GetValue(loop).ToString()
+ "\r\n";
}
try
{
ipHost=System.Net.Dns.GetHostByName("ppp_peer");
ppp_peer=ipHost.AddressList.GetValue(0).ToString();
}
catch
{
ppp_peer="Not available";
}
return "Host Name: " + HostName + "\r\n" +
Addresses + "ppp_peer info: \r\n" + ppp_peer;
}
}
}
//
//
// THIS GUY DOES ALL THE WORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
//
public class UdpServer
{
private const int udpPort = 51800;
public Thread udpThread;
public UdpServer()
{
try
{
//Starting the UDP Server thread.
udpThread = new Thread(new ThreadStart(StartReceiveFrom));
udpThread.Start();
Console.WriteLine("Started UdpServer's UDP Receiver Thread!\n");
}
catch (Exception e)
{
Console.WriteLine("An UDP Exception has occurred!" + e.ToString());
}
}
public void StartReceiveFrom()
{
IPHostEntry localHostEntry;
try
{
//Create a UDP socket.
Socket soUdp = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
try
{
localHostEntry = Dns.GetHostByName(Dns.GetHostName());
}
catch(Exception)
{
Console.WriteLine("Local Host not found"); // fail
return ;
}
IPEndPoint localIpEndPoint = new
IPEndPoint(localHostEntry.AddressList[0], udpPort);
soUdp.Bind(localIpEndPoint);
while (true)
{
Byte[] received = new Byte[256];
IPEndPoint tmpIpEndPoint = new
IPEndPoint(localHostEntry.AddressList[0], udpPort);
EndPoint remoteEP = (tmpIpEndPoint);
int bytesReceived = soUdp.ReceiveFrom(received, ref remoteEP);
byte packetNum = received[0];
Console.WriteLine("Got packet #{0}.", packetNum);
}
}
catch (SocketException se)
{
Console.WriteLine("A Socket Exception has occurred!" + se.ToString());
}
}
}
----------- JAVA COD THAT SENDS UDP
import java.net.*;
import java.io.*;
import java.util.Date;
public class PacketGenerator
{
public static void main(String[] args) throws IOException,
InterruptedException
{
PacketGenerator client = new PacketGenerator();
client.generateTraffic("192.168.0.4",
Integer.parseInt(args[0]));
}
private void delay(long millis) throws InterruptedException
{
Thread.sleep(millis);
}
private void generateTraffic(String hostname, int port) throws
IOException, InterruptedException
{
System.out.println("Generating UDP traffic to " + hostname +
"port" + port);
InetAddress address = InetAddress.getByName(hostname);
// create datagram packet
final int PACKET_SIZE = 10;
DatagramPacket packet = new DatagramPacket(new
byte[PACKET_SIZE], PACKET_SIZE, address, port);
// create datagram client socket
DatagramSocket clientSocket = new DatagramSocket();
boolean finished = false;
short i = 0;
while (!finished)
{
i++;
byte bytes[] = new byte[10];
bytes[0] = (byte) i;
packet.setData(bytes);
// write data to packet buffer
System.out.println("Sending " + packet.getLength() + " byte
packet #" + i + "to" + address);
clientSocket.send(packet);
delay(1000);
finished = false;
}
}
}
- Previous message: Ishan Tigunait: "Re: How to create a UDP, broadcast socket with "On-Receive" notifications for WinCE?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|