简体   繁体   中英

C# TCP Socket.Receive issue when reusing socket

preface:

I've been stumped on this for awhile now and am not having much luck finding what I need. I have a C# (.NET 3.5) Service. One thread acts as an asynchronous listener for incoming TCP connections. When data comes in I spawn off a new worker thread to handle the data, and sends an acknowledgement back.

On a second thread in the same service we send commands out, until today it would gather information from the data base, build a new socket, connect then ship the command and I'm using the Socket.Receive to invoke blocking and wait for a response (or until a timeout occurrs).

Everything has been working great until a new client has a need to send data to us so fast (5-10 second intervals) that we can no longer open a new socket to get a command through. So I started looking into when a command needs to be sent that the "listener" thread has a client connected. If that client is connected currently use that socket instead of creating a new one.

Issue: I'm to the point where I can send my command back on the same socket the listener receives, but when the client sends data back as the response it takes twice for the Socket.Receive method to actually fire thinking it received data. The first time it gets into my listener class, the 2nd time, in my command class where I actually want it to be.

Question: Is there some option or something I need to do before calling my Socket.Receive method to ensure the data gets to the correct place?

In my listener class I have a list of objects "CSocketPacket"

public class CSocketPacket
{
   public CSocketPacket(System.Net.Sockets.Socket socket)
   {
      thisSocket = socket;
      this.IpAddress =
          ((System.Net.IPEndPoint)socket.RemoteEndPoint).Address.ToString();
   }

   public System.Net.Sockets.Socket thisSocket;
   public byte[] dataBuffer = new byte[BUFFER_SIZE];
   public string IpAddress; //Use this to search for the socket
}

Then when I send a command I'm creating a new tcp socket object:

client = new Socket(
   AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(
   IPAddress.Parse(Strings.Trim(ipAddress)), port);
IPEndPoint LocalIp = new IPEndPoint(IPAddress.Parse(
   System.Configuration.ConfigurationManager.AppSettings["SourceIP"]), port);

then I'm looking into my listener class list to see if that socket is connected:

if (listener.SocketExists(ipAddress)) 
{
   // set the client socket in this class to the 
   // instance of the socket from the listener class
   SocketIndex = listener.FindSocketInList(ipAddress);
   if (SocketIndex != -1)
   {
      // might need to figure out how to avoid copying the socket
      // to a new variable ???
      client = listener.ConnectedSockets[SocketIndex].thisSocket;
      SocketBeingReUsed = true;
   }
}
else
{
   // try to connect to the client
   client.Connect(ep);
}

finally I go through my steps of sending and receiving

if (client.Connected)
{
   if (client.Poll(1000, SelectMode.SelectWrite))
   {
      int sentAmount = Send(ref client);
      client.ReceiveTimeout = 90000; //90 seconds
      returnData = ReceiveData(ref client, sentAmount);
   }
}

everything works up to the point in my ReceiveData(ref client, sentAmount) method where I call the Socket.Receive(data, total, Socket.ReceiveBufferSize, SocketFlags.None); method.

I've been using a tool called Hercules to test sending/receiving packets across two machines on my home network.

Does anyone have any ideas of what I can do to solve this? I do apologize for such a lengthy question but I want to try to give as much info and not paste my entire project. I'm up for any suggestions.

Disclaimer: I wrote this code approx 3 years ago, so I'm pry doing things I shouldn't be I'm sure :P

Thanks to all who read this.

Sincerely,

Chris

OK, so now I'm following along! Given what you've said in the comments above, then the way to solve the problem is to have a single class/thread that reads from the socket (which is the correct way to read from sockets anyway) and then it will coordinate which class gets the data. I think it might work a little like the Command Design Pattern .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM