By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 2,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

UDP Async Server/Client Problem

P: 1
Hello, I've just recently began network programming for c# (bought the book C# Network Programming) and I've ran into a few problems. Whenever I click the submit button on the client (to send the UDP packet to the UDP server), it doesn't work the first time (so it seems) and I must click it again to get the server to receive the packet and display it in a List Box. I was wondering if somone could help me track this problem down, I'm sure it's just something simple because I'm still very new to Network Programming. Here is my code:

UDP Server
Expand|Select|Wrap|Line Numbers
  1.  
  2. public partial class Form1 : Form
  3.     {       
  4.         private byte[] data = new byte[1024];
  5.         private int size = 1024;
  6.         private Socket server;          
  7.         public static IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
  8.         public static IPEndPoint sender2 = new IPEndPoint(IPAddress.Any, 0);
  9.         public static EndPoint sender3 = (EndPoint)sender2;
  10.  
  11.         public Form1()
  12.         {
  13.             CheckForIllegalCrossThreadCalls = false;
  14.             InitializeComponent();  
  15.             server = new Socket(AddressFamily.InterNetwork,
  16.             SocketType.Dgram, ProtocolType.Udp);     
  17.             server.Bind(iep);           
  18.             server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(connTest), server);
  19.         }
  20.  
  21.  
  22.         void connTest(IAsyncResult iar)
  23.         {
  24.             Socket client = (Socket)iar.AsyncState;
  25.  
  26.  
  27.             //Receive Data from Client
  28.  
  29.             string dataString;           
  30.             client.ReceiveFrom(data, ref sender3);
  31.             dataString = Encoding.ASCII.GetString(data);
  32.             results.Items.Add(dataString);         
  33.  
  34.  
  35.             data = new byte[1024];
  36.             int sent = client.EndReceive(iar);
  37.             server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(connTest), client);
  38.         }      
  39.  
  40.  
  41.     }
  42.  


UDP Client (Code was cut down a bit)
Expand|Select|Wrap|Line Numbers
  1.  
  2.   public partial class Form1 : Form
  3.     {
  4.         public static byte[] data = new byte[1024];
  5.         public static string stringData;
  6.         public static UdpClient server = new UdpClient("127.0.0.1", 9050);
  7.         public static IPEndPoint sender2 = new IPEndPoint(IPAddress.Any, 0);
  8.         public static Socket newSock = new Socket(AddressFamily.InterNetwork,
  9.                 SocketType.Dgram, ProtocolType.Udp);
  10.  
  11.  
  12. void sentData(IAsyncResult iar)
  13.         {
  14.             label5.Text = "Data Sent";
  15.         }
  16.  
  17.  
  18. private void submitText_Click(object sender, EventArgs e)
  19.         {
  20.             //data
  21.             data = new byte[1024];
  22.             data = Encoding.ASCII.GetBytes(input.Text);
  23.  
  24.             //Send message to server
  25.             server.BeginSend(data, data.Length, sentData, server);            
  26.         }
  27. }
  28.  
  29.  
  30.  
Any help would be appreciated!

Thanks.
Jun 12 '06 #1
Share this Question
Share on Google+
1 Reply


P: 1
Hello, I've just recently began network programming for c# (bought the book C# Network Programming) and I've ran into a few problems. Whenever I click the submit button on the client (to send the UDP packet to the UDP server), it doesn't work the first time (so it seems) and I must click it again to get the server to receive the packet and display it in a List Box. I was wondering if somone could help me track this problem down, I'm sure it's just something simple because I'm still very new to Network Programming. Here is my code:

UDP Server
Expand|Select|Wrap|Line Numbers
  1.  
  2. public partial class Form1 : Form
  3.     {       
  4.         private byte[] data = new byte[1024];
  5.         private int size = 1024;
  6.         private Socket server;          
  7.         public static IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
  8.         public static IPEndPoint sender2 = new IPEndPoint(IPAddress.Any, 0);
  9.         public static EndPoint sender3 = (EndPoint)sender2;
  10.  
  11.         public Form1()
  12.         {
  13.             CheckForIllegalCrossThreadCalls = false;
  14.             InitializeComponent();  
  15.             server = new Socket(AddressFamily.InterNetwork,
  16.             SocketType.Dgram, ProtocolType.Udp);     
  17.             server.Bind(iep);           
  18.             server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(connTest), server);
  19.         }
  20.  
  21.  
  22.         void connTest(IAsyncResult iar)
  23.         {
  24.             Socket client = (Socket)iar.AsyncState;
  25.  
  26.  
  27.             //Receive Data from Client
  28.  
  29.             string dataString;           
  30.             client.ReceiveFrom(data, ref sender3);
  31.             dataString = Encoding.ASCII.GetString(data);
  32.             results.Items.Add(dataString);         
  33.  
  34.  
  35.             data = new byte[1024];
  36.             int sent = client.EndReceive(iar);
  37.             server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(connTest), client);
  38.         }      
  39.  
  40.  
  41.     }
  42.  


UDP Client (Code was cut down a bit)
Expand|Select|Wrap|Line Numbers
  1.  
  2.   public partial class Form1 : Form
  3.     {
  4.         public static byte[] data = new byte[1024];
  5.         public static string stringData;
  6.         public static UdpClient server = new UdpClient("127.0.0.1", 9050);
  7.         public static IPEndPoint sender2 = new IPEndPoint(IPAddress.Any, 0);
  8.         public static Socket newSock = new Socket(AddressFamily.InterNetwork,
  9.                 SocketType.Dgram, ProtocolType.Udp);
  10.  
  11.  
  12. void sentData(IAsyncResult iar)
  13.         {
  14.             label5.Text = "Data Sent";
  15.         }
  16.  
  17.  
  18. private void submitText_Click(object sender, EventArgs e)
  19.         {
  20.             //data
  21.             data = new byte[1024];
  22.             data = Encoding.ASCII.GetBytes(input.Text);
  23.  
  24.             //Send message to server
  25.             server.BeginSend(data, data.Length, sentData, server);            
  26.         }
  27. }
  28.  
  29.  
  30.  
Any help would be appreciated!

Thanks.
The problem I've seen in your code is the following:

client.ReceiveFrom(data, ref sender3);

In this line you are attemping to read from the client socket, but at this point you already have a message in the data Byte array.

You could put a breakpoint at this line and watch the value for the data variable to se the behavior I am trying to explain.

Sorry for my bad english, I hope my comments let you some help.

Kind Regards,
DMenT.
Oct 26 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.