Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:49:20+00:00 2026-05-28T01:49:20+00:00

preface: I’ve been stumped on this for awhile now and am not having much

  • 0

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 😛

Thanks to all who read this.

Sincerely,

Chris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:49:20+00:00Added an answer on May 28, 2026 at 1:49 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Preface: This is not much about how to structure code within files. I have
Preface: I'm not sure what resources are. I need this form (which is working
Preface : I'm honestly not sure if this should be on StackOverflow, SuperUser or
Preface: I am sure this is incredibly simple, but I have searched this site
Preface I've rarely ever been a JS developer and this is my first attempt
Preface: This is the first real swing program I have done. I have a
Preface: This has become a quite a long post. While I'm not new to
To preface this, I'm not familiar with OLAP at all, so if the terminology
Preface: This is not a homework question. I'm going through an algos book in
To preface this question, I have a competent understanding of OpenGL and the maths

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.