Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need Help in multithread C# socket programming!!!

Need Help in multithread C# socket programming!!!

Scheduled Pinned Locked Moved C#
csharpsysadminhelp
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yum 2010
    wrote on last edited by
    #1

    Hi everyone! when i run the programs of server and client, they both run and also show the acknowledgement like >>Server Started! >>Client No:1 started! >>From client-1Message from Client >>Server to client<1>1 and so on for client 2, 3 ... but i want communication like if i type some text and then it display that text!!! I am using visual 2010... multithread Server program is

    using System;
    using System.Threading;
    using System.Net.Sockets;
    using System.Text;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    TcpListener serverSocket = new TcpListener(8888);
    TcpClient clientSocket = default(TcpClient);
    int counter = 0;

    serverSocket.Start();
    Console.WriteLine(" >> " + "Server Started");

    counter = 0;
    while (true)
    {
    counter += 1;
    clientSocket = serverSocket.AcceptTcpClient();
    Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
    handleClinet client = new handleClinet();
    client.startClient(clientSocket, Convert.ToString(counter));
    }

    clientSocket.Close();
    serverSocket.Stop();
    Console.WriteLine(" >> " + "exit");
    Console.ReadLine();
    }
    }

    //Class to handle each client request separatly
    public class handleClinet
    {
    TcpClient clientSocket;
    string clNo;
    public void startClient(TcpClient inClientSocket, string clineNo)
    {
    this.clientSocket = inClientSocket;
    this.clNo = clineNo;
    Thread ctThread = new Thread(doChat);
    ctThread.Start();
    }
    private void doChat()
    {
    int requestCount = 0;
    byte[] bytesFrom = new byte[10025];
    string dataFromClient = null;
    Byte[] sendBytes = null;
    string serverResponse = null;
    string rCount = null;
    requestCount = 0;

    while ((true))
    {
    try
    {
    requestCount = requestCount + 1;
    NetworkStream networkStream = clientSocket.GetStream();
    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
    Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);

    rCount = Convert.ToString(requestCount);
    serverResponse = "Server to clinet(" + clNo + ") " + rCount;
    sendBytes = Encoding.ASCII.GetBytes(serverResponse);
    networkStream.Write(sendBytes, 0, sendBytes.Length);
    networkStream.Flush();
    Console.WriteLine(" >> " + serverResponse);
    }
    catch (Exception ex)
    {
    Console.WriteLine(" >> " + ex.ToString());
    }
    }
    }
    }
    }

    the Clie

    L 1 Reply Last reply
    0
    • Y yum 2010

      Hi everyone! when i run the programs of server and client, they both run and also show the acknowledgement like >>Server Started! >>Client No:1 started! >>From client-1Message from Client >>Server to client<1>1 and so on for client 2, 3 ... but i want communication like if i type some text and then it display that text!!! I am using visual 2010... multithread Server program is

      using System;
      using System.Threading;
      using System.Net.Sockets;
      using System.Text;

      namespace ConsoleApplication1
      {
      class Program
      {
      static void Main(string[] args)
      {
      TcpListener serverSocket = new TcpListener(8888);
      TcpClient clientSocket = default(TcpClient);
      int counter = 0;

      serverSocket.Start();
      Console.WriteLine(" >> " + "Server Started");

      counter = 0;
      while (true)
      {
      counter += 1;
      clientSocket = serverSocket.AcceptTcpClient();
      Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
      handleClinet client = new handleClinet();
      client.startClient(clientSocket, Convert.ToString(counter));
      }

      clientSocket.Close();
      serverSocket.Stop();
      Console.WriteLine(" >> " + "exit");
      Console.ReadLine();
      }
      }

      //Class to handle each client request separatly
      public class handleClinet
      {
      TcpClient clientSocket;
      string clNo;
      public void startClient(TcpClient inClientSocket, string clineNo)
      {
      this.clientSocket = inClientSocket;
      this.clNo = clineNo;
      Thread ctThread = new Thread(doChat);
      ctThread.Start();
      }
      private void doChat()
      {
      int requestCount = 0;
      byte[] bytesFrom = new byte[10025];
      string dataFromClient = null;
      Byte[] sendBytes = null;
      string serverResponse = null;
      string rCount = null;
      requestCount = 0;

      while ((true))
      {
      try
      {
      requestCount = requestCount + 1;
      NetworkStream networkStream = clientSocket.GetStream();
      networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
      dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
      dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
      Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);

      rCount = Convert.ToString(requestCount);
      serverResponse = "Server to clinet(" + clNo + ") " + rCount;
      sendBytes = Encoding.ASCII.GetBytes(serverResponse);
      networkStream.Write(sendBytes, 0, sendBytes.Length);
      networkStream.Flush();
      Console.WriteLine(" >> " + serverResponse);
      }
      catch (Exception ex)
      {
      Console.WriteLine(" >> " + ex.ToString());
      }
      }
      }
      }
      }

      the Clie

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      yum 2010 wrote:

      but i want communication like if i type some text and then it display that text!!!

      You mean when you press "A", that the other side immediately receives that character and displays it?

      yum 2010 wrote:

      Plzzzzz guide me

      Guiding someone trough a forest is a level beyond pointing the right direction :)

      I are Troll :suss:

      Y 1 Reply Last reply
      0
      • L Lost User

        yum 2010 wrote:

        but i want communication like if i type some text and then it display that text!!!

        You mean when you press "A", that the other side immediately receives that character and displays it?

        yum 2010 wrote:

        Plzzzzz guide me

        Guiding someone trough a forest is a level beyond pointing the right direction :)

        I are Troll :suss:

        Y Offline
        Y Offline
        yum 2010
        wrote on last edited by
        #3

        Eddy Vluggen wrote:

        You mean when you press "A", that the other side immediately receives that character and displays it?

        yeah!!

        L 1 Reply Last reply
        0
        • Y yum 2010

          Eddy Vluggen wrote:

          You mean when you press "A", that the other side immediately receives that character and displays it?

          yeah!!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Then send a message whenever a key is pressed :) You're already sending messages, I guess you can adapt it to send a message on every keystroke.

          I are Troll :suss:

          Y 1 Reply Last reply
          0
          • L Lost User

            Then send a message whenever a key is pressed :) You're already sending messages, I guess you can adapt it to send a message on every keystroke.

            I are Troll :suss:

            Y Offline
            Y Offline
            yum 2010
            wrote on last edited by
            #5

            Eddy Vluggen wrote:

            I guess you can adapt it to send a message on every keystroke.

            hey can yu guide me that how can i do this?? :(

            L 1 Reply Last reply
            0
            • Y yum 2010

              Eddy Vluggen wrote:

              I guess you can adapt it to send a message on every keystroke.

              hey can yu guide me that how can i do this?? :(

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              yum 2010 wrote:

              hey can yu guide me that how can i do this?? :(

              I can't. The code that you posted is a basic client/server example. I suggest you search for multiple chat-applications and study them.

              I are Troll :suss:

              Y 1 Reply Last reply
              0
              • L Lost User

                yum 2010 wrote:

                hey can yu guide me that how can i do this?? :(

                I can't. The code that you posted is a basic client/server example. I suggest you search for multiple chat-applications and study them.

                I are Troll :suss:

                Y Offline
                Y Offline
                yum 2010
                wrote on last edited by
                #7

                Eddy Vluggen wrote:

                I can't. The code that you posted is a basic client/server example. I suggest you search for multiple chat-applications and study them.

                okay!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups