Java Networking

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>

Tweet

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.

Next:   Java Networking: Socket

时间: 2024-10-15 05:11:44

Java Networking的相关文章

Java Networking: Socket

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 Networ

Java网络编程基础(三) Datagram类使用方法

Datagram(数据包)是一种尽力而为的传送数据的方式,它只是把数据的目的地记录在数据包中,然后就直接放在网络上,系统不保证数据是否能安全送到,或者什么时候可以送到,也就是说它并不保证传送质量. 1 UDP套接字 数据报(Datagram)是网络层数据单元在介质上传输信息的一种逻辑分组格式,它是一种在网络中传播的.独立的.自身包含地址信息的消息,它能否到达目的地.到达的时间.到达时内容是否会变化不能准确地知道.它的通信双方是不需要建立连接的,对于一些不需要很高质量的应用程序来说,数据报通信是一

【网络编程2】Java数据报套接字

这篇博文是本文学习<Java网络程序设计>书中第5章数据报套接字的学习总结.初学者网友学习这篇Java数据报套接字文章,如果难于理解文章前面理论部分,可以先运行后面的程序,边看运行后面的程序边理解前面的原理,这对初学者是最好的方法.所有源代码都在文章后面我的github链接代码中. --惠州学院13网络工程 吴成兵 20160609 目录 1 目录 1 一 数据报套接字概述 二 DatagramPacket 21 创建DatagramPacket对象 211 创建的DatagramPacket

Java NIO ServerSocketChannel

Java Nio  1 Java NIO Tutorial 2 Java NIO Overview 3 Java NIO Channel 4 Java NIO Buffer 5 Java NIO Scatter / Gather 6 Java NIO Channel to Channel Transfers 7 Java NIO Selector 8 Java NIO FileChannel 9 Java NIO SocketChannel 10 Java NIO ServerSocketCha

Java网络教程

本系列尚未翻译完成,有兴趣参与翻译的请在本文评论处留言. 1 Java 网络教程: 基础 2 Java 网络教程: Socket 3 Java 网络教程: ServerSocket 4 Java Networking: UDP DatagramSocket 5 Java 网络教程: URL + URLConnection 6 Java网络教程:JarURLConnection 7 Java 网络教程: InetAddress 8 Java网络教程:Protocol Design 转载自 并发编程

Java NIO SocketChannel

Java Nio  1 Java NIO Tutorial 2 Java NIO Overview 3 Java NIO Channel 4 Java NIO Buffer 5 Java NIO Scatter / Gather 6 Java NIO Channel to Channel Transfers 7 Java NIO Selector 8 Java NIO FileChannel 9 Java NIO SocketChannel 10 Java NIO ServerSocketCha

JDK/JRE5.0中对于IPv6的支持-解读JDK5.0对IPv6网络编程的支持

编程|网络 JDK5.0 Document:Networking IPv6 User Guide for JDK/JRE 5.0This document covers the following topics:Overview Supported Operating Systems Using IPv6 in Java Details on IPv6 Support in Java Special IPv6 Address Types IPv6-Related System Propertie

并发网系列文章集

JAVA: Java视角理解系统结构 Java 7: 全面教程 Java8初体验系列文章 Java ByteCode Java字节码浅析 Java Virtual Machine JVM实用参数系列 JVM性能优化 Java Virtual Machine  Concurrency Java虚拟机并发编程 Java Memory Model 深入理解java内存模型 Java内存模型Cookbook-前言 Java内存模型FAQ 同步和Java内存模型 Java Concurrency Java

《Netty实战》Netty In Action中文版 第1章——Netty——异步和事件驱动(一)

<Netty实战>样章由人民邮电出版社授权并发编程网发布,本书的中文版已经由人民邮电出版社引进并出版. 京东预售链接(优先发货):<Netty实战>([美]诺曼·毛瑞尔(Norman Maurer),马文·艾伦·沃尔夫泰尔(Marvin Allen Wolfthal)) 第一部分 Netty的概念及体系结构 Netty是一款用于创建高性能网络应用程序的高级框架.在第一部分,我们将深入地探究它的能力,并且在3个主要的方面进行示例: 使用Netty构建应用程序,你不必是一名网络编程专家