thpogani
thpogani

Reputation: 13

HTTP Client with Java sockets

i try to open a TCP Socket in Java and sent a GET request to a server (www.abc.net.au) and print the response to the console.

Client code:

import java.util.Scanner;

import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Client {
    public static void main(String[] args) throws Exception{

        Scanner sc;
        String addressString;
        int port =80;
        int timeoutMs = 30000;
        String temp;

        //Read address from commandline
        sc =new Scanner(System.in);
        System.out.print("Adresse: ");
        addressString = sc.next();
        sc.close();
        //Open socket
        InetAddress addr = InetAddress.getByName(addressString);
        SocketAddress sockaddr = new InetSocketAddress(addr, port);
        Socket socket = new Socket();

        //Connection timeout
        socket.connect(sockaddr,timeoutMs);

        System.out.println( socket.getPort() +"\n");

        //Define input/output
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);

        //Send GET request
        pw.print("GET / HTTP/1.1\r\n");
        pw.print("Host: "+ addressString +"\r\n\r\n");
        while((temp=br.readLine())!=null){
            System.out.println(temp);

        }
        //Close Input/Output and Socket
        pw.close();
        br.close();
        socket.close();
    }
}

The code seems to work till it reaches the while and afterwards i get:

HTTP/1.0 408 Request Time-out
Server: AkamaiGHost
Mime-Version: 1.0
Date: Sun, 07 Oct 2018 15:36:40 GMT
Content-Type: text/html
Content-Length: 218
Expires: Sun, 07 Oct 2018 15:36:40 GMT

<HTML><HEAD>
<TITLE>Request Timeout</TITLE>
</HEAD><BODY>
<H1>Request Timeout</H1>
The server timed out while waiting for the browser's request.<P>
Reference&#32;&#35;2&#46;3d5b6068&#46;1538926600&#46;0
</BODY></HTML>

Process finished with exit code 0

I don't understand what causes the problem, already looked for answers on stackoverflow, java2s and a book i got at home. Trying other pages often ends just with:

Process finished with exit code 0

and nothing else.

Any idea what's missing/incorrect with my request?

Appriciate every hint.

Upvotes: 1

Views: 4329

Answers (1)

Iłya Bursov
Iłya Bursov

Reputation: 24219

PrintWriter has internal buffer which is used to store data, so when you're calling print - nothing is sent to server

From https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output.

So, you can either use of these methods instead of print or just call flush explicitely:

pw.print("GET / HTTP/1.1\r\n");
pw.print("Host: " + addressString + "\r\n\r\n");
pw.flush(); // actually send data to server

Upvotes: 1

Related Questions