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 8986
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.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by Ben |
last post: by
|
6 posts
views
Thread by Vanessa |
last post: by
|
6 posts
views
Thread by Shak |
last post: by
|
7 posts
views
Thread by Shak |
last post: by
|
7 posts
views
Thread by =?Utf-8?B?Q2FybG8gRm9saW5p?= |
last post: by
|
15 posts
views
Thread by dennis.richardson |
last post: by
|
10 posts
views
Thread by Frankie |
last post: by
|
3 posts
views
Thread by Ryan Liu |
last post: by
|
1 post
views
Thread by Ryan Liu |
last post: by
|
1 post
views
Thread by =?Utf-8?B?TWFyaw==?= |
last post: by
| | | | | | | | | | |