none
TCP Listener didn't receive message from tcpclient RRS feed

  • Question

  • Hi,
    i have a desktop application which receives data from client and process. For client server communication i used TCP Listener and TCPClient. Client send data everyday morninng 8:00AM to  the next day 1:00 AM. The next day client started to send message. But the server didn't receive message. but server shows the client is connected. after restart only  the server receives data from client.

    Why it is happened?
    Kindly advise is what changes should be required in my server code .

    Thanks,
    Mahalakshmi
    • Edited by Maha12 Saturday, March 7, 2020 5:41 AM
    Saturday, March 7, 2020 5:25 AM

All replies

  • There could be any # of reasons but I'm going to guess you're opening a connection to the server and leaving it open right? That is bad. Any hiccup in the network is going to disconnect the client. The client won't figure it out until they try to send the data and at that point there is nothing to do but reconnect. A hiccup can occur at any point on the network and could be temporary or permanent. 

    Your connections should be temporal. At the appointed time you should open a connect to the server and send your requests. If you're going to be sending a series of requests then leave the connection open until you're done. If you are going to send data only every X minutes (where there is a long gap such as hours) or something then open the connection, send the data and close it. At any point if you get a disconnect error you'll want to build in some retry logic. When I've done this sort of thing in the past I had a (configurable) retry/wait setting that specified something like "retry 3 times waiting 30 seconds between each attempt". If the retries fail then you should assume the server is down and do your normal error handling. Otherwise you can resume sending data.  The retry logic will allow your client/server to be more resilient to network issues.

    You might also look into the TCP keepalive stuff but I don't know if that is applicable to your situation.


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, March 7, 2020 5:01 PM
    Moderator
  • The next day client started to send message. But the server didn't receive message. but server shows the client is connected. after restart only the server receives data from client.

    Why it is happened?

    Judging based on your description of a very long wait between transmissions, my bet is that a firewall on either end is shutting down the ports you use to transmit data because the firewall thinks the connection is idle or broken and wants to protect its network from 3rd Party transmissions piggybacking the open pipe. The server and client still think they're connected to each other because neither side ever received a TCP disconnect signal, so when you try to send data from either end the firewall just discards the data and it never gets where you thought it was going.

    As CoolDadTx points out, you shouldn't just leave a socket perpetually open like that anyway. Either take his good advice to connect, do business, disconnect immediately or else look into TCP KeepAlive or else homebrew a system where one side sends a tiny amount of data and the other side responds - you'd have to time this to occur less than every 5 minutes for most firewalls, but it may be less. The only way to find the perfect delay value for your specific hardware is experimentation.


    In order to learn anything at all the first thing everybody has to learn is that there's stuff they don't know.

    Saturday, March 7, 2020 5:53 PM
  • Hi Maha12,
    Based on your description, I can't reproduce the situation. Could you provide some related code?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 9, 2020 9:43 AM
  • hello friend

     if (!tcpClient.Connected)  //Check for client connection

    tcpClient.connect(parameters) // client connection

    TcpListener server=new  TcpListener(IP,Port);

    Check every time status of the server  if (!server.Active) //server status check

    Start the server something like server=new  TcpListener(IP,Port); //Re start

     
    Monday, March 9, 2020 2:26 PM
  • No, this is not good advice.  You are mixing the client side with the server side.  Your tcpClient code can help recover from a dropped connection, but that's not the right answer for this specific question.

    And a TCP server shouldn't ever stop listening in the face of bad connections.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, March 9, 2020 5:16 PM
  • No, this is not good advice.  You are mixing the client side with the server side.  Your tcpClient code can help recover from a dropped connection, but that's not the right answer for this specific question.

    And a TCP server shouldn't ever stop listening in the face of bad connections.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Agreed.  I wanted to post something similar but I wasn't sure where to start.

    Additionally, tcpClient.Connected will continue to return TRUE when an intervening firewall shuts down TCP communications by port-blocking.  That's because the tcpClient expects a TCP signal representing a disconnect request or command, and the firewall won't send those out - it just prevents network traffic from passing through altogether.


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Monday, March 9, 2020 5:30 PM