欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多客户端网络通信

程序员文章站 2024-03-22 23:35:22
...

首先写一个继承Thread的线程类
在其中处理客户端请求

package tcpscoket_obj_29;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;

//线程类:处理客户请求
public class LoginThread extends Thread{

	Socket socket=null;
	//通过构造方法完成对Socket的接收
	public LoginThread(Socket socket){
		this.socket=socket;
	}
	//处理客户端请求
	public void run(){
		try{
			//3.通过对象输入流获取用户请求信息(反序列化的过程)
			InputStream is=socket.getInputStream();
			ObjectInputStream ois=new ObjectInputStream(is);
			User user=(User)ois.readObject();
			System.out.println("客户端说:"+user.getUsername()+"-"+user.getPassword());
			
			//通过输出流给客户端一个响应
			OutputStream os=socket.getOutputStream();
			String reply="欢迎登录!";
			os.write(reply.getBytes());
			//4.关闭相应的数据流和Socket
			os.close();
			ois.close();
			is.close();
			socket.close();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
}

然后写服务器类
将监听客户端请求循环,并在监听到客户端请求后创建一个线程

package tcpscoket_obj_29;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


//客户端发送请求给服务器(多客户请求)
//(1)一直建通==监听客户端请求(2)一旦监听到一个客户端请求,就创建一个线程并启动
public class LoginServer {

	public static void main(String[] args) throws ClassNotFoundException {
		// TODO Auto-generated method stub

		try {
			//1.创建一个服务器端Socket对象
			ServerSocket serverSocket=new ServerSocket(5000);
			while(true){
			//2.使用accept()方法来监听用户请求,创建链接
			Socket socket=serverSocket.accept();
			LoginThread thread=new LoginThread(socket);
			thread.start();
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

下面是多个客户端类
LoginClient

package tcpscoket_obj_29;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class LoginClient {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {
			//1.创建客户端Socket对象
			Socket socket=new Socket("localhost",5000);
			//2.创建输出流对象
			OutputStream os=socket.getOutputStream();
//			//3.通过对象输出流将用户对象序列化 ,发送请求给服务器
			ObjectOutputStream oos=new ObjectOutputStream(os);
			User user=new User("admin","123456");
			oos.writeObject(user);
//			String loginInfo="用户名:admin:密码:123456";
//			//把字符串打散成字节数组
//			os.write(loginInfo.getBytes());
			
			
			//通过输入流接收服务器返回给客户端的响应
			socket.shutdownOutput();
			InputStream is=socket.getInputStream();
			BufferedReader br=new BufferedReader(new InputStreamReader(is));
			String reply;
			while ((reply=br.readLine())!=null) {
				System.out.println("服务端说:"+reply);
			}
			
			
			//4.关闭流及Socket对象
			br.close();
			is.close();
			oos.close();
			os.close();
			socket.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

LoginClient1

package tcpscoket_obj_29;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class LoginClient1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {
			//1.创建客户端Socket对象
			Socket socket=new Socket("localhost",5000);
			//2.创建输出流对象
			OutputStream os=socket.getOutputStream();
//			//3.通过对象输出流将用户对象序列化 ,发送请求给服务器
			ObjectOutputStream oos=new ObjectOutputStream(os);
			User user=new User("lisi","123456");
			oos.writeObject(user);
//			String loginInfo="用户名:admin:密码:123456";
//			//把字符串打散成字节数组
//			os.write(loginInfo.getBytes());
			
			
			//通过输入流接收服务器返回给客户端的响应
			socket.shutdownOutput();
			InputStream is=socket.getInputStream();
			BufferedReader br=new BufferedReader(new InputStreamReader(is));
			String reply;
			while ((reply=br.readLine())!=null) {
				System.out.println("服务端说:"+reply);
			}
			
			
			//4.关闭流及Socket对象
			br.close();
			is.close();
			oos.close();
			os.close();
			socket.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

先运行服务器类,再依次运行客户端类

相关标签: Java socket java