Hello I'm trying to build a chat using BinaryReader/BinaryWriter, I came into a dead end where I can't figure out how do I make my server send the message to all connected clients..
I have tried adding all clients to a list and running foreach loop on the list to send the message to every connected client, that didn't workout..
Server:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace server {
internal class Program {
public static int counter = 0;
//List<Client> clientList = new List <Client>();
private static readonly IPAddress sr_ipAddress = IPAddress.Parse("127.0.0.1");
public TcpListener Listener = new TcpListener(sr_ipAddress, 8888);
public static void Main(string[] args) {
Program server = new Program();
server.Start();
Console.ReadKey();
}
public void Start() {
Listener.Start();
Console.WriteLine("Server started");
StartAccept();
}
private void StartAccept() {
Listener.BeginAcceptTcpClient(HandleAsyncConnection, Listener);
}
public void HandleAsyncConnection(IAsyncResult res) {
StartAccept(); //listen for new connections again
TcpClient clientSocket = Listener.EndAcceptTcpClient(res);
Client client = new Client(clientSocket);
client.StartClient();
}
}
internal class Client {
public TcpClient ClientSocket;
public string CleintName{get; set;}
public Client(TcpClient inClientSocket) {
ClientSocket = inClientSocket;
NetworkStream netStream = ClientSocket.GetStream();
BinaryReader Listen = new BinaryReader(netStream);
CleintName = Listen.ReadString();
}
public void StartClient() {
Thread clientThread = new Thread(Chat);
clientThread.Start();
Console.WriteLine("New Client connected {0} => {1}", ClientSocket.GetHashCode(), CleintName);
}
private string _message;
//private string _serverMessage;
public void Chat() {
NetworkStream netStream = ClientSocket.GetStream();
BinaryReader listen = new BinaryReader(netStream);
BinaryWriter send = new BinaryWriter(netStream);
while (true) {
//listening int1
try {
_message = listen.ReadString();
Console.WriteLine("{0} :{1}", CleintName, _message);
send.Write(CleintName + ": " + _message);
//Send.Write(CleintName + " :" + _message);
}
catch (Exception ex) {
listen.Close();
send.Close();
netStream.Close();
Console.WriteLine(ex.Message);
return;
}
}
}
}
}
Client:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace tcpClient {
internal class Program {
private static readonly IPAddress s_ipAddress = IPAddress.Parse("127.0.0.1");
private static readonly TcpClient s_client = new TcpClient();
private static void Main(string[] args) {
//Console.WriteLine("Press Enter to start");
//Console.ReadLine();
try {
s_client.Connect(s_ipAddress, 8888);
}
catch (Exception ex) {
Console.WriteLine("{0}", ex.Message);
Console.ReadKey();
return;
}
NetworkStream netStream = s_client.GetStream();
BinaryReader listen = new BinaryReader(netStream);
BinaryWriter send = new BinaryWriter(netStream);
Console.WriteLine("Connected");
Console.Write("Enter name: ");
string name = Console.ReadLine();
send.Write(name);
Console.WriteLine("Chat started");
while (true) {
var message = Console.ReadLine();
send.Write(message);
//Console.WriteLine("{0}: {1}", name, message);
Console.WriteLine(listen.ReadString());
}
}
}
}