Java Networking

简介: Java Networking 1Java Networking2Java Networking: Socket3Java Networking: ServerSocket4Java Networking: UDP DatagramSocket5Java...


Java Networking 

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

Java Networking

 
By Jakob Jenkov
 Connect with me: 
Rate article:
<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;origin=http%3A%2F%2Ftutorials.jenkov.com&amp;url=http%3A%2F%2Ftutorials.jenkov.com%2Fjava-networking%2Findex.html&amp;gsrc=3p&amp;ic=1&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;id=I0_1416445641285&amp;parent=http%3A%2F%2Ftutorials.jenkov.com&amp;pfname=&amp;rpctoken=41058523" data-gapiattached="true" style="position: absolute; top: -10000px; width: 450px; margin: 0px; border-style: none;"></iframe>
Share article:
<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;usegapi=1&amp;action=share&amp;height=24&amp;annotation=none&amp;origin=http%3A%2F%2Ftutorials.jenkov.com&amp;url=http%3A%2F%2Ftutorials.jenkov.com%2Fjava-networking%2Findex.html&amp;gsrc=3p&amp;ic=1&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;id=I1_1416445641289&amp;parent=http%3A%2F%2Ftutorials.jenkov.com&amp;pfname=&amp;rpctoken=26664239" data-gapiattached="true" style="position: absolute; top: -10000px; width: 450px; margin: 0px; border-style: none;"></iframe>

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.

There are three other tutorials here at tutorials.jenkov.com that are relevant to this Java networking tutorial. These are:

  1. Java IO Tutorial
  2. Java NIO Tutorial
  3. Java Multithreaded Servers Tutorial

Even though the Java Networking APIs enable you to open and close network connections via sockets, all communication happens via the Java IO classes InputStream and OutputStream.

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.

Java TCP Networking Basics

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.

Java Socket's and ServerSocket's

When a client wants to open a TCP/IP connection to a server, it does so using a Java Socket. The socket is told what IP address and TCP port to connect to and the rest is done by Java.

If you want to start a server that listens for incoming connections from clients on some TCP port, you have to use aJava ServerSocket. When a client connects via a client socket to a server's ServerSocket, a Socket is assigned on the server to that connection. The client and server now communicates Socket-to-Socket.

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

Java UDP Networking Basics

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.

There are several situations in which the connectionless UDP model is preferable over TCP. These are covered in more detail in the text on Java's UDP DatagramSocket's.








目录
相关文章
|
2月前
|
存储 缓存 安全
从0开始回顾Java---系列四
String 1、String 是 Java 基本数据类型吗?可以被继承吗?为什么? String是Java基本数据类型吗? 不是。Java 中的基本数据类型只有8个:byte、short、int、long、float、double、char、boolean;除了基本类型(primitive type),剩下的都是引用类型(reference type)。 String是一个比较特殊的引用数据类型。 String 类可以继承吗? 不行。String 类使用 final 修饰,是所谓的不可变类,无法被继承。 String 类为什么要被设计成不可继承? 1. 字符串是一种非常重要且常用的数据类型
|
2月前
|
安全 Java 程序员
从0开始回顾Java---系列一
Java概述 1、什么是Java? Java是一门面向对象的编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。 Java语言作为静态面向对象编程语言的优秀代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。 2、Java 语言的优点? 1. 平台无关性,摆脱硬件束缚,"一次编写,到处运行",保证这一点的是 Java 的虚拟机机制。 2. 相对安全的内存管理和访问机制,避免大部分内存泄漏和指针越界。 3. 面向对象(封装,继承,多态) 4. 支持多线程。C++ 语言没有内置的多线程
|
5月前
|
消息中间件 数据可视化 Java
【Java】The Java Headless Mode
【Java】The Java Headless Mode
53 0
【Java】The Java Headless Mode
|
12月前
|
Java 应用服务中间件 Linux
Java 在linux或者tomcat下使用java.jwt.*这个类,报java.awt.headless 报空异常
在开发的过程中使用到了java.jwt.*包下的东西,在开发工具中使用没问题,但是如果到了单独的tomcat或Linux里就会报:java.awt.headless null空异常,再去配置java mv?非常麻烦,看我是如何解决的。
93 0
java.security.egd
java.security.egd
|
算法 Java 编译器
java.io.InvalidClassException异常解决
java.io.InvalidClassException异常解决
Java8-Annotations
import java.lang.annotation.ElementType; import java.lang.annotation.Repeatable; import java.
900 0