473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

UDP Async Server/Client Problem

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
1 9231
DMenT
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

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

Similar topics

1
by: Ben | last post by:
I've written a fair amount of sockets code using the Winsock2 API, but I am having some trouble converting to the .Net Sockets API, specifically asynchronous sockets. What I have is a form that is...
6
by: Vanessa | last post by:
I have a question regarding async mode for calling Microsoft.XMLHTTP object. Microsoft.XMLHTTP hangs the IE once in a while suddenly, but it will work again after half an hour or so without doing...
6
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to...
7
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets...
7
by: =?Utf-8?B?Q2FybG8gRm9saW5p?= | last post by:
Hi, I implemented asynchronous calls to a web resource (using HttpWebRequest) from asp.net 2.0. The request it's made asyncronously (I see that beginGetResponse returns immediately). The number...
15
by: dennis.richardson | last post by:
Greetings all. Here's a problem that's been driving me nuts for the last 48 hours. I'm hoping that someone has come across this before. I have a C# Application that reads a UDP broadcast...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
3
by: Ryan Liu | last post by:
Will TcpClient.GetStream().Read()/ReadByte() block until at least one byte of data can be read? In a Client/Server application, what does it mean at the end of stream/no more data available? ...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
1
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... There are a few questions wrapped up in this, but the main one is that the WebService.MyMethodAsync() methods that are automatically generated in the client code by VS 2005 don't seem to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.