You are on page 1of 2

java.net.

Socket
A socket is an endpoint of a network connection. A socket enables an application to
read from and write to the network. Two software applications residing on two different
computers can communicate with each other by sending and receiving byte streams over a
connection. To send a message from your application to another application, you need to
know the IP address as well as the port number of the socket of the other application. In
Java, a socket is represented by a java.net.Socketobject.
To create a socket, you can use one of the many constructors of the Socket class.
One of these constructors accepts the host name and the port number:
public Socket(java.lang.String host, int port)
where host is the remote machine name or IP address and port is the port number of
the remote application. For example, to connect to yahoo.com at port 80, you would
construct the following Socket object:
new Socket("yahoo.com", 80)
Once you create an instance of the Socket class successfully, you can use it to send
and receive streams of bytes. To send byte streams, you must first call
the Socket class's getOutputStream method to obtain a java.io.OutputStream object.
To
send
text
to
a
remote
application,
you
often
want
to
construct
a java.io.PrintWriter object from the OutputStream object returned. To receive byte
streams
from
the
other
end
of
the
connection,
you
call
the Socket class's getInputStream method that returns a java.io.InputStream.
The code in Listing simulates an HTTP client using a socket. It sends an HTTP request
to the host and displays the response from the server.
To get a proper response from the Web server, you need to send an HTTP request
that complies with the HTTP protocol. If you have read the previous section, The
Hypertext Transfer Protocol (HTTP), you should be able to understand the HTTP request in
the code above.
Note
The HttpClient library from the Apache HTTP Components project
(http://hc.apache.org) provides classes that can be used as a more sophisticated HTTP
client.

Listing: A simple HTTP client


import
import
import
import
import
import

java.io.BufferedReader;
java.io.IOException;
java.io.InputStreamReader;
java.io.OutputStream;
java.io.PrintWriter;
java.net.Socket;

public class HTTPRequest


{
public static void main(String[] args)
{
String host = "www.rajalakshmi.org";
try
{
Socket socket = new Socket(host, 80);
OutputStream os = socket.getOutputStream();
boolean autoflush = true;
PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
// send an HTTP request to the web server
out.println("GET / HTTP/1.1");
out.println("Host: " + host + ":80");
out.println("Connection: Close");
out.println();
// read the response
boolean loop = true;
StringBuilder sb = new StringBuilder(8096);
while (loop)
{
if (in.ready())
{
int i = 0;
while (i != -1)
{
i = in.read();
sb.append((char) i);
}
loop = false;
}
}
// display the response to the out console
System.out.println(sb.toString());
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

You might also like