Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

New Trends In Databases And Information Systems Adbis 2019 Short Papers Workshops Bbigap Qauca Sembdm Simpda M2p Madeisd And Doctoral Consortium Bled Slovenia September 8 11 2019 Proceedings

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Robert Wrembel ,Mirjana Ivanovic ,Johann Gamper ,Mikolaj Morzy ,Theodoros Tzouramanis ,Jerome Darmont

1st Edition

ISBN: 3030302776, 978-3030302771

More Books

Students also viewed these Databases questions

Question

Compute the derivative of f(x)cos(-4/5x)

Answered: 1 week ago

Question

Discuss the process involved in selection.

Answered: 1 week ago

Question

Differentiate tan(7x+9x-2.5)

Answered: 1 week ago

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago