0% found this document useful (0 votes)
5 views4 pages

TCP Socket HTTP Client Example

Uploaded by

Vijayakrishnan J
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

TCP Socket HTTP Client Example

Uploaded by

Vijayakrishnan J
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Write a HTTP web client program to download a web page using TCP sockets

[Link]: 2

import [Link].*;

import [Link].*;

public class SocketHTTPClient

public static void main(String[] args)

String hostName = "[Link]";

int portNumber = 80;

try {

Socket socket = new Socket(hostName, portNumber);

PrintWriter out = new PrintWriter([Link](), true);

BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));

[Link]("GET / HTTP/1.1\r\n");

[Link]("Host: " + hostName + "\r\n");

[Link]("\r\n");

[Link]();

String inputLine;

while ((inputLine = [Link]()) != null)

[Link](inputLine);

}
} catch (UnknownHostException e)

[Link]("Don't know about host " + hostName);

[Link](1);

} catch (IOException e)

[Link]("Couldn't get I/O for the connection to " + hostName);

[Link](1);

Applications using TCP sockets like Echo client and echo server

[Link]:3a

SERVER Program

import [Link].*;

import [Link].*;

public class EServer {

public static void main(String[] args) throws IOException {

ServerSocket ss = new ServerSocket(5000);

[Link]("Server running on port 5000...");

Socket s = [Link](); // accept client

BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));

PrintStream out = new PrintStream([Link]());


String line;

while ((line = [Link]()) != null) {

[Link]("Client: " + line);

[Link](line); // echo back

if ([Link]("bye")) break;

[Link]();

[Link]();

Client Program

import [Link].*;

import [Link].*;

public class EClient {

public static void main(String[] args) throws IOException {

Socket c = new Socket("localhost", 5000);

BufferedReader user = new BufferedReader(new InputStreamReader([Link]));

PrintStream out = new PrintStream([Link]());

BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));

String line;

while (true) {

[Link]("Client: ");

line = [Link]();

[Link](line);
[Link]("Server: " + [Link]());

if ([Link]("bye")) break;

[Link]();

You might also like