Socket problems

J

Joshua Moore

Please help. I included the source code below. All I get from the buffer
is 0000000000000000. The exception it catches is "Message:
InvalidOperationException".

Thanks in advance,

Joshua Moore



public byte[] GetPacket()

{

byte[] buffer = new byte[82];

try

{

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

EndPoint sender = new IPEndPoint(IPAddress.Any,2068);

socket.Blocking = false;

socket.Bind(sender);

int test = socket.ReceiveFrom(buffer, ref sender);

Console.WriteLine(test.ToString());

if (buffer[0] == 1)

{

Console.WriteLine("IT WORKS!");

socket.ReceiveFrom(buffer, ref sender);

}

}

catch(SocketException e)

{

Console.WriteLine("SocketException caught!!!");

Console.WriteLine("Message : " + e.Message);


}

catch(ArgumentNullException e)

{

Console.WriteLine("ArgumentNullException caught!!!");

Console.WriteLine("Message : " + e.Message);

}

catch(NullReferenceException e)

{

Console.WriteLine("NullReferenceException caught!!!");

Console.WriteLine("Message : " + e.Message);

}

catch(Exception e)

{

Console.WriteLine("Exception caught!!!");

Console.WriteLine("Message : " + e.Message);

}

return buffer;

}

public Network()

{

}
 
A

Alex Feinman [MVP]

Not sure this is the problem, but why would you use ReceiveFrom on a bound
socket? I think you actually need Receive here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top