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. client/server problem

client/server problem

Scheduled Pinned Locked Moved C#
csharpc++sysadminhelptutorial
5 Posts 3 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.
  • S Offline
    S Offline
    staticv
    wrote on last edited by
    #1

    I have developed a simple client/server app which echoes whatever text the clients sends to the server. Currently, it echoes text only to one client, which has send the text, but I want it to send to all the clients connected. The problem is that when one client writes to the server after that the client waits for receiving and as soon it has received the echo it starts writing to the server, so if some other client sends to the server, and the server echoes back to all the clients, only those will receive the message which are waiting for reading, but none are except the client which has send the message to the server. So how to overcome this? Btw, it is a console application, is it possible to do in it? I guess I explained it correctly. If not, then please ask again @Mustufa: Sorry if you got annoyed of the earlier posts, I was confused converting C++/CLI to C# and posted the wrong code. The code is For server

    // ServerApp.cpp : main project file.

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using System.Threading;
    using System.Collections;
    using System.Text;
    public static class GlobalMembers
    {

    public const int ECHO\_PORT = 8080;
    
    internal static void Main()
    {
        try
        {
            // Bind the server to the local port
            TcpListener clientListener = new TcpListener(ECHO\_PORT);
    
            // Start to listen
            clientListener.Start();
    
            Console.WriteLine("Waiting for connections...");
    
            while (true)
            {
                // Accept the connection
                TcpClient client = clientListener.AcceptTcpClient();
    
                ClientHandler cHandler = new ClientHandler();
    
                cHandler.clientSocket = client;
    
                // Create a new thread for the client
                Thread clientThread = new Thread(new ThreadStart(cHandler.RunClient));
                clientThread.Start();
            }
    
            clientListener.Stop();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: " + e);
        }
    }
    

    }

    public class ClientHandler
    {
    public TcpClient clientSocket;
    public static Hashtable users = new Hashtable(30);

    public void RunClient()
    {
        // Create the stream classes
        StreamReader readerStream = new StreamReader(clientSocket.GetStream());
        NetworkStream writerStream = clientSocket.GetStream();
    
        st
    
    N 1 Reply Last reply
    0
    • S staticv

      I have developed a simple client/server app which echoes whatever text the clients sends to the server. Currently, it echoes text only to one client, which has send the text, but I want it to send to all the clients connected. The problem is that when one client writes to the server after that the client waits for receiving and as soon it has received the echo it starts writing to the server, so if some other client sends to the server, and the server echoes back to all the clients, only those will receive the message which are waiting for reading, but none are except the client which has send the message to the server. So how to overcome this? Btw, it is a console application, is it possible to do in it? I guess I explained it correctly. If not, then please ask again @Mustufa: Sorry if you got annoyed of the earlier posts, I was confused converting C++/CLI to C# and posted the wrong code. The code is For server

      // ServerApp.cpp : main project file.

      using System;
      using System.Net;
      using System.Net.Sockets;
      using System.IO;
      using System.Threading;
      using System.Collections;
      using System.Text;
      public static class GlobalMembers
      {

      public const int ECHO\_PORT = 8080;
      
      internal static void Main()
      {
          try
          {
              // Bind the server to the local port
              TcpListener clientListener = new TcpListener(ECHO\_PORT);
      
              // Start to listen
              clientListener.Start();
      
              Console.WriteLine("Waiting for connections...");
      
              while (true)
              {
                  // Accept the connection
                  TcpClient client = clientListener.AcceptTcpClient();
      
                  ClientHandler cHandler = new ClientHandler();
      
                  cHandler.clientSocket = client;
      
                  // Create a new thread for the client
                  Thread clientThread = new Thread(new ThreadStart(cHandler.RunClient));
                  clientThread.Start();
              }
      
              clientListener.Stop();
          }
          catch (Exception e)
          {
              Console.WriteLine("Exception: " + e);
          }
      }
      

      }

      public class ClientHandler
      {
      public TcpClient clientSocket;
      public static Hashtable users = new Hashtable(30);

      public void RunClient()
      {
          // Create the stream classes
          StreamReader readerStream = new StreamReader(clientSocket.GetStream());
          NetworkStream writerStream = clientSocket.GetStream();
      
          st
      
      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Ahmed Manzoor wrote:

      so if some other client sends to the server, and the server echoes back to all the clients, only those will receive the message which are waiting for reading, but none are except the client which has send the message to the server.

      Client should run two threads, one for writing text to server and one for reading the text. So client will be always ready to read.

      Navaneeth How to use google | Ask smart questions

      S 1 Reply Last reply
      0
      • N N a v a n e e t h

        Ahmed Manzoor wrote:

        so if some other client sends to the server, and the server echoes back to all the clients, only those will receive the message which are waiting for reading, but none are except the client which has send the message to the server.

        Client should run two threads, one for writing text to server and one for reading the text. So client will be always ready to read.

        Navaneeth How to use google | Ask smart questions

        S Offline
        S Offline
        staticv
        wrote on last edited by
        #3

        Well if it runs two threads, and on the write thread it is waiting for the Console::ReadLine() At the same time the read thread prints a line, where will it be printed? What will happen to the Console::ReadLine() func in the write thread? will it be abandoned?

        Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

        N J 2 Replies Last reply
        0
        • S staticv

          Well if it runs two threads, and on the write thread it is waiting for the Console::ReadLine() At the same time the read thread prints a line, where will it be printed? What will happen to the Console::ReadLine() func in the write thread? will it be abandoned?

          Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Ahmed Manzoor wrote:

          What will happen to the Console::ReadLine() func in the write thread? will it be abandoned?

          No. It can still read the input.

          Navaneeth How to use google | Ask smart questions

          1 Reply Last reply
          0
          • S staticv

            Well if it runs two threads, and on the write thread it is waiting for the Console::ReadLine() At the same time the read thread prints a line, where will it be printed? What will happen to the Console::ReadLine() func in the write thread? will it be abandoned?

            Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Ahmed Manzoor wrote:

            Well if it runs two threads, and on the write thread it is waiting for the Console::ReadLine() At the same time the read thread prints a line, where will it be printed?

            The problem you have here is that the standard console only has one "area" of I/O, namely the next "line" of the console. Why dont you try making your client a windows forms app, and then you can have a multiline textbox for the chat messages, and a textbox with button for the user to enter his message to send. Or, you could open 2 console windows from your client, one to send allo messages to and one to capture user's message.

            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