socket群聊通讯

server服务器:

package chatwithother;

import java.awt.List;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class server {
	private static ArrayList<Mycontain> all=new ArrayList<Mycontain>();
	private static int count=0;
	public static void main(String[] args) throws IOException{
		new server().start();
	}
	public static void start() throws IOException{
		ServerSocket server =new ServerSocket(9999);
		System.out.println("服务器已经建立,正在等待连接...");
		while(true){
			Socket client =server.accept();
			count++;
			System.out.println("已经有"+count+"位用户连接服务器");
			Mycontain channel =new Mycontain(client);
			all.add(channel);
			new Thread(channel).start(); 
		}
	}
	static class Mycontain implements Runnable{
		private ServerSocket ser;
		private DataInputStream dis;
		private DataOutputStream dos;
		private String name;
		public String rece() throws IOException{//从流中读取数据
			String str="";
			str=dis.readUTF();
			return str;
			
		}
		public Mycontain(Socket client){
			try {
				dis = new DataInputStream(client.getInputStream());
				dos = new DataOutputStream(client.getOutputStream());				
				this.name =dis.readUTF();				
				this.send("欢迎您进入聊天室");
				sendOthers("进入了聊天室");
			} catch (IOException e) {
			}
		}
		private void sendOthers(String msg) throws IOException {
			for(Mycontain i:all){
				if(i==this){
					continue;
				}
				else{
					i.send("["+name+"]"+msg);
				}
			}
			
			
		}
		private void send(String msg) throws IOException {
			dos.writeUTF(msg);
			dos.flush();
			
		}
		public void run() {
			while(true){
				try {
					sendOthers(rece());
				} catch (IOException e) {
					
				}
			}
		}
	}
}


client客户端:

package chatwithother;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class client {
	private static String name;
	private static BufferedReader br;
	public static void main(String[] args) throws UnknownHostException, IOException{
		//InetAddress host=InetAddress.getByName("10.12.64.232");
		Socket Client=new Socket("localhost",9999);
		System.out.println("请输入你的id:");
		br=new BufferedReader(new InputStreamReader(System.in));
		name=br.readLine();
		if(name==null||name.equals("")){
			return;
		}
		new Thread(new SendMsg(Client,name)).start();
		new Thread(new ReceiveMsg(Client)).start();
		
	}
}

SendMsg发送信息:

package chatwithother;

import java.awt.List;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;

public class SendMsg implements Runnable {
	private DataInputStream in;
	private BufferedReader br;
	private DataOutputStream dos;
	private String name;
	private Socket client;
	public void run() {
		while(true){
			try {
				send(getMsg());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public SendMsg(){
		br=new BufferedReader(new InputStreamReader(System.in));
	}
	public SendMsg(Socket client,String name) throws IOException{
		this();
		this.name=name;
		in=new DataInputStream(client.getInputStream());
		dos=new DataOutputStream(client.getOutputStream());
		send(this.name);
	}
	public String getMsg() throws IOException{
		String msg="";
		msg=br.readLine();
		return msg;
	}
	public void send(String msg) throws IOException{
		if(msg!=null&&!msg.equals("")){
			dos.writeUTF(msg);
			dos.flush();
		}

	}
	
}

ReceiveMsg接受信息:

package chatwithother;

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

public class ReceiveMsg implements Runnable{
	private Socket client;
	private DataInputStream in;
	public ReceiveMsg(Socket client) throws IOException{
		this.client=client;
		
		
	}
	public String receive() throws IOException{
		in=new DataInputStream(client.getInputStream());
		String msg="";
		msg=in.readUTF();
		return msg;
		
	}
	public void run() {
		while(true){
			try {
				System.out.println(receive());
			} catch (IOException e) {
				//e.printStackTrace();
			}
		}
		
	}

}

运行结果:
服务器端:
在这里插入图片描述
客户端(注意有多个):
在这里插入图片描述
在这里插入图片描述
实验截图就不一一上了,欢迎留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值