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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:55:52+00:00 2026-06-15T18:55:52+00:00

C# client can not write message to server, I think the parsing byte to

  • 0

C# client can not write message to server, I think the parsing byte to string is bad. I connect in client class with the connect method via ipaddress and port. However I start the compile the server don’t change, but client will turn off.

Here is my Server:

class Server
{
    private TcpListener tcpListener;
    private Thread listenThread;

    public Server()
    {
        this.tcpListener = new TcpListener(IPAddress.Parse("10.0.0.44"), 3000);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
    }

    private void ListenForClients()
    {
        this.tcpListener.Start();

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

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

    private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        TcpClient sendClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

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

        while (true)
        {
            bytesRead = 0;

            try
            {
                //blocks until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                //a socket error has occured
                break;
            }

            if (bytesRead == 0)
            {
                //the client has disconnected from the server
                break;
            }

            //message has successfully been received
            ASCIIEncoding encoder = new ASCIIEncoding();
            System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));

            Console.WriteLine("To: " + tcpClient.Client.LocalEndPoint);
            Console.WriteLine("From: " + tcpClient.Client.RemoteEndPoint);
            Console.WriteLine(encoder.GetString(message, 0, bytesRead));
            clientStream.Flush();
            sendToClient("Test back", sendClient);
        }

        tcpClient.Close();
    }

    public void sendToClient(String line, TcpClient client)
    {
        try
        {
            NetworkStream stream = client.GetStream();
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes(line + "\0");

            stream.Write(buffer, 0, buffer.Length);
            stream.Flush();
        }
        catch
        {
            Console.WriteLine("Client Disconnected." + Environment.NewLine);

        }
    }

    public void broadcast(String line, TcpClient thisClient)
    {
        sendToClient(line, thisClient);
    }

    static void Main(string[] args)
    {
        new Server();
    }
}

Here is my client:

public class Client
{
    TcpClient _tcpClient;

    // Connects the client to a server at the specified
    // IP address and port.
    public void Connect(IPAddress address, int port)
    {
        _tcpClient = new TcpClient();

        IPEndPoint serverEndPoint = new IPEndPoint(address, port);

        _tcpClient.Connect(serverEndPoint);

        // Create a thread to read data sent from the server.
        ThreadPool.QueueUserWorkItem(
           delegate
           {
               Read();
           });
    }

    // Sends bytes to the server.
    public void Send(byte[] buffer)
    {
        _tcpClient.GetStream().Write(
           buffer, 0, buffer.Length);

        _tcpClient.GetStream().Flush();
    }

    public void Read()
    {
        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] buffer = new byte[4096];
        int bytesRead;
        while (true)
        {
            bytesRead = _tcpClient.GetStream().Read(buffer, 0, 4096);
            Console.WriteLine(encoder.GetString(buffer, 0, bytesRead));
        }
    }

    public void Dispose()
    {
        _tcpClient.Close();
    }

    public static void Main(string[] args)
    {
        Client c = new Client();
        c.Connect(IPAddress.Parse("10.0.0.44"), 3000);
    }
}
  • 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-06-15T18:55:53+00:00Added an answer on June 15, 2026 at 6:55 pm

    Deadlock

    your client is not sending the data and the server is also not able to send any data since it is getting blocked at

    bytesRead = clientStream.Read(message, 0, 4096);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I use TFS Client 2010 with TFS server 2008? Or it is not
With my code I can read a message on the server and write from
I know on client side (javascript) you can use windows.location.hash but could not find
I am trying to write a client/server program in C for a homework assignment.
I have the following web server class found here . I need to write
I want to send a message from the server to the client and found
I have been asked to write a class that connects to a server, asynchronously
I am making a prototype client & server so that I can understand how
I'm using the xmpppy library to write an XMPP client that can chat with
I am going to write a client/server card game for learning/practice purposes, and intend

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.