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

  • Home
  • SEARCH
  • 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 238051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:28:29+00:00 2026-05-11T20:28:29+00:00

I am trying to conceptually work through a model for a client-server socket application

  • 0

I am trying to conceptually work through a model for a client-server socket application I am writing in c# (both client and server). My server will need to handle many clients at once, and preferably multiple requests from a client at once. I have worked out a container for my communication where I will send a fixed length header at the beginning of each message which will contain (among other things) the length of the message. I have some experience with socket programming in c#, so I am comfortable using Asynchronous sockets.

The main thing I am having trouble with, conceptually, is I need both the client and server to be able to receive messages at any time. The client will establish a connection, and remain “logged in” (like an IM client), and it will need to both receive data at arbitrary times and make requests at arbitrary times. I had also wanted, as part of my protocol, to receive a response to each request made (be it from the server to client, or client to server).

I would like to be able to use a single socket if possible. I believe I could make this work using two sockets, one for making server->client requests and one for client->server requests, but I don’t want the extra complications of dealing with two ports/connections. However, using a single socket, I am not sure how to manage sending requests and getting responses when they could be interleaved.

I cannot find any examples of a similar server or client in my searching. Thanks to anyone who offers any ides.

  • 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-11T20:28:29+00:00Added an answer on May 11, 2026 at 8:28 pm

    If you’re using TCP, then you will necessarily have to have one socket per client on the server side, as well as the socket to listen for connections. For UDP you can do this all with one socket. Regardless, do the following for either your one UDP socket, or for each TCP client socket:

    Have a BeginReceive going at all times and BeginWrite when you the necessary event occurs (ie the user presses a button or a message came in that you need to do something about).

    EDIT: In response to your comment, the way I’d handle it would be to include a request ID in your header. Whenever sending a request (from either end) include a unique value for that message. Use a Dictionary<RequestId, RequestState> (with appropriate substitutions for the types) and see if an incoming message relates to a pre-existing request. Furthermore, you could specify that all IDs with the high bit set are requests originating at the client and IDs with the high bit cleared are from the server to avoid collisions:

    Server                      Client
    Request 0x00 -------------> Starts processing
    
    Starts processing <-------  (unrelated) Request 0x80
    
    Request 0x00 complete <---- Sends response to 0x00
    
    Sends response to 0x80 ---> Request 0x80 complete
    

    This is the system that AOL’s OSCAR protocol uses for (possibly slow) stateful requests.

    EDIT2: Hm, ok… you really want to block on it? Something you could do is have a separate thread handle the Send/Receive calls. This thread would communicate with the main thread via a thread-safe “send message” queue and a similar “message received” psuedo-queue. The reason I call the latter a psuedo-queue is that you would want the ability to take messages out of order from the queue. The main thread would put a message on the send queue, then block on the receive queue. Each time the socket thread updates the receive queue, the main thread would wake up and check if the message it wants is there yet. If it is, it would take it (out of order) and finish the desired operation. If the message is not there yet, it’d just block on the queue again. When the operation is finally completed it’d just resume normal operation and get messages in order from the receive queue and process them or do whatever else it does.

    Does that make any sense? I’m sorry if it’s kinda confusing…

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

Sidebar

Ask A Question

Stats

  • Questions 115k
  • Answers 115k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Explicit interface implementation does not let you specify any access… May 11, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer Here's a Channel9 thread that has some answers. May 11, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer The second version is the best for me, it avoids… May 11, 2026 at 10:23 pm

Related Questions

I am looking for a regex statement that will let me extract the HTML
Currently my team uses Visual Sourcesafe, and being very familiar with how horrible the
I'm toying with the idea of writing a physics simulation software in which each
So I've tried to work with the pure-CSS, never-use-tables-for-layout gospel, I really have. But
recently I have been encountering problems with exposing events from .NET to COM. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.