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