Java, TCP / IP UDP protocol a simple summary

1. UDP and TCP / IP difference

The UDP
1. connectionless-oriented, data transfer is not particularly safe
2. Because the connectionless transmission speed
3. Since the connectionless-oriented, packet loss exists data transfer
4. UDP client and server without distinction, as can be transmitted and the receiver
UDP protocol to use scene
live, online games

TCP / IP
1. connection-oriented, more secure data transfer
2. Because connection-oriented, all delivery is slower
connection 3. oriented, secure data transfer
4. TCP / IP protocol is a clear concept of server and client
TCP / IP protocol usage scenarios
client login, data download, file transfer

2. IP class

SUN offers developers use the IP address class
InetAddress
common methods:
InetAddress getLocalHost ();
obtaining the IP address of the class object
InetAddress getByName (String str);
Get IP address of the object corresponding to the specified host name
InetAddress [] getAllByName (String str);
Get the specified host name, the domain name or IP addresses corresponding to all class objects

package com.qfedu.a_ip;

import java.net.InetAddress;
import java.net.UnknownHostException;

/*
 * IP类演示
 */
public class Demo1 {
	public static void main(String[] args) throws UnknownHostException {
		InetAddress localHost = InetAddress.getLocalHost();
		System.out.println(localHost);
		
		InetAddress byName = InetAddress.getByName("DESKTOP-M89SDP7");
		System.out.println(byName);
		
		InetAddress byName2 = InetAddress.getByName("www.4399.com");
		System.out.println(byName2);
		
		System.out.println("----------------------------------");
		
		InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com");
		for (InetAddress inetAddress : allByName) {
			System.out.println(inetAddress);
		}
		
		System.out.println("----------------------------------");
		InetAddress[] allByName1 = InetAddress.getAllByName("www.taobao.com");
		for (InetAddress inetAddress : allByName1) {
			System.out.println(inetAddress);
		}
		
		System.out.println("----------------------------------");
		InetAddress[] allByName2 = InetAddress.getAllByName("www.jd.com");
		for (InetAddress inetAddress : allByName2) {
			System.out.println(inetAddress);
		}
		
	}
}

3. UDP data transmission

User Datagram Protocol
data transfer using the packet transfer mode, all of the data packaging operation to be performed, and there is no concept corresponding to the client server, and only the transmission section and the receiving end

Socket Socket
data transfer operation is required, it must have a corresponding Socket data transfer among the two computers. UDP protocol used here, there must be a protocol UDP Socket
DatagramSocket ();
to create a transmission protocol UDP Socket object side
DatagramSocket (int port);
create a Socket object receiving the UDP protocol, where the need to listen [port] Specify

The method of packing packets sending end:
of DatagramPacket of DatagramPacket (byte [] buf, int length, InetAddress address, int Port);
buf: array of bytes needed to transfer data
length: the current byte in the array is the number of bytes of data capacity
address: receiving the IP address of an object side
port: the port number corresponding to the receiving end

A receiving end receiving packets embodiment
herein have a blank packet
of DatagramPacket of DatagramPacket (byte [] buf, int length);
buf: buffer byte array, is typically an integer multiple of 1024
length: the capacity of the current byte array buffer

UDP code demonstrates

3.1 transmitting end

Process:
1. Create a UDP server transmitting end corresponding to the Socket
2. prepare the corresponding data packet, with the specified data needs to
3. The transmission data Send
4. Close UDP sending end

package com.qfedu.b_udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/*
流程:
	1. 创建UDP服务器对应的发送端Socket
	2. 准备对应数据包,需要带有指定数据
	3. 发送数据 send
	4. 关闭UDP发送端
 */
public class SenderDemo1 {
	public static void main(String[] args) throws IOException {
		System.out.println("发送端启动");
		// 创建对应的Socket
		DatagramSocket socket = new DatagramSocket();
		
		// 准备数据包
		byte[] bytes = "今天中午吃蒸羊羔...".getBytes();
		DatagramPacket packet = new DatagramPacket(bytes,  // 字节数组数据
				bytes.length,  // 字节数组数据长度
				InetAddress.getLocalHost(),  // 指定接收端IP地址
				8848); // 8848对应端口号
		
		// 发送数据包
		socket.send(packet);
		
		// 关闭UDP发送端
		socket.close();
	}
}	

3.2 the receiving end

Process:
1. Open the UDP service, and listening on the specified port
2. Create a new empty packet
3. Socket receive data through
4. Close the UDP service receiving end

package com.qfedu.b_udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/*
流程:
	1. 打开UDP服务,并且监听指定端口
	2. 创建新的空数据包
	3. 通过Socket接收数据 receive
	4. 关闭UDP服务接收端
 */
public class ReceiveDemo1 {
	public static void main(String[] args) throws IOException {
		// 创建Socket监听端口
		DatagramSocket socket = new DatagramSocket(8848);
		
		// 准备空数据包
		byte[] buf = new byte[1024];
		DatagramPacket packet = new DatagramPacket(buf, buf.length);
		
		// 接收数据
		socket.receive(packet);
		
		// 确定接收到的字节长度
		int length = packet.getLength();
		
		System.out.println(new String(buf, 0, length));
		
		// 关闭socket
		socket.close();
		
	}
}
Published 10 original articles · won praise 10 · views 4490

Guess you like

Origin blog.csdn.net/wangjizhan2010/article/details/104660736