Socket Appliation throw a socketExpcetion in wan after a fixed time

亮亮 孙 20 Reputation points
2024-02-15T01:30:37.5866667+00:00

I have a socket application. Server:

public static void Main(string[] args)
{
    try
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        Socket server_socketListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
       
        IPAddress ips = IPAddress.Parse("127.0.0.1");
        IPEndPoint ipNode = new IPEndPoint(ips, 9998);
        server_socketListen.Bind(ipNode);
        server_socketListen.Listen(10);
        Socket socket_commu = server_socketListen.Accept();
        while (true)
        {
            byte[] buffer = new byte[1024 * 1024];
          
            int num = socket_commu.Receive(buffer);
            string str = Encoding.UTF8.GetString(buffer, 0, num);
            Console.WriteLine(DateTime.Now.ToString() + ":" + "data : " + str);
            System.Threading.Thread.Sleep(480000);
         
            socket_commu.Send(Encoding.UTF8.GetBytes("server:" + str));
        }
    }catch (Exception ex) 
    {
       
    }
}

Client:

public static void Main(string[] args)
{
    Socket client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
   
    IPAddress ipAdress = IPAddress.Parse("127.0.0.1");
  
    IPEndPoint ipEndpoint = new IPEndPoint(ipAdress, 9998);
  
    client_socket.Connect(ipEndpoint);
    while (true)
    {
        string rl = Console.ReadLine();
      
        client_socket.Send(Encoding.UTF8.GetBytes(rl.ToUpper()));
        byte[] buffer = new byte[1024 * 1024];
       
        int num = client_socket.Receive(buffer);
        string str = Encoding.UTF8.GetString(buffer, 0, num);
        Console.WriteLine(DateTime.Now.ToString() + ":"+"data : " + str);
    }
}

I write " System.Threading.Thread.Sleep(480000);" in server, it's mean Server do something when receive client's data. when ipAddress is '127.0.0.1' or lan ip, it's run and client get right server's response! but if ipAddress is wan ip and set sleep's time is large time,such as 480000, and the server run in server computer with wan ip, and the client run in client compute, after a fixed time(maybe 240000), the server will throw a socketexception(errorcode:10060,msg: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)!! i think it's seem socket server nerver timeout in lan, but if in wan ,server will throw a socketexception after a fixed time value Can i adjust this time value?

Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-02-15T09:19:54.83+00:00

    Hi @亮亮 孙 , Welcome to Microsoft Q&A,

    The exception is thrown due to connection timeout, not socket timeout. In a WAN environment, connections are typically more restricted, so the default connection timeout may be shorter than in a LAN environment.

    You can set the connection timeout in client code to extend the duration of the connection. In the C# Socket class, you can achieve this by setting the timeout of the Socket.Connect method. Here's how to modify the client code to set a connection timeout:

    public static void main(String[] parameters)
    {
        Socket client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
       
        IPAddress ipAdress = IPAddress.Parse("your_server_wan_ip");
      
        IPEndPoint ipEndpoint = new IPEndPoint(ipAdress, 9998);
    
        //Set the connection timeout to 10 minutes (600000 milliseconds)
        client_socket.ConnectTimeout = 600000;
    
        try
        {
            client_socket.Connect(ipEndpoint);
        }
        catch (before SocketException)
        {
            // Handle connection exception
            Console.WriteLine("Connection failed: " + ex.Message);
            return;
        }
    
        // Code after successful connection
        And (true)
        {
            String rl = Console.ReadLine();
          
            client_socket.Send(Encoding.UTF8.GetBytes(rl.ToUpper()));
            Bytes[] buffer = new Bytes[1024 * 1024];
           
            int num = client_socket.Receive(buffer);
            String str = Encoding.UTF8.GetString(buffer, 0, num);
            Console.WriteLine(DateTime.Now.ToString() + ":"+"data : " + str);
        }
    }
    

    You can extend the connection timeout by setting the ConnectTimeout property. Note that the timeout is in milliseconds.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.