TcpListener Hangs at 4096 bytes?
-
The BeginListener function will not receive any information from the socket past a certain amount (apparently 4096 bytes). Information sent that is smaller than that is received properly. If data is sent that is larger than that, it's truncated to 4096 bytes, and then the while loop exits (which it should never do).
private void BeginListener() { byte[] b = new byte[10000000]; int k; Socket s; String strMessage = ""; TcpListener newList = new TcpListener(10000); newList.Start(); while (true) { strMessage = ""; s = newList.AcceptSocket(); k = s.Receive(b); for (int nCounter = 0; nCounter < k; nCounter++) { strMessage += (char)b[nCounter]; } } newList.Stop(); }
private void SendCommand(String Address,String strTempFile) { StreamReader newReader = new StreamReader(strTempFile); String strSendString = newReader.ReadToEnd(); newReader.Close(); newClient = new TcpClient(); NetworkStream tcpStream = null; byte[] ba; try { newClient.Connect(Address,10000); tcpStream = newClient.GetStream(); ASCIIEncoding asem = new ASCIIEncoding(); ba = asem.GetBytes(strSendString); tcpStream.Write(ba,0,ba.Length); } catch (System.Net.Sockets.SocketException) { //Error Handling. } finally { if (tcpStream != null) {tcpStream.Close();} if (newClient != null) {newClient.Close();} } }
Any help would be greatly appreciated. -
The BeginListener function will not receive any information from the socket past a certain amount (apparently 4096 bytes). Information sent that is smaller than that is received properly. If data is sent that is larger than that, it's truncated to 4096 bytes, and then the while loop exits (which it should never do).
private void BeginListener() { byte[] b = new byte[10000000]; int k; Socket s; String strMessage = ""; TcpListener newList = new TcpListener(10000); newList.Start(); while (true) { strMessage = ""; s = newList.AcceptSocket(); k = s.Receive(b); for (int nCounter = 0; nCounter < k; nCounter++) { strMessage += (char)b[nCounter]; } } newList.Stop(); }
private void SendCommand(String Address,String strTempFile) { StreamReader newReader = new StreamReader(strTempFile); String strSendString = newReader.ReadToEnd(); newReader.Close(); newClient = new TcpClient(); NetworkStream tcpStream = null; byte[] ba; try { newClient.Connect(Address,10000); tcpStream = newClient.GetStream(); ASCIIEncoding asem = new ASCIIEncoding(); ba = asem.GetBytes(strSendString); tcpStream.Write(ba,0,ba.Length); } catch (System.Net.Sockets.SocketException) { //Error Handling. } finally { if (tcpStream != null) {tcpStream.Close();} if (newClient != null) {newClient.Close();} } }
Any help would be greatly appreciated.You should never read this much information in one read. Many servers don't even send information that fast and your socket times-out while waiting. Buffer your respones into
byte[4096]
(a good recommended size) and write them out to a stream (using the read byte count) as they're read-in. This is how practically every transport mechanism works. "Well, I wouldn't say I've been missing it, Bob." - Peter Gibbons -
The BeginListener function will not receive any information from the socket past a certain amount (apparently 4096 bytes). Information sent that is smaller than that is received properly. If data is sent that is larger than that, it's truncated to 4096 bytes, and then the while loop exits (which it should never do).
private void BeginListener() { byte[] b = new byte[10000000]; int k; Socket s; String strMessage = ""; TcpListener newList = new TcpListener(10000); newList.Start(); while (true) { strMessage = ""; s = newList.AcceptSocket(); k = s.Receive(b); for (int nCounter = 0; nCounter < k; nCounter++) { strMessage += (char)b[nCounter]; } } newList.Stop(); }
private void SendCommand(String Address,String strTempFile) { StreamReader newReader = new StreamReader(strTempFile); String strSendString = newReader.ReadToEnd(); newReader.Close(); newClient = new TcpClient(); NetworkStream tcpStream = null; byte[] ba; try { newClient.Connect(Address,10000); tcpStream = newClient.GetStream(); ASCIIEncoding asem = new ASCIIEncoding(); ba = asem.GetBytes(strSendString); tcpStream.Write(ba,0,ba.Length); } catch (System.Net.Sockets.SocketException) { //Error Handling. } finally { if (tcpStream != null) {tcpStream.Close();} if (newClient != null) {newClient.Close();} } }
Any help would be greatly appreciated.First of all, there no such limitation in the .NET Socket implementation : the actual code beinh Receive() is in fact a direct Winsock2 recv() function call, and it passes your buffer directly, not an intermediate buffer. I can see an error in your code : in your loop, you are Accepting a new socket instance at each iteration. :confused: Usually, you accept a socket once, and then loop with Receive.