天天看點

Java Networking: SocketJava Networking: Socket

Java Networking 

1

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/index.html">Java Networking</a>

2

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/sockets.html">Java Networking: Socket</a>

3

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/server-sockets.html">Java Networking: ServerSocket</a>

4

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/udp-datagram-sockets.html">Java Networking: UDP DatagramSocket</a>

5

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/url-urlconnection.html">Java Networking: URL + URLConnection</a>

6

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/jarurlconnection.html">Java Networking: JarURLConnection</a>

7

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/inetaddress.html">Java Networking: InetAddress</a>

8

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/protocol-design.html">Java Networking: Protocol Design</a>

Java Networking: SocketJava Networking: Socket

Rate article:

&lt;iframe frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" tabindex="0" vspace="0" width="100%" id="I0_1416446000690" name="I0_1416446000690" src="https://apis.google.com/se/0/_/+1/fastbutton?usegapi=1&amp;amp;origin=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;url=http%3A%2F%2Ftutorials.jenkov.com%2Fjava-networking%2Fsockets.html&amp;amp;gsrc=3p&amp;amp;ic=1&amp;amp;jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.zh_CN.0KI2lcOUxJ0.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCPnLWTRWXjQ3yHtGTFSsUVyRcOV5g#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh&amp;amp;id=I0_1416446000690&amp;amp;parent=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;pfname=&amp;amp;rpctoken=31463063" data-gapiattached="true" style="position: absolute; top: -10000px; width: 450px; margin: 0px; border-style: none;"&gt;&lt;/iframe&gt;

Share article:

&lt;iframe frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" tabindex="0" vspace="0" width="100%" id="I1_1416446000694" name="I1_1416446000694" src="https://apis.google.com/se/0/_/+1/sharebutton?plusShare=true&amp;amp;usegapi=1&amp;amp;action=share&amp;amp;height=24&amp;amp;annotation=none&amp;amp;origin=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;url=http%3A%2F%2Ftutorials.jenkov.com%2Fjava-networking%2Fsockets.html&amp;amp;gsrc=3p&amp;amp;ic=1&amp;amp;jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.zh_CN.0KI2lcOUxJ0.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Ft%3Dzcms%2Frs%3DAGLTcCPnLWTRWXjQ3yHtGTFSsUVyRcOV5g#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh%2Conload&amp;amp;id=I1_1416446000694&amp;amp;parent=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;pfname=&amp;amp;rpctoken=22420384" data-gapiattached="true" style="position: absolute; top: -10000px; width: 450px; margin: 0px; border-style: none;"&gt;&lt;/iframe&gt;

<a target="_blank" href="https://twitter.com/share">Tweet</a>

Table of Contents

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/sockets.html#creating-a-socket">Creating a Socket</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/sockets.html#writing-to-a-socket">Writing to a Socket</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/sockets.html#reading-from-a-socket">Reading from a Socket</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/sockets.html#closing-a-socket">Closing a Socket</a>

<a target="_blank"></a>

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

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

That's how simple it is!

Don't forget to call <code>flush()</code> 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.

Pretty simple, right?

Keep in mind that you cannot always just read from the Socket's <code>InputStream</code> 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 <code>InputStream</code>. 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.

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

<a target="_blank" href="http://tutorials.jenkov.com/java-networking/server-sockets.html">Next:   Java Networking: ServerSocket</a>