天天看点

Java NetworkingJava Networking

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 NetworkingJava Networking

Rate article:

&lt;iframe frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" tabindex="0" vspace="0" width="100%" id="I0_1416445641285" name="I0_1416445641285" 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%2Findex.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_1416445641285&amp;amp;parent=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;pfname=&amp;amp;rpctoken=41058523" 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_1416445641289" name="I1_1416445641289" 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%2Findex.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_1416445641289&amp;amp;parent=http%3A%2F%2Ftutorials.jenkov.com&amp;amp;pfname=&amp;amp;rpctoken=26664239" 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>

Java has a reasonably easy-to-use builtin networking API which makes it easy to communicate via TCP/IP sockets or UDP sockets over the internet. TCP is typically used more often than UDP, but both options are explained in this tutorial.

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

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

<a target="_blank" href="http://tutorials.jenkov.com/java-multithreaded-servers/index.html">Java Multithreaded Servers Tutorial</a>

Alternatively you can use the networking classes in the Java NIO API. These classes are similar to the classes found in the Java Networking API, except the Java NIO API can work in non-blocking mode. Non-blocking mode may give a performance boost in some situations.

Typically a client opens a TCP/IP connection to a server. The client then starts to communicate with the server. When the client is finished it closes the connection again. Here is an illustration of that:

A client may send more than one request through an open connection. In fact, a client can send as much data as the server is ready to receive. The server can also close the connection if it wants to.

Socket's and ServerSocket's are covered in more detail in later texts.

UDP works a bit differently from TCP. Using UDP there is no connection between the client and server. A client may send data to the server, and the server may (or may not) receive this data. The client will never know if the data was received at the other end. The same is true for the data sent the other way from the server to the client.

Because there is no guarantee of data delivery, the UDP protocol has less protocol overhead.

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