Can someone help with my java program assignment? I finished writing the first p
ID: 3692268 • Letter: C
Question
Can someone help with my java program assignment? I finished writing the first part of the assignment (the bold words). I need help writing the second part of the assignment. (not the bold words). I would be really appreciate if someone can help me with this assignment.
-----------------------------------------------------------------------------------------------------------------
Write a program that connects to TCP port 80 on server home.adelphi.edu. Upon connection, send the following output from your program back to the server:
Note that the extra empty line is important!
As you read back what the server sends back to you, you'll find it sends a number of lines that consist of Key: Value pairs, followed by a blank line, followed by more text.
The first part of the response are headers, and they describe the rest of the response, which is known as thebody.
In this case, the server is expected to close the connection after sending the body, but it doesn't. Your program should scan each line that comes back from the server and terminate the connection after it receives a response with the phrase "degrees F" in it.
Have your program print out ONLY the digits of the temperature. This is the temperature measured in the Computer Science lab in SCB401.
--------------------------------------------------------------------------------------------------------------------------
class TCPClient {
public static void main(String argv[]) throws Exception {
String modifiedSentence;
Socket clientSocket = new Socket("home.adelphi.edu", 80);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String toSend = "GET /~leune/temperature/temp_now.txt HTTP/1.0 ";
outToServer.writeBytes(toSend + ' ');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Explanation / Answer
import java.io.*;
import java.net.*;
public class TcpClient
{
public static void main(String[] args) throws IOException
{
Socket clientSocket = new Socket();
String host = "home.adelphi.edu";
PrintWriter s_out = null;
BufferedReader inFromServer = null;
try
{
clientSocket.connect(new InetSocketAddress(host , 80));
System.out.println("Connected");
//writer for socket
s_out = new PrintWriter( clientSocket.getOutputStream(), true);
//reader for socket
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
//Host not found
catch (UnknownHostException e)
{
System.err.println("Don't know about host : " + host);
System.exit(1);
}
//Send message to server
String toSend = GET/~leune/temperature/temp_now.txtHTTP/1.0 "
s_out.println( toSend );
System.out.println("Message send");
//Get response from server
String modifiedSentence;
while ((modifiedSentence = inFromServer.readLine()) != null)
{
System.out.println(“From Server:”+modifiedSentence );
}
//close the i/o streams
s_out.close();
inFromServer.close();
//close the socket
clientSocket.close();
}
}