vSystem.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer,Int32 offset,Int32 size,SocketFlags socketflags,Endpoint& remoteEP) [modified]
-
Hi, I'm having trouble getting the UDPClient to work with C#. The udpClient has connected and sent a signal to 127.0.0.1 on port 8050 with no exceptions thrown. On the below line of code,
int recv = server.ReceiveFrom(data, ref tmpRemote);
I receive the below exception error, at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP) RemoteIPEndPoint is defined using the following line of code,
IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8050);
EndPoint tmpRemote = (EndPoint)sender;Any ideas why it throws the exception and how to solve it?
modified on Thursday, March 12, 2009 1:02 AM
-
Hi, I'm having trouble getting the UDPClient to work with C#. The udpClient has connected and sent a signal to 127.0.0.1 on port 8050 with no exceptions thrown. On the below line of code,
int recv = server.ReceiveFrom(data, ref tmpRemote);
I receive the below exception error, at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP) RemoteIPEndPoint is defined using the following line of code,
IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8050);
EndPoint tmpRemote = (EndPoint)sender;Any ideas why it throws the exception and how to solve it?
modified on Thursday, March 12, 2009 1:02 AM
mrithula8 wrote:
Any ideas why it throws the exception and how to solve it?
Nope. That info is contained in the SocketException object that gets thrown. You need to catch it and either print it or use the debugger to view it and find out what's going on.
-
Hi, I'm having trouble getting the UDPClient to work with C#. The udpClient has connected and sent a signal to 127.0.0.1 on port 8050 with no exceptions thrown. On the below line of code,
int recv = server.ReceiveFrom(data, ref tmpRemote);
I receive the below exception error, at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP) RemoteIPEndPoint is defined using the following line of code,
IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8050);
EndPoint tmpRemote = (EndPoint)sender;Any ideas why it throws the exception and how to solve it?
modified on Thursday, March 12, 2009 1:02 AM
Your exception is probably something to do with the buffer, offset, and size. here are some guidelines that may help you understand and solve your problem: The "buffer" must be large enough to accomodate the largest UDP datagram you expect to receive. Make sure you have a buffer that is large enough for you application. For example
byte[] buffer = new buffer[1024];
Next, "offset" is the index to the buffer. First recv this is probably zero (0). Next recv it may be advanced by the value from the datagram PDU, e.g. a size of payload. The key here is offet MUST be with the bounds of the buffer 0 to n. You should advance the offset by the bytes recv'd like this (with error checking of ocurse):int nbytes = recv(buffer, offset, size, ...);
offset += nbytes"Size" is determines how much. Let's say your first read got a 12 byte Header and in this header you extract a length. Well that means you would have used 12 for the first recv, and the if the payload size from the Header was 146 bytes, then your second recv would be for that amount. BUT you must make sure the offset advances before the second recv. Make sure you process recv errors, such as:
int nbytes = recv(buffer, offset, size, ...);
if (nbytes > 0)
offset += nbytes;
else
ProcessSocketError();Good luck
-
mrithula8 wrote:
Any ideas why it throws the exception and how to solve it?
Nope. That info is contained in the SocketException object that gets thrown. You need to catch it and either print it or use the debugger to view it and find out what's going on.
Hi, I used the SocketException to catch the exception It displays the exception that the existing connection was forcibly closed by the remote host.How do i find where the connection is getting closed?Can you give some suggestions //client
private void SendMessage()
{
try
{
listBox6.Items.Add("Connecting....");
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
listBox6.Items.Add("Connected");
byte[] data = new byte[1024];
String snd = "hello";
data = Encoding.ASCII.GetBytes(snd);
listBox6.Items.Add("Transmitting...");
sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep);//sent hello
listBox6.Items.Add("Sent...");
EndPoint tmpRemote = (EndPoint)ipep;
listBox6.Items.Add("Message received from {0}:");
listBox6.Items.Add(ipep.ToString());
data = new byte[1024];
int recv = sock.ReceiveFrom(data,0,data.Length, SocketFlags.None, ref tmpRemote);
String zz=Encoding.ASCII.GetString(data, 0, recv);
listBox6.Items.Add(zz);
if (zz == "Welcome to the Server")
{
lb9.Text = "Active";
lb9.BackColor = Color.Green;
}
else{
lb9.Text = "Inactive";
lb9.BackColor=Color.Red;
}
}catch (SocketException e) { //Console.WriteLine("Error..... " + e.StackTrace); MessageBox.Show(e.Message); } }
//server
public static void start_server()
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
Socket newsock = new Socket(AddressFamily.InterNetwork , SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
Console.WriteLine("Waiting for a client...");
while (true)
{
try
{
EndPoint tmpRemote = (EndPoint)ipep;
byte[] data = new byte[1024];
Console.WriteLine("hai");
int -
Hi, I used the SocketException to catch the exception It displays the exception that the existing connection was forcibly closed by the remote host.How do i find where the connection is getting closed?Can you give some suggestions //client
private void SendMessage()
{
try
{
listBox6.Items.Add("Connecting....");
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
listBox6.Items.Add("Connected");
byte[] data = new byte[1024];
String snd = "hello";
data = Encoding.ASCII.GetBytes(snd);
listBox6.Items.Add("Transmitting...");
sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep);//sent hello
listBox6.Items.Add("Sent...");
EndPoint tmpRemote = (EndPoint)ipep;
listBox6.Items.Add("Message received from {0}:");
listBox6.Items.Add(ipep.ToString());
data = new byte[1024];
int recv = sock.ReceiveFrom(data,0,data.Length, SocketFlags.None, ref tmpRemote);
String zz=Encoding.ASCII.GetString(data, 0, recv);
listBox6.Items.Add(zz);
if (zz == "Welcome to the Server")
{
lb9.Text = "Active";
lb9.BackColor = Color.Green;
}
else{
lb9.Text = "Inactive";
lb9.BackColor=Color.Red;
}
}catch (SocketException e) { //Console.WriteLine("Error..... " + e.StackTrace); MessageBox.Show(e.Message); } }
//server
public static void start_server()
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
Socket newsock = new Socket(AddressFamily.InterNetwork , SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
Console.WriteLine("Waiting for a client...");
while (true)
{
try
{
EndPoint tmpRemote = (EndPoint)ipep;
byte[] data = new byte[1024];
Console.WriteLine("hai");
intMSDN says that you have to call
Socket.Bind(...)
before you callSocket.ReceiveFrom(...)
. I see where you're doing that in your server code but not your client. I'd try adding a call to that function to the client. If you still have lots of problems gettingSocket
s to work then I'd suggest you take a look at the UdpClient[^] class. It simplifies a lot of things and makes sockets easier to work with.