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 443187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:08:44+00:00 2026-05-12T21:08:44+00:00

I have a simple server that waits for a client to connect, reads the

  • 0

I have a simple server that waits for a client to connect, reads the incoming stream, and sends a message back. What I would like to do is have every connection handled by a separate thread. This is my first time working with sockets and threads in C#, and most of the examples I have found are really confusing, so any help or simple examples would be much appreciated.

Here is what I have right now.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;

    [STAThread]
    static void Main(string[] args)
    {
        TestServer ts = new TestServer();
         ts.Start();
    }

class TestServer
{
    private readonly int port = 48888;
    private readonly IPAddress ip = IPAddress.Parse("127.0.0.1");

    private TcpListener listener;

    public TestServer()
    {
       this.listener = new TcpListener(this.ip, this.port);
    }

    public void Start()
    {
        this.listener.Start();
        Console.WriteLine("Server Running...");

        Socket s;
        Byte[] incomingBuffer;
        int bytesRead;
        Byte[] Message;

        while (true)
        {
            s = this.listener.AcceptSocket();

            incomingBuffer = new Byte[100];
            bytesRead = s.Receive(incomingBuffer);
            string message = "Hello from the server";
            Message = Encoding.ASCII.GetBytes(message.ToCharArray());

            s.Send(Message);
        }
    }
}
  • 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-12T21:08:44+00:00Added an answer on May 12, 2026 at 9:08 pm

    Try this:

    public void Start()
    {
        this.listener.Start();
        Console.WriteLine("Server running...");
    
        while (true)
        {
            Socket s = this.listener.AcceptSocket();
            ThreadPool.QueueUserWorkItem(this.WorkMethod, s);
        }
    }
    
    private void WorkMethod(object state)
    {
        using (Socket s = (Socket)state)
        {
            byte[] buffer = new byte[100];
            int count = s.Receive(buffer);
            string message = "Hello from the server";
            byte[] response = Encoding.ASCII.GetBytes(message);
            s.Send(response);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple Java RMI Server that looks like the following: import
I have a simple php script on a server that's using fsockopen to connect
We have the following simple Stored Procedure that runs as an overnight SQL server
I am creating a simple Unix server written in C++ that simply waits for
I have a very simple TCP server written in C. It runs indefinitely, waiting
Let's say I'm running a simple server and have accept() ed a connection from
Ok, simple problem hopefully. I have started to make a simple server for a
I have a simple problem when querying the SQL Server 2005 database. I have
I have written a secure TCP server in .NET. This was basically as simple
I have a simple scenario, where two servers are connected through a gigabit link.

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.