基于UTP的Socket编程(基础)

2023-12-25 16:38:20 浏览数 (1)

User

代码语言:javascript复制
package com.imooc.UTPReturn;

import java.io.Serializable;

public class User implements Serializable{
	
	private String username;
	
	private String password;

	public User(String username, String password) {
		
		this.username = username;
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [username="   username   ", password="   password   "]";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	

}

Server

代码语言:javascript复制
package com.imooc.UTPReturn;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UTPServer {

	public static void main(String[] args) throws Exception {

		/*
		 * 服务器端接收客户端的请求
		 */
		// 创建服务器端DatagramSocket
		DatagramSocket socket = new DatagramSocket(8800);

		// 创建数据包
		byte[] data = new byte[1024];
		DatagramPacket packet = new DatagramPacket(data, data.length);

		// 服务器端已经启动...
		System.out.println("服务器端已经 启动....");
		socket.receive(packet);// 接收数据

		// 读取客户端发来的数据
		// String str=new String(data, 0, packet.getLength());
		// System.out.println("我是服务器,客户端说:" str);

		// bytearray to object
		ByteArrayInputStream bi = new ByteArrayInputStream(data);
		ObjectInputStream oi = new ObjectInputStream(bi);

		User user = (User)oi.readObject();
		bi.close();
		oi.close();
		System.out.println(user.toString());
		
		

		/*
		 * 服务器端回应客户端
		 */
		// 给客户端发出响应
		byte[] responseData = null;
		responseData = "欢迎你".getBytes();
		InetAddress address = packet.getAddress();
		int port = packet.getPort();

		DatagramPacket responsePacket = new DatagramPacket(responseData,
				responseData.length, address, port);

		socket.send(responsePacket);

		// 关闭资源
		socket.close();
	}

}

Client

代码语言:javascript复制
package com.imooc.UTPReturn;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UTPClient {

	public static void main(String[] args) throws Exception {
		/*
		 * 客户端向服务器端发送数据
		 */
		// 定义数据报的地址,端口,数据
		InetAddress address = InetAddress.getByName("localhost");
		int port = 8800;
		// 定义数据报的内容
		// byte[] data="用户名admin,密码123".getBytes();
		User user = new User("张三", "123");
		byte[] data = null;
		
		
		// object to bytearray
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		ObjectOutputStream oo = new ObjectOutputStream(bo);
		oo.writeObject(user);
		data = bo.toByteArray();

		bo.close();
		oo.close();
		// 创建数据报
		DatagramPacket packet = new DatagramPacket(data, data.length, address,
				port);

		// 创建DatagramSocket对象
		DatagramSocket socket = new DatagramSocket();

		// 发送数据报
		socket.send(packet);

		/*
		 * 客户端接收服务器端的回应
		 */
		socket.receive(packet);

		String responseStr = new String(data, 0, packet.getLength());
		System.out.println("我是客户端,服务器端回应说:"   responseStr);

	}

}

0 人点赞