Java Networking: Socket


Java Networking 

1Java Networking
2Java Networking: Socket
3Java Networking: ServerSocket
4Java Networking: UDP DatagramSocket
5Java Networking: URL + URLConnection
6Java Networking: JarURLConnection
7Java Networking: InetAddress
8Java Networking: Protocol Design

Java Networking: Socket

 
By Jakob Jenkov
 Connect with me: 
Rate article:
Share article:

In order to connect to a server over the internet (via TCP/IP) in Java, you need to create a java.net.Socket and connect it to the server. Alternatively you can use a Java NIO SocketChannel, in case you prefer to use Java NIO.

Creating a Socket

This code example connects to the server with IP address 78.46.84.171 on port 80. That server happens to be my web server (www.jenkov.com), and port 80 is the web servers port.

Socket socket = new Socket("78.46.84.171", 80);

You can also use a domain name instead of an IP address, like this:

Socket socket = new Socket("jenkov.com", 80);

Writing to a Socket

To write to a Java Socket you must obtain its OutputStream. Here is how that is done:

Socket socket = new Socket("jenkov.com", 80);
OutputStream out = socket.getOutputStream();

out.write("some data".getBytes());
out.flush();
out.close();

socket.close();

That‘s how simple it is!

Don‘t forget to call flush() when you really, really want the data sent across the internet to the server. The underlying TCP/IP implementation in your OS may buffer the data and send it in larger chunks to fit with with the size of TCP/IP packets.

Reading from a Socket

To read from a Java Socket you will need to obtains its InputStream. Here is how that is done:

Socket socket = new Socket("jenkov.com", 80);
InputStream in = socket.getInputStream();

int data = in.read();
//... read more data...

in.close();
socket.close();

Pretty simple, right?

Keep in mind that you cannot always just read from the Socket‘s InputStream until it returns -1, as you can when reading a file. The reason is that -1 is only returned when the server closes the connection. But a server may not always close the connection. Perhaps you want to send multiple requests over the same connection. In that case it would be pretty stupid to close the connection.

Instead you must know how many bytes to read from the Socket‘s InputStream. This can be done by either the server telling how many bytes it is sending, or by looking for a special end-of-data character.

Closing a Socket

When you are done using a Java Socket you must close it to close the connection to the server. This is done by calling the Socket.close() method, like this:

Socket socket = new Socket("jenkov.com", 80);

socket.close();






郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。