Note use JAVA Write a server program using TCP that accepts
Note : use JAVA
Write a server program using TCP that accepts requests from clients to capitalize strings sent by the clients. When a client connects, a new thread is started to handle an interactive dialog in which the client sends a string and the server sends back the capitalized version of the string. Set a maximum limit of 10 strings to accept from a client. The server capitalizes each string and sends it back to the client with a prompt for the next string. You may choose to terminate the connection before the maximum limit of 10 strings is reached. Send an appropriate message to the server in that case when it prompts for next string. After the maximum limit of 10 strings is reached, your program should close the connection automatically and display the message \"Connection Closed.\" and terminate execution. You should write the corresponding client program as well.Solution
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
public class serv
{
public static void Main()
{
try
{
IPAddress ipAd = IPAddress.Parse(\"172.21.5.99\"); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine(\"The server is running at port 8001...\");
Console.WriteLine(\"The local End point is :\" + myList.LocalEndpoint);
Console.WriteLine(\"Waiting for a connection.....\");
Socket s = myList.AcceptSocket();
Console.WriteLine(\"Connection accepted from \" + s.RemoteEndPoint);
int numberOfStringAccepted = 0;
while (numberOfStringAccepted <= 10)
{
byte[] b = new byte[100];
int k = s.Receive(b);
string receivedString = string.Empty;
numberOfStringAccepted = numberOfStringAccepted + 1;
for (int i = 0; i < k; i++)
receivedString = receivedString + Convert.ToChar(b[i]);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(receivedString.ToUpper()));
/* clean up */
s.Close();
}
myList.Stop();
}
catch (Exception e)
{
Console.WriteLine(\"Error..... \" + e.StackTrace);
}
}
}

