Server sockts

Hi there
I am working on the following code and I cannot seem to get it to compile please advise.
import java.io.*;
import java.net.*;
import java.util.Scanner;
 
class GetDataFromWebServer {
 
GetDataFromWebServer () throws IOException;
 
 
String address = "http://www.curl.com";
URL u = new URL (address);
URLConnection connection = u.openConnection();
 
 
final int HTTP_PORT  = 80;
 
 
Socket  s = new Socket() ;
 
 
InputStream ins = s.getInputStream();
 
 
OutputStream outs = s.getOutputStream();
 
 
Scanner in = new Scanner (ins);
 
 
PrintWriter out = new PrintWriterv("outs");
 
 
String command = "GET / HTTP/1.0\n\n";
out.print(command);
 
 
 
out.flush ();
 
 
while (in.hasNextLine()) {
String line = in.nextLine();
out.println (line);
}
 
 
s.close;
 
public sProxy-Connection: keep-alive Cache-Control: max-age=0 tic void main (String[] args) {
try {
GetDataFromWebServer g = new GetDataFromWebServer();
}
catch (Exception e) {
}
}

Open in new window

SdurraniAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Thomas4019Connect With a Mentor Commented:
I would say use URLConnection or use a Socket. Do NOT use both. URLConnection is like a socket but designed specifically for URLs. I went through you code fixing your problems and modifiying it to only use URLConnection. Currently it reads the webpage and saves it to "outFile.txt".
import java.io.*;
import java.net.*;
import java.util.Scanner;
 
class GetDataFromWebServer
{
	GetDataFromWebServer() throws IOException
	{
		String address = "http://www.curl.com";
		URL u = new URL (address);
		URLConnection connection = u.openConnection();
 
		InputStream ins = connection.getInputStream();
 
		Scanner in = new Scanner(ins);
 
		PrintWriter out = new PrintWriter("outFile.txt");
 
		while (in.hasNextLine())
		{
			String line = in.nextLine();
			out.println (line);
		}
 
		out.close();
	}
 
	public static void main (String[] args)
	{
		try
		{
			GetDataFromWebServer g = new GetDataFromWebServer();
		}
		catch (Exception e){e.printStackTrace();}
	}
}

Open in new window

0
 
swapnil001Connect With a Mentor Commented:

import java.io.*;
import java.net.*;
import java.util.Scanner;
 
class GetDataFromWebServer {
 
    GetDataFromWebServer() throws IOException {
//        String address = "http://www.curl.com";
        String host = "curl.com";
//        URL u = new URL (address);
//        URLConnection connection = u.openConnection();
        final int HTTP_PORT  = 80;
        Socket  s = new Socket(host,HTTP_PORT) ;
        InputStream ins = s.getInputStream();
        OutputStream outs = s.getOutputStream();
        Scanner in = new Scanner (ins);
        PrintWriter out = new PrintWriter(outs);
        String command = "GET / HTTP/1.0\n\n";
        out.print(command);
        out.flush ();
        while (in.hasNextLine()) {
            String line = in.nextLine();
            System.out.println (line);
        }
        s.close();
    }
    public static void main (String[] args) {
    try {
        GetDataFromWebServer g = new GetDataFromWebServer();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    }
}

Open in new window

0
 
__geof__Connect With a Mentor Commented:
The whole structure of your code is wrong. You need to format it correctly. I guess that GetDataFromWebServer () throws IOException; is supposed to be a method so it should be GetDataFromWebServer () throws IOException{
Second: public sProxy-Connection: keep-alive Cache-Control: max-age=0 tic void main (String[] args) { is not a correct format either.
0
All Courses

From novice to tech pro — start learning today.