Lt9-Java Network Socket

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:
  1. Stream sockets(Connection-Oriented backed by TCP)
  2. 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值