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 -
-
public partial class Form1 : Form
-
{
-
private byte[] data = new byte[1024];
-
private int size = 1024;
-
private Socket server;
-
public static IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
-
public static IPEndPoint sender2 = new IPEndPoint(IPAddress.Any, 0);
-
public static EndPoint sender3 = (EndPoint)sender2;
-
-
public Form1()
-
{
-
CheckForIllegalCrossThreadCalls = false;
-
InitializeComponent();
-
server = new Socket(AddressFamily.InterNetwork,
-
SocketType.Dgram, ProtocolType.Udp);
-
server.Bind(iep);
-
server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(connTest), server);
-
}
-
-
-
void connTest(IAsyncResult iar)
-
{
-
Socket client = (Socket)iar.AsyncState;
-
-
-
//Receive Data from Client
-
-
string dataString;
-
client.ReceiveFrom(data, ref sender3);
-
dataString = Encoding.ASCII.GetString(data);
-
results.Items.Add(dataString);
-
-
-
data = new byte[1024];
-
int sent = client.EndReceive(iar);
-
server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(connTest), client);
-
}
-
-
-
}
-
UDP Client (Code was cut down a bit) -
-
public partial class Form1 : Form
-
{
-
public static byte[] data = new byte[1024];
-
public static string stringData;
-
public static UdpClient server = new UdpClient("127.0.0.1", 9050);
-
public static IPEndPoint sender2 = new IPEndPoint(IPAddress.Any, 0);
-
public static Socket newSock = new Socket(AddressFamily.InterNetwork,
-
SocketType.Dgram, ProtocolType.Udp);
-
-
-
void sentData(IAsyncResult iar)
-
{
-
label5.Text = "Data Sent";
-
}
-
-
-
private void submitText_Click(object sender, EventArgs e)
-
{
-
//data
-
data = new byte[1024];
-
data = Encoding.ASCII.GetBytes(input.Text);
-
-
//Send message to server
-
server.BeginSend(data, data.Length, sentData, server);
-
}
-
}
-
-
-
Any help would be appreciated!
Thanks.
1 9231
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 -
-
public partial class Form1 : Form
-
{
-
private byte[] data = new byte[1024];
-
private int size = 1024;
-
private Socket server;
-
public static IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
-
public static IPEndPoint sender2 = new IPEndPoint(IPAddress.Any, 0);
-
public static EndPoint sender3 = (EndPoint)sender2;
-
-
public Form1()
-
{
-
CheckForIllegalCrossThreadCalls = false;
-
InitializeComponent();
-
server = new Socket(AddressFamily.InterNetwork,
-
SocketType.Dgram, ProtocolType.Udp);
-
server.Bind(iep);
-
server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(connTest), server);
-
}
-
-
-
void connTest(IAsyncResult iar)
-
{
-
Socket client = (Socket)iar.AsyncState;
-
-
-
//Receive Data from Client
-
-
string dataString;
-
client.ReceiveFrom(data, ref sender3);
-
dataString = Encoding.ASCII.GetString(data);
-
results.Items.Add(dataString);
-
-
-
data = new byte[1024];
-
int sent = client.EndReceive(iar);
-
server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(connTest), client);
-
}
-
-
-
}
-
UDP Client (Code was cut down a bit) -
-
public partial class Form1 : Form
-
{
-
public static byte[] data = new byte[1024];
-
public static string stringData;
-
public static UdpClient server = new UdpClient("127.0.0.1", 9050);
-
public static IPEndPoint sender2 = new IPEndPoint(IPAddress.Any, 0);
-
public static Socket newSock = new Socket(AddressFamily.InterNetwork,
-
SocketType.Dgram, ProtocolType.Udp);
-
-
-
void sentData(IAsyncResult iar)
-
{
-
label5.Text = "Data Sent";
-
}
-
-
-
private void submitText_Click(object sender, EventArgs e)
-
{
-
//data
-
data = new byte[1024];
-
data = Encoding.ASCII.GetBytes(input.Text);
-
-
//Send message to server
-
server.BeginSend(data, data.Length, sentData, server);
-
}
-
}
-
-
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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? ...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
| |