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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:01:23+00:00 2026-05-27T07:01:23+00:00

I’m trying to implement a client server app which a Server program has a

  • 0

I’m trying to implement a client server app which a
Server program has a counter which is incremented by one upon request(No counter value provided to a client can be duplicated)
Client program asks the server program to increment the counter and get the incremented value
Server program should be capable of handling multiple clients(at least 5) at a time
The counter value should be shared by all the server threads if any

I have implemented the server and the client apps in seperate projects.When i start running the server and then the client the server programs following line gives a NullReferanceException.

NetworkStream netstream = ((TcpClient)client).GetStream();

I’m new in C#.I want to know how the client can connect to the server and get the incremented value.
I have attached my code here.
Thanks in advance.

MyServer.cs

public abstract class MyServer
    {

        private static int port = 8080;
        private static TcpListener listener;
        private static Thread thread;
        private static int clientId = 0;

        static void Main(string[] args)
        {

            listener = new TcpListener(new IPAddress(new byte[] {127,0,0,1}),port);
            thread = new Thread(new ThreadStart(Listen));
            thread.Start();
        }


        private static void Listen()
        {
            listener.Start();
            Console.WriteLine("Listening on: " + port.ToString());

            while(true)
            {
                Console.WriteLine("Waiting for connection....");
                Console.WriteLine("Client No: " + clientId);
                TcpClient client = listener.AcceptTcpClient();
                Thread listenThread = new Thread(new ParameterizedThreadStart(ListenThread));
                listenThread.Start();
            }
        }

        private static void ListenThread(Object client)
        {

                NetworkStream netstream = ((TcpClient)client).GetStream();
                Console.WriteLine("Request made");
                clientId = clientId + 1;
                // String message = "Hello world";
                byte[] resMessage = Encoding.ASCII.GetBytes(clientId.ToString());
                netstream.Write(resMessage, 0, resMessage.Length);
                netstream.Close();


        }
    }

MyClient.cs

 class MyClient
    {
        static void Main(string[] args)
        {
            TcpClient tcpClient;
            int port = 8080;
            NetworkStream stream = null;

            tcpClient = new TcpClient("127.0.0.1", port);
            Console.WriteLine("Connection was established....");

            stream = tcpClient.GetStream();

            Byte[] response = new Byte[tcpClient.ReceiveBufferSize];
            stream.Read(response,0,(int)tcpClient.ReceiveBufferSize);

            String returnData = Encoding.UTF8.GetString(response);
            Console.WriteLine("Server Response " + returnData);

            tcpClient.Close();
            stream.Close();

        }
    }
  • 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-27T07:01:24+00:00Added an answer on May 27, 2026 at 7:01 am

    You’re starting the thread with no arguments while it expects one (the TcpStream of the client). Change this:

    listenThread.Start();
    

    to this:

    listenThread.Start(client);
    

    By the way, I’d call that thread clientThread rather than listenThread; the thread isn’t listening, it’s attending to the client.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.