socket实现双向通信(一对多)

实现一个服务器连接多个客户端需要使用到多线程

1.服务端

package jichu;

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

public class socket2 {
public static void main(String[] args) {
	
		//实例化对象
		ServerSocket serversocket;
		try {
			serversocket = new ServerSocket(7777);
			//提示用户
			System.out.println("服务端已启动~~~");
			while(1==1){
			//启动侦听
			Socket socket = serversocket.accept();
			//将这个socket对象传递过去给多线程类
			SocketThread st = new SocketThread(socket);
			new Thread(st).start();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

2.线程类

package jichu;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
/*处理socket
 * 处理数据的读和写的操作*/
public class SocketThread implements Runnable{
    private Socket socket;
    //构造方法中的socket就是使用这个socketThread类对象的时候传递过来给他的
	public SocketThread(Socket socket) {
		super();
		this.socket = socket;
	};
	public void run() {
		while(1==1){
		//服务端这里的socket也是一个socket
		InputStream in;
		try {
			in = socket.getInputStream();
			//这里得到一个datainputstream
			DataInputStream dis = new DataInputStream(in);
			//通过dis来读取数据
			String str = dis.readUTF();
			System.out.println("服务器读取客户端的:"+str);
			
			//服务器端,连接成功后,接收到客户端的消息,就发送一条消息
			OutputStream out = socket.getOutputStream();
			//dataOutputSream
			DataOutputStream dos = new DataOutputStream(out);
			if(str.equals("")){
				//服务器接收到了数据之后没要发送给客户端一条数据
			/*	dos.writeUTF("土豆土豆,我是地瓜~");
			}else{
				dos.writeUTF("土豆你在哪~");*/
				Scanner scanner = new Scanner(System.in);
				dos.writeUTF(scanner.nextLine());
			}else{
				Scanner scanner = new Scanner(System.in);
				dos.writeUTF(scanner.nextLine());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//释放资源
		}
		}
	}
}
3.客户端
package jichu;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class client2 {
	public static void main(String[] args) {
		try {
			// 创建客户端得socket对象,连接的是本机所以ip地址为127.0.0.1
			// 端口号为服务端的端口号
			Socket client = new Socket("127.0.0.1", 7777);
			while(1==1){
			// 输出流
			OutputStream out = client.getOutputStream();
			// DataOutputStream
			DataOutputStream dos = new DataOutputStream(out);
			// dos执行写的操作
			//dos.writeUTF("hello");
			//用户从控制台输入
			Scanner scanner = new Scanner(System.in);
			dos.writeUTF(scanner.nextLine());
			//客户端接收服务器端传递过来的字符串
			InputStream in = client.getInputStream();
			//创建dataInputStream
			DataInputStream dis = new DataInputStream(in);
			//读取服务器传递过来的信息
			System.out.println("客户端读取服务端传递过来的信息:"+dis.readUTF());
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(socket实现双向通信(一对多))