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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!