Java network socket
Intended learning outcomes
- TCP/IP Networking Model
- Understand the use of socket and reliable TCP protocol.
- Learn to establish TCP connection by using the Socket and ServerSocket class.
- Learn to handle multiple connections by multi-threaded programming.
- Understand the use of datagram and non-guaranteed UDP protocol.
- Learn to communicate in UDP by using DatagramPacket and DatagramSocket.
- IP Multicasting and Multicast Group
- Learn to use MulticastSocket to send and receive broadcast messages.
TCP/IP networking model
Have four layers of this model:
Application layer:providing process-to-process data exchange for applications
Transport layer:handling host-to-host communication
(TCP,UDP,RTP,SCTP)
Internet layer:providing internetworking between independent networks define the addressing and routing structures(IP,ICMP)
Link layer:containing communication methods for data that remains within a single network segment(ARP,PPP,MAC)
Other layers of TCP/IP:physical layer, session layer,presentation layers
Connection-Orientd vs Connectionless
This function in the transport layer(can be categorized:TCP and UDP)
- Connection-Oriented:TCP
- Connectionless:UDP
Then let us analyse differences between two ways:
TCP | UDP |
---|---|
reliable protocol | Unreliable protocal |
Keep track of lost packets and make sure they are resent | does’t keep track or lost packets |
FIFO | not care the order of arrival packets |
Slow and require more computer resource | Fast and use less computer resources |
Supprot flow control | no flow control |
Used by critical applications | used by real-time applications |
Network Socket
- A network socket is an endpoint of an inter-process communication flow across a computer network(进程间通信流的端点)(Similar to the java file object that provides an endpoint for read and write to a file)
- A socket is bound to a port number so that the transport layer can identify the application that data is destined to be sent.(传输层识别数据要发送的app)
- When a client attempts to make connection with a particular server program, it supplies the port number of the associated service and the host address. The host machine examines the port number and passes the client’s request to the appropriate server program for processing.
- Many clients can be connected to a server(多对一)The server distinguish the clients via the use of sockets.
- A client want to connect with a server, it creates a socket at its end of the communication link,Upon receiving the client’s request, server will create a new socket at its end that will be dedicated to communication with that particular client.
- Each TCP/IP or UDP/IP connection is uniquely defined by a 5-tuple:Source Address,Source Port Number,Destination Address,Destination port number and Protocal
socket communications in Java
- A socket is a bi-directional communication channel.(most fundamental means for client-server communications across the network)
- There are 2 types of sockets in java:
- Stream sockets(Connection-Oriented backed by TCP)
- Datagram Sockets(Conectionless,backed by UDP)
- The socket API is full duplex(全双工)-both client and server can send and receive at the same time.
- The socket operations at the client side are slightly different from those at the server side.
Stream Socket(code block)
Difference at two endpoint
Operation at server side | Operation at client side |
---|---|
A server is always waiting for incoming connection requests. So a server socket only specifies its own port number. | A client needs to specify both host address and port number of the |