UDP and TCP 2 for Network Programming

TCP

TCP: A connection-oriented, reliable, byte-stream-based transport layer, communication protocol
Features: Connection-oriented,
Point-to-point communication
high reliability
High system resources and low efficiency
UDP: A connectionless transport layer protocol that provides simple, unreliable, transaction-oriented messaging services
Features: Non-connection oriented, unreliable transmission, possible loss
Send whether the other party is ready or not, the recipient receives and does not confirm
Can broadcast
Very simple protocol, low overhead
Socket Socket
The network applications we develop are at the application layer, TCP and UDP are transport layer protocols
How do you use transport layer services in the application layer?
Between the application and transport layers, sockets are used for separation
A socket is like a small opening in the transport layer for an application layer through which an application sends or receives data sent remotely, and within this opening, after the data enters this port or before the data comes out of this port, it does not know or need to know, nor care about how it is transmitted. This belongs to other levels of the network.Work

UDP Programming

UDP Sender

package UDP programming;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

/**
 * Sender
 * 1.Create sender using DatagramSocket specified port
 * 2.Prepare a data container that must be converted to a byte array.
 * 3.Encapsulate as a DatagramPacket package, specifying a destination
 * 4.Send package send(DatagramPacket p)
 * 5.Release Resources
 * @author T011921
 *
 */
public class UdpClient {
	public static void main(String[] args) throws Exception {
		System.out.println("The sender is starting.");
//		 * 1. Use DatagramSocket to specify the port to create the sender
		DatagramSocket client = new DatagramSocket(8888);
//		 * 2. Prepare the data container and convert it to a byte array.
		String data = "Wuhan Petroleum, China Petroleum";
		byte[] datas = data.getBytes();
//		 * 3. Encapsulate as a DatagramPacket package and specify a destination
		DatagramPacket packet = new DatagramPacket(datas,0,datas.length,
				new InetSocketAddress("localhost",9999));
//		 * 4. Send package send(DatagramPacket p)
		client.send(packet);
//		 * 5. Release Resources
		client.close();
	}
}


UDP Receiver

package UDP programming;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * receiving end
 * 1.Use DatagramSocket to specify port to create receiver
 * 2.Prepare containers to encapsulate into DatagramPacket packages
 * 3.Blocking Receive Package (DatagramPacket p)
 * 4.Analyzing data
 * 	byte[] getData()
 *  	   getLength()
 * 
 * @author T011921
 *
 */
public class UdpServer {
	public static void main(String[] args) throws Exception {
		System.out.println("Receiver Receiving");
//		 * 1. Use DatagramSocket to specify the port to create the receiver
		DatagramSocket server = new DatagramSocket(9999);
//		 * 2. Prepare containers to encapsulate as DatagramPacket packages
		byte[] container = new byte[1024*60];
		DatagramPacket packet = new DatagramPacket(container,0,container.length);
//		 * 3. Blocking package receive(DatagramPacket p)
		server.receive(packet);
//		 * 4. Analyzing data
//		 * 	byte[] getData()
//		 *  	   getLength()
		byte[] datas = packet.getData();
		int len = packet.getLength();
		System.out.println(new String(datas,0,len));
//		5. Release resources
		server.close();
	}
}

TCP programming

TCP Client

package TCP programming;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 	Familiarize yourself with the process
 * Create Client Server
 * 1.Set up a connection to use the Socket to create the address and port of the client+service
 * 2.Operation: Input and output stream operation
 * 3.Release Resources
 *
 */
public class Client {
	public static void main(String[] args) throws Exception, IOException {
		System.out.println("-------------clent------");
//		* 1. Set up a connection and use the Socket to create the address and port of the client+service
		Socket client = new Socket("localhost",8888);
//		 * 2. Operation: Input and Output Stream Operation
		DataOutputStream dos = new DataOutputStream(client.getOutputStream());
		String data = "hello";
		dos.writeUTF(data);
//		 * 3. Release resources
		dos.close();
		client.close();

	}
}

TCP Server

package TCP programming;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 	Familiarize yourself with the process
 * Create Server
 * 1.Specify the port to use ServerSocket to create the server
 * 2.Blocking Wait Connection accept
 * 3.Operation: Input and output stream operation
 * 4.Release Resources
 *
 */
public class Server {

	public static void main(String[] args) throws Exception {
		System.out.println("-------------server------");
//		* 1. Specify the port and use ServerSocket to create the server
		ServerSocket server =new  ServerSocket(8888);
//		 * 2. Blocked waiting connection accept
		Socket client =  server.accept();
		System.out.println("A client established a connection");
//		 * 3. Operation: Input and Output Stream Operation
		DataInputStream dis = new DataInputStream(client.getInputStream());
		String data = dis.readUTF();
		System.out.println(data);
//		 * 4. Release resources
		dis.close();
		client.close();
		server.close();
	}
}

86 original articles published. 2. 10,000 visits+
Private letter follow

Tags: Java socket Programming network

Posted on Mon, 03 Feb 2020 23:02:38 -0500 by naskar