Write a simple TCP program for a server that accepts lines of input from a clien
ID: 647641 • Letter: W
Question
Write a simple TCP program for a server that accepts lines of input from a client and prints the lines onto theservers standard output. You can do this by modifying the TCPServer.java program in the text. Compile and execute your program. On any other machine that contains aWeb browser, set the proxy server in the browser to the host that is runningyour server program; also configure the port number appropriately. Your browser should now send its GET request messages to your server, and your server should display the messages on its standard output. Use this platform to determine whether your browser generates conditional GET messages for objects that arelocally caches.
Here's the TCPServer.java file
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + ' ';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Explanation / Answer
import java.io.*;
import java.net.*;
class ClientProgram {
public static void main(String argv[]) throws Exception
{
String input;
String modifiedinput;
Socket clientSocket = new Socket("hostname", 6789);
BufferedReader UserInput = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream ServerOutput = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader ServerInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
input = UserInput.readLine();
ServerOutput.writeBytes(input + ' ');
modifiedinput = ServerInput.readLine();
System.out.println("FROM SERVER: " + modifiedinput);
clientSocket.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.