10054 Socket Error C#
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss socket error 10054 connection reset by peer delphi the workings and policies of this site About Us Learn more
Socket Error 10054 Ftp
about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow windows socket error 10054 Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping socket error errno 10054 each other. Join them; it only takes a minute: Sign up C# Socket ErrorCode 10054 on Client Refresh Connection up vote 1 down vote favorite I've been attempting to write a communication layer utilizing TCP for an application that I am building. I control both server and client side operations. The problem that I am running in to
Socket Error Code 10054
is that when I use 'localhost' as my IP, I have no problems. However, when I connect using the internet, everything works magically...the first time. When the client attempts to build another socket connection to the server, I get SocketException ErrorCode 10054 on the server side, during ReadCallback, specifically when I perform a socket.EndReceive operation. I have already looked at the possibility that my sockets are not getting disposed properly - they sure should be, as I cannot see a path out of the client or server side application which doesn't lead to a close. My code fairly closely resembles code from MSDN, which can be found here (server) and here (client) Here is the relevant parts of my client code: public class ClientStateObject { // Client socket. public Socket workSocket = null; // Receive buffer. public byte[] buffer = new byte[Constants.ClientBuffer]; // Received data string. public StringBuilder sb = new StringBuilder(); public List
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack wsa error 10054 Overflow the company Business Learn more about hiring developers or posting ads with us Stack socketerror 10054 Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community
Socket Error 10054 Connection Reset By Peer
of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Socket.EndAccept() error 10054 up vote 1 down vote favorite 2 new user to the site here. I'm http://stackoverflow.com/questions/30145027/c-sharp-socket-errorcode-10054-on-client-refresh-connection working on a simple asynchronous tcp server. My connection listener looks like this. public static bool Listen(int port) { try { IPEndPoint ep = new IPEndPoint(IPAddress.Any, port); listener = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listener.Bind(ep); listener.Listen(4); listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); return true; } catch (Exception e) { Console.WriteLine("Unknown exception: {0}", e.ToString()); return false; } } private static void AcceptCallback(IAsyncResult res) { try { Socket listener = (Socket)res.AsyncState; Socket inSocket = listener.EndAccept(res); Console.WriteLine("Accepted handle: http://stackoverflow.com/questions/7704417/socket-endaccept-error-10054 {0}", inSocket.Handle); listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); } catch (SocketException se) { Console.WriteLine("SocketException: {0}", se.ErrorCode); } catch (Exception e) { Console.WriteLine("Unknown exception: {0}", e.ToString()); } } Most of the time it works fine, but occasionally listener.EndAccept(res) triggers a socketexception. It has the error code 10054. The error then keeps occurring for every connection attemp until I restart the listener. What could the problem be? Also an additional question, what should I set my backlog to? c# .net sockets tcp share|improve this question asked Oct 9 '11 at 15:06 Anders J. 133 Why do you have special catch for the SocketException? If you let it fall through into catch (Exception) you would see the actual error message and understand better what is going on. –Ilia G Oct 9 '11 at 15:23 I made a special cast for SocketException because I wanted the native socket error code. I don't have much coding experience so I figured that a special catch would be the best for this one. –Anders J. Oct 9 '11 at 15:31 Use Listen(int.MaxValue); –Hans Passant Oct 9 '11 at 15:40 add a comment| 1 Answer 1 active oldest votes up vote 0 down vote accepted Occasional "connection-resets" are legal. Client may have closed the connection before you sta
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this http://stackoverflow.com/questions/13666782/why-does-socket-receive-throw-socketexception-10054-when-remote-host-disconnec site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why does socket error Socket.Receive throw SocketException (10054) when remote host disconnects? up vote 1 down vote favorite I used to write socket programs in C and can't understand why the above is happening. My server blocks at a Receive call, when it returns 0, I break out of the while loop and shutdown the thread. public class MyServer { public MyServer() { } public void Init() { socket error 10054 ThreadPool.QueueUserWorkItem(StartListening); while (true) { Console.Read(); } } public void StartListening(Object state) { // Establish the local endpoint for the socket. IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { // Bind the socket to the local endpoint and listen for incoming connections. listener.Bind(localEndPoint); listener.Listen(100); while (true) { Console.WriteLine("Waiting for a connection..."); // get connection Socket client = listener.Accept(); // push client to another thread ThreadPool.QueueUserWorkItem(HandleClient, client); } } catch (Exception e) { Console.WriteLine(e.ToString()); } } private void HandleClient(Object obj) { // Get the socket that handles the client request. Socket client = (Socket)obj; // Create the state object. StateObject state = new StateObject(); state.workSocket = client; try { while (true) { int bytesRead = client.Receive(state.buffer); Console.WriteLine("bytesRead=" + bytesRead); // remote dc. if (bytesRead == 0) break; String content = Encoding.ASCII.GetString(state.buffer, 0, bytesRead); Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content); client.Send(state.buffer, 0, state.buffer.Length, 0); } } catch (SocketException e) { Console.WriteLine("SocketException : {0}", e.ToString()); } client.Shutdown(SocketShutdown.Both); client.Close(); } private void Send(Socket handler, String data) { byte[] byteData = Encoding.ASCII.GetBytes(data); // Begin sending the data to the remote de