Question
Write a C# program to create a TCP based console chat room. Accept multiple clients on a TCP port and whenever someone sends a string
Write a C# program to create a TCP based console chat room. Accept multiple clients on a TCP port and whenever someone sends a string with new line relay it to the other clients. The server should ask for a name on first connect and if one isn't provided, to automatically generate one. Server should be a separate file from the client. Type quit to exit out of program. Provide output screen.
Given is what I have so far.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; using System.Threading;
namespace chatRoom { class Program { static void Main(string[] args) { TcpListener myList = new TcpListener(IPAddress.Parse("127.0.0.1"), 88); myList.Start();
Console.WriteLine("The local end point is: " + myList.LocalEndpoint); Console.WriteLine("Waiting for a connection....");
while (true) { Thread t = new Thread(handleConnection); Socket s = myList.AcceptSocket(); t.Start(s);
Server svr = new Server(); Chatroom room = new Chatroom();
svr.OnSocketAccept += room.AddConnection; while(true) { Thread.Sleep(100); } }
}
static void handleConnection(object p1) { Socket s = (Socket)p1; Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100]; int k = s.Receive(b); Console.WriteLine("Received...."); for (int i = 0; i < k; i++) Console.Write(Convert.ToChar(b[i]));
ASCIIEncoding asen = new ASCIIEncoding(); s.Send(asen.GetBytes("")); Console.WriteLine(" Sent Acknowledgement"); s.Close();
}
public class Server { internal int OnSocketAccept;
}
public class Chatroom { internal int AddConnection; } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started