• 0

Help with TCP Socket Server Client


Question

Hi,

 

I am trying to write this TCP Socket Client Server app for testing purposes. It works for the most part but some things are not working and I need some help. Please see the project attached created in VS2013 targeting .NET Framework 4.5 using C#.

 

ServerClient.zip

 

I took the socket code from here https://code.msdn.microsoft.com/High-Performance-NET-69c2df2f

 

The issues I am having are,

 

* When I stop listening on server, I am still receiving data. I would expect that the client should find that it is not connected anymore and throw an error and disconnect.

* I need to send server time back to the client upon receiving data (once the receive is complete). I am doing that but it is not working.

* I have set the buffer size to 64 bytes but if I send more data then it doesn't like that. I thought it was possible to grab data to fill that buffer and then continue receiving more until it finishes.

* One last thing might I ask is some suggestions with error handling since the socket code from MSDN says the "LastError" is not the correct way of doing this.

 

So I am way over my head with TCP stuff because I don't do network programming often and need some help to sort of these issues.

 

Cheers :)

Link to comment
https://www.neowin.net/forum/topic/1257794-help-with-tcp-socket-server-client/
Share on other sites

2 answers to this question

Recommended Posts

  • 0
  On 26/05/2015 at 05:34, wrack said:

* When I stop listening on server, I am still receiving data. I would expect that the client should find that it is not connected anymore and throw an error and disconnect.

From a cursory glance, it doesn't appear to close accepted sockets in Stop().

public void Stop()
{
ConnectionSocket.Close();
_limitClientConnectionMutex.ReleaseMutex();
}
I don't know C# or .Net sockets very well, but isn't ConnectionSocket merely the listening socket? Calling this function won't actually close/inform the client that you disconnected. In C, I'd usually iterate through the client connections and gracefully shut them down:

for ( iter = list_first ( clients ); NULL != iter; iter = list_next ( clients, iter ) ) {
Socket *s = list_get_ptr ( iter );
shutdown ( s->fd, SHUT_RDWR );
close ( s->fd );
}
list_free ( clients );
  On 26/05/2015 at 05:34, wrack said:

* I need to send server time back to the client upon receiving data (once the receive is complete). I am doing that but it is not working.

I'd suggest you debug ProcessData() in the SocketUserToken class. You appear to be setting the non-blocking write buffer for the socket there:

var sendBuffer = Encoding.UTF8.GetBytes(received + " Time : " + DateTime.UtcNow.ToString("O")); // "O" = ISO-8601
args.SetBuffer(sendBuffer, 0, sendBuffer.Length);
You need to confirm this gets executed and does what it's supposed to do before you close the socket. I'm assuming the underlying class automatically sends the buffer once there's data there. If not, you may have to call some kind of send() function.

 

  On 26/05/2015 at 05:34, wrack said:

* I have set the buffer size to 64 bytes but if I send more data then it doesn't like that. I thought it was possible to grab data to fill that buffer and then continue receiving more until it finishes.

I think the problem is that you're limiting your packet/message buffer '_stringBuilder' in SocketUserTokenClass to DefaultBufferSize (64 bytes) as well. Specifically in SocketServerClass's ProcessAcept():

readSocket.UserToken = new SocketUserTokenClass(AddToLoggerMethod, acceptSocket, DefaultBufferSize);
Which translates to this executing:

if ((_totalByteCount + bytecount) > _stringBuilder.Capacity)
{
LastError = "Receive Buffer cannot hold the entire message for this connection.";
return false;
}
'_stringBuilder' is set to 64 by your DefaultBufferSize, so if you send any more than that it will error out. To be honest, I think SocketUserTokenClass is just a placeholder for your own custom receive/parsing packet functions. It's very rudimentary. However, provided you change the packet/message buffer size passed into the SocketUserTokenClass constructor, you should be fine. You need to understand that there are actually two buffers involved here. 1) The socket read buffer, and 2) The packet/message buffer. That's how most reliable network code works. You construct the packet/message once you've received the whole thing. That's usually done via encoding such as prefixing the packet size.

 

  On 26/05/2015 at 05:34, wrack said:

* One last thing might I ask is some suggestions with error handling since the socket code from MSDN says the "LastError" is not the correct way of doing this.

Sounds a lot like 'errno' from ISO C. There are only really two ways to handle errors:

1) Check return value from a function. Sometimes this is used in conjunction with 'errno' to determine the real error. For instance, the socket() function returns -1 if an error occurred. To obtain the actual error, you can access errno/call strerror().

2) Employ try/catch exception handling. Though in the case of sockets, it might not be efficient. After all, network errors are so common. I suppose it depends on how often you expect them to occur and whether you're willing to accept the performance penalty.

 

  On 26/05/2015 at 05:34, wrack said:

So I am way over my head with TCP stuff because I don't do network programming often and need some help to sort of these issues.

Networking is simple once you get the hang of it. You'll be writing your own packet protocols in no time ;)
This topic is now closed to further replies.
  • Posts

    • My best decision: SHIFT+DELETE WINDOWS Then Installed Fedora Linux. Now I am a Happy Person
    • I value a game as a whole including graphics. But going backwards in terms of graphics with a game I'm not already invested in (as in something I played and loved in the past and still do) is off-putting. Its a very poor demo trailer if they didn't crank the settings to max that would be ridiculous. Different strokes for different folks i guess, i'm not into slop the trailer and likely game could have been far better, it doesn't it instead looks like it was made on a shoe string budget or with lack of experience. But that could also be down to trying to get it running on low budget/outdated hardware and sacrificing the top end.
    • > Our goal is to ensure that the App Store remains an outstanding opportunity for developers and a safe, trusted experience for our users. There are so many scam apps on the platform that it is hard to believe they are truly interested in having a safe, trusted experience for their users.
    • Google announces upgraded Gemini 2.5 Pro model with enhanced capabilities by Pradeep Viswanathan Google today announced an updated Gemini 2.5 model with several improvements that can be observed in popular AI benchmarks. Google particularly highlighted that the new Gemini 2.5 Preview 06-05 "Thinking" model performs better in coding, math, science, and reasoning. Last month, during Google I/O, Google released an updated version of the Gemini 2.5 Pro model with significant improvements. Today's update builds on the release of the previous month with further enhancements. In addition to benchmark improvements, based on user feedback, this updated Gemini 2.5 Pro model also comes with improved style and structure, which will result in creative and better-formatted responses for end users. You can find the benchmark comparison with other leading AI models below. As you can notice in the above table, this updated Gemini 2.5 Pro preview model is now SOTA (State-of-the-Art) in coding benchmarks like Aider Polyglot. It also scored SOTA performance on GPQA and Humanity’s Last Exam (HLE) benchmarks, which test math, science, knowledge, and reasoning capabilities. In real-world testing, this latest 2.5 Pro model scored 24 points better on LMArena, maintaining its lead, and a 35-point jump on WebDevArena at 1443. Developers can access this latest Gemini 2.5 Pro preview model through the Gemini API via Google AI Studio and Vertex AI. General consumers will be able to access this model via the Gemini app. Google also confirmed that the Gemini 2.5 Pro model will be generally available in a couple of weeks, allowing developers to start using it in production-ready enterprise-scale applications.
    • The Bromance is definitely over! 🤣🤣🤣
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      419
    2. 2
      +FloatingFatMan
      182
    3. 3
      snowy owl
      181
    4. 4
      ATLien_0
      176
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!