Simple example of Socket based on UDP protocol

Simple example of Socket based on UDP protocol

Use two classes  DatagramPacket -- representing the datagram packet
                  DatagramSocket -- representing the class for end-to-end communication

Four steps
on the server side 1. Define the data content of the server IP port
2. Create a DategramPacket data packet object to include the data content
3. Create a DategramSocket object 
4. Socket.send() to send;

 

UdpSocketServer code 
package com.chen.study.socket.socketServer;

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

public class UdpSocketServer {
	private static int port = 12000;
	
	/**
	 * UDP socket service
	 */
	public static void UdpSocketServer(){
		try {
			//1. Create service port
			DatagramSocket socket = new DatagramSocket(port);
			System.out.println("UDPsocket has started.....");
			//2. Create a datagram to receive the data sent by the client
			byte[] data = new byte[1024];
			DatagramPacket packet = new DatagramPacket(data, data.length);
			//3. Accept client request
			socket.receive(packet);//This method is blocked until data is received
			//4. Read data
			String info = new String(data, 0,packet.getLength());
			System.out.println("I am the server, the client said: "+info);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
	}
	public static void main(String[] args) {
		UdpSocketServer();
	}
}
 

 

client 

1. Get the IP address and port number InetAddress inetaddress

2. Create a datagram

3. Create Socket

4. Send Socket.send(DatagramPacket packet)

The client code writes
package com.chen.study.socket.socketServer;

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

public class UdpSocketClient {

	private static int port = 12000;
	private static String ip = "192.168.119.35";
	
	public static void UdpSocketClient(){
		
		try {
			byte[] data = "Hello server!".getBytes();
		System.out.println("Client sends: "+"Hello server!");
		InetAddress inetaddress = InetAddress.getByName(ip);
		//create datagram
		DatagramPacket packet = new DatagramPacket(data, data.length, inetaddress, port);
		//create socket
		DatagramSocket  socket = new DatagramSocket();
		socket.send(packet);
			
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UdpSocketClient();
	}

}
 

 

 

Attached:

String info = new String (date,0,packet.getLength());
String constructor
public String(byte[] bytes,
int offset,
int length)
bytes - byte 
offset to be decoded into characters - the first one to decode byte index 
length - the number of bytes to decode

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326720232&siteId=291194637