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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:22:32+00:00 2026-05-30T15:22:32+00:00

I have a TcpClient application that send a message from client to the server.

  • 0

I have a TcpClient application that send a message from client to the server.
Code below:

clsServer.cs

public void server_start()
    {
        this.tcpListener = new TcpListener(new IPAddress(new Byte[] { 172, 16, 1, 55 }), 9999);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));

        _rtb_Input.AppendText("Listening... Press any key to stop" + System.Environment.NewLine);
        this.listenThread.Start();
    }

    private void ListenForClients()
    {
        this.tcpListener.Start();
        while (true)
        {
            //blocks until a client has connected to the server
            client = this.tcpListener.AcceptTcpClient();

            //create a thread to handle communication
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }

    public event MessageReceivedHandler MessageReceived;
    private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
            bytesRead = clientStream.Read(message, 0, 4096);

            //message has successfully been received
            ASCIIEncoding encoder = new ASCIIEncoding();
            string msg = encoder.GetString(message, 0, bytesRead);
            if (this.MessageReceived != null)
            {
                this.MessageReceived(msg);
            }
            //send message back select the message from the db
            byte[] buffer = encoder.GetBytes("Hello Client!");

            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
        }
        tcpClient.Close();
    }

frmClient.cs

public frmClient()
    {
        InitializeComponent();         
    }

    private void btn_ClientStart_Click(object sender, EventArgs e)
    {
        TcpClient client = new TcpClient();

        IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("172.16.1.55"), 9999);
        try
        {
            client.Connect(serverEndPoint);
            NetworkStream clientStream = client.GetStream();

            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes(rtb_Outpot.Text);

            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
        }
        catch (Exception ex)
        {
            rtb_Outpot.AppendText(System.Environment.NewLine + "Failed To Connect to the Server" + System.Environment.NewLine);
        }
    }

frmServer.cs

private void btn_ServerStart_Click(object sender, EventArgs e)
    {
        btn_ServerStop.Enabled = true;
        btn_ServerStart.Enabled = false;
        server = new clsServer(rtb_Input);
        server.MessageReceived += new MessageReceivedHandler(Message_Received);
        server.server_start();
    }

    private void Message_Received(string message)
    {
        //update the display using invoke
        Invoke(new MessageReceivedHandler(PrintToScreen), new object[] { message });
    }

    private void PrintToScreen(string msg)
    {
        msg_counter++;
        rtb_Input.AppendText("msg no': " + msg_counter + System.Environment.NewLine + msg + System.Environment.NewLine);
    }

Works fine, when I send a message from client to the server.
But now I can figure out a way to send data from server back to the client.
Will I have to make a thread for the client form too?

Thanks.

  • 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-30T15:22:34+00:00Added an answer on May 30, 2026 at 3:22 pm

    The code posted looks more like you’re trying to find a way to receive the message sent from the server. The code in clsServer.cs already contains the code necessary to send data to the client.

    Your client code is just missing the necessary calls to read the response from clientStream. How that is implemented in your application will vary depending on your requirements. Personally, I’d probably offload the reading to another thread.

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

Sidebar

Related Questions

I have a project to develop a client server application in java . This
I've created a Client-Server application, and on the Server I want to have the
I have a server application (singleton, simple .NET console application) that talks to a
I have a server and a client application, here's how it's currently implemented: The
I have a client and a server that I'm trying to get to speak
I have wrote a simple client that use TcpClient in dotnet to communicate. In
Helloes good people of Stack Overflow, I have a .NET client-server application running with
I have TCP client application and trying to connect with Server located at remote
I have an application that's listening messages from a modem in some 30 cars.
Hy, I have an application that sends mails with Gmail SMTP server (smtp.gmail.com) using

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.