tags:

views:

40

answers:

2

When I use sendto() to send messages, i get the error "message too long"?

 Socket tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

How can I find out the maximum possible size that i can send? Can I change it? What could be possible problem here?

A: 

From the Linux man pages for socket(), you should be able to use the sysctls(2) or /proc/sys/net/core/* files. I think in particular you could use the following:

rmem_default
contains the default setting in bytes of the socket receive buffer.
rmem_max
contains the maximum socket receive buffer size in bytes which a user may set by using the SO_RCVBUF socket option.

  • Use the first one to check what the default size is for the receive buffer.
  • Look at the second one to determine maximum size and ensure the size of the packets you are sending are less than that.
  • Use the SO_RCVBUF socket option to change the size of the receive buffer.
IntelliChick
+1  A: 

First of all, SendTo is only used for UDP sockets. In the code snippet above, you are opening a TCP socket. SendTo won't work with a TCP socket. Try it with a UDP socket and see if it works. Bear in mind that the maximum practical size of a UDP packet is 65,507 bytes. Generally, you want to keep UDP packets small to avoid fragmentation by the various network elements involved with their transmission.

EDIT:

Use TcpClient to make your life easier.

Int32  port = 13293;
String host = "somehost.com";
TcpClient tcpClient = new TcpClient(host, port);

Byte[] data = System.Text.Encoding.ASCII.GetBytes("your message to send");

NetworkStream stream = tcpClient.GetStream();

stream.Write(data, 0, data.Length);
lrm
Or connect the TCP socket and use `Send`.
Stephen Cleary
I was using connect and then sendto. I also tried using Stream.write(). But still i get the same error. I have to use TCP socket and cant use UDP. Also my message size is hardly 30 bytes.
Sam
I updated the answer with an example using TcpClient.
lrm