Hi, I am new to TCP socket. I have a question about socket. I need modify the TCP Client program below, to fetch the webpage at: www.cs.montclair.edu/~wangd/pubs.html. I run the code below, but got an 404 error.
Any inputs are appreciated.
Below is the code.
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("cs.montclair.edu", 80);
DataOutputStream outToServer = new
DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
System.out.println("Type in any sentence: ");
sentence = inFromUser.readLine();
System.out.println(" in sentence: "+sentence);
outToServer.writeBytes("GET/~wangd/pubs.html" + '\n');
outToServer.flush();
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
ASKER
You almost certainly don't want to do this using a raw TCP Socket.Quite right. Unless of course this is a HTTP learning exercise. If that's the case, you're doing things back to front. First you need to know the protocol before coding. As someone pointed out, it's clear that you don't yet know it, so you need to learn it first.
ASKER
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
outToServer.writeBytes("GET /~wangd/pubs.html" + '\n');