Click here to Skip to main content
15,924,317 members
Home / Discussions / C#
   

C#

 
QuestionCrystal Report using a parameterized Stored Procedure Pin
The_Collector27-Jan-09 1:30
The_Collector27-Jan-09 1:30 
QuestionDownload from RapidShare (premium account) Pin
Bartosz Kita27-Jan-09 0:48
Bartosz Kita27-Jan-09 0:48 
QuestionToolTip with Close button Pin
San27-Jan-09 0:47
San27-Jan-09 0:47 
AnswerRe: ToolTip with Close button Pin
N a v a n e e t h27-Jan-09 0:56
N a v a n e e t h27-Jan-09 0:56 
GeneralRe: ToolTip with Close button Pin
San27-Jan-09 0:59
San27-Jan-09 0:59 
GeneralRe: ToolTip with Close button Pin
N a v a n e e t h27-Jan-09 1:15
N a v a n e e t h27-Jan-09 1:15 
GeneralRe: ToolTip with Close button Pin
MadArtSoft27-Jan-09 1:34
MadArtSoft27-Jan-09 1:34 
Questionclient/server problem Pin
staticv27-Jan-09 0:38
staticv27-Jan-09 0:38 
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();

        string returnData = readerStream.ReadLine();
        string userName = returnData;

        // Add a user
        users.Add(userName, clientSocket);

        Console.WriteLine("Welcome " + userName + " to the Server");

        while (true)
        {
            returnData = readerStream.ReadLine();
            string tempData = returnData.ToLower();

            if (tempData.IndexOf("quit") > -1)
            {
                Console.WriteLine("Bye Bye " + userName);
                break;
            }

            Console.WriteLine(userName + ": " + returnData);
            returnData += "\r\n";

            Byte[] dataWrite = Encoding.ASCII.GetBytes(returnData);

            TcpClient[] tcpClients = new TcpClient[users.Count];
            users.Values.CopyTo(tcpClients, 0);
            for (int i = 0; i < tcpClients.Length; i++)
            {
                NetworkStream tempWriteStream = tcpClients[i].GetStream();
                tempWriteStream.Write(dataWrite, 0, dataWrite.Length);
            }
        }

        clientSocket.Close();
    }

}


I store every connected client and then send data to it, but at that time is waiting for input, that is, it is waiting for the Console::ReadLine() func.

For client
using System; 
using System.Net; 
using System.IO; 
using System.Net.Sockets; 
using System.Text; 

public class EchoClient 
{ 
   const int ECHO_PORT = 8080; 

   public static void Main(string [] arg) 
   { 
      Console.Write("Your UserName:"); 
      string userName = Console.ReadLine(); 
      Console.WriteLine("-----Logged In----->"); 

      try 
      { 

         // Create a connection with the ChatServer 
         TcpClient eClient = new TcpClient("127.0.0.1", ECHO_PORT); 

         // Create the stream classes 
         StreamReader readerStream = new StreamReader(eClient.GetStream()); 
         NetworkStream writerStream = eClient.GetStream(); 

         string dataToSend; 

         dataToSend = userName; 
         dataToSend += "\r\n"; 

         // Send username to the server 
         byte[] data = Encoding.ASCII.GetBytes(dataToSend); 

         writerStream.Write(data,0,data.Length); 

         while(true) 
         { 
            Console.Write(userName + ":"); 

            // Read line from server 
            dataToSend = Console.ReadLine(); 
            dataToSend += "\r\n"; 

            data = Encoding.ASCII.GetBytes(dataToSend); 
            writerStream.Write(data, 0, data.Length); 

            // If QUIT is sent, then quit application 
            if (dataToSend.IndexOf("QUIT") > -1) 
               break; 

            string returnData; 

            // Receive response from server 
            returnData = readerStream.ReadLine(); 

            Console.WriteLine("Server: " + returnData); 
         } 

         // Close TcpClient 
         eClient.Close(); 
      } 
      catch(Exception exp) 
      { 
         Console.WriteLine("Exception: " + exp); 
      } 
   } 
} 


Top Web Hosting Providers[^]

Do, or do not. There is no 'try'.

AnswerRe: client/server problem Pin
N a v a n e e t h27-Jan-09 0:45
N a v a n e e t h27-Jan-09 0:45 
GeneralRe: client/server problem Pin
staticv27-Jan-09 0:53
staticv27-Jan-09 0:53 
GeneralRe: client/server problem Pin
N a v a n e e t h27-Jan-09 1:13
N a v a n e e t h27-Jan-09 1:13 
GeneralRe: client/server problem Pin
J4amieC27-Jan-09 2:30
J4amieC27-Jan-09 2:30 
QuestionAdding bitmap icon for custom toolbox component Pin
kanchoette27-Jan-09 0:31
kanchoette27-Jan-09 0:31 
AnswerRe: Adding bitmap icon for custom toolbox component Pin
DaveyM6927-Jan-09 0:57
professionalDaveyM6927-Jan-09 0:57 
AnswerRe: Adding bitmap icon for custom toolbox component Pin
Mirko198027-Jan-09 1:01
Mirko198027-Jan-09 1:01 
GeneralRe: Adding bitmap icon for custom toolbox component Pin
kanchoette27-Jan-09 1:34
kanchoette27-Jan-09 1:34 
AnswerRe: Adding bitmap icon for custom toolbox component Pin
MadArtSoft27-Jan-09 1:38
MadArtSoft27-Jan-09 1:38 
QuestionHow to disable treeview child node in C# winforms Pin
Member 183949627-Jan-09 0:24
Member 183949627-Jan-09 0:24 
AnswerRe: How to disable treeview child node in C# winforms Pin
N a v a n e e t h27-Jan-09 0:48
N a v a n e e t h27-Jan-09 0:48 
Questionadding columns to grid view programatically Pin
scotchy2hotty2k227-Jan-09 0:00
scotchy2hotty2k227-Jan-09 0:00 
AnswerRe: adding columns to grid view programatically Pin
MadArtSoft27-Jan-09 1:42
MadArtSoft27-Jan-09 1:42 
QuestionXml writing Pin
Naveed72726-Jan-09 23:39
Naveed72726-Jan-09 23:39 
AnswerRe: Xml writing Pin
SeMartens26-Jan-09 23:47
SeMartens26-Jan-09 23:47 
GeneralRe: Xml writing Pin
Naveed72726-Jan-09 23:49
Naveed72726-Jan-09 23:49 
AnswerRe: Xml writing Pin
benjymous26-Jan-09 23:50
benjymous26-Jan-09 23:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.