java网络编程(模拟群聊)

 代码:(个人所写仅供参考)

①客户端写入代码,此处将客户端中的Socket引入

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class writer_client extends Thread {
	Client_00 client;
	public  writer_client(Client_00 client) {
		this.client=client;
	}
	Scanner scan=new Scanner(System.in);
	@Override
	public void run() {
		while(true) {
		try {
			OutputStream os=client.s.getOutputStream();
			PrintStream ps=new PrintStream(os);
			System.out.println("请输入消息。。。");
			String str=scan.nextLine();
			ps.println(str);
			ps.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}
}

②客户端读取服务器部分代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class reader_client	extends Thread {
	Client_00 client;
	public reader_client(Client_00 client) {
		this.client=client;
	}
	@Override
	public void run() {
		while(true) {
			InputStream is;
			try {
				is = client.s.getInputStream();
				InputStreamReader isr=new InputStreamReader(is);
				BufferedReader br=new BufferedReader(isr);
				String str=br.readLine();
				System.out.println("向您发送消息:"+str);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
	}
}

3.客户端运行:

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

public class Client_00 {
	public  static Socket s;
	public static void main(String[] args) {
		Client_00 c=new Client_00();
		
		try {
			//链接服务器
			System.out.println("客户端请求链接服务其。。。");
			s=new Socket("localhost", 8888);
			System.out.println("服务器已连接!");
			writer_client wc_0=new writer_client(c);
			reader_client rc_01=new reader_client(c);
			wc_0.start();
			rc_01.start();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

1.服务器部分:

<1

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

public class ServerSocket_00 {
	public static List<Socket> list=new ArrayList<Socket>();
	public static Socket sk;
	public static void main(String[] args) {
		try {
			ServerSocket ss=new ServerSocket(8888);
			int i=1;
			while(true) {
				System.out.println("等待服务器联入。。。");
				sk =ss.accept();
				System.out.println("第"+i+"个客户端已经联入");
				list.add(sk);
				Sockets_00 skk=new Sockets_00(sk);
				skk.start();
				i++;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

<2socket线程部分

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;
//功能:对客户端发来的消息进行转发
public class Sockets_00 extends Thread{
	Socket sk;
	public Sockets_00(Socket sk) {
		this.sk=sk;
	}
	@Override
	public void run() {
		while(true) {
			try {
				InputStream is=sk.getInputStream();
				InputStreamReader isr=new InputStreamReader(is);
				BufferedReader br=new BufferedReader(isr);
				//进行消息的转发
				String str=br.readLine();
				for(Socket socket:ServerSocket_00.list) {
					OutputStream os=socket.getOutputStream();
					PrintWriter pw=new PrintWriter(os);
					if(sk.equals(socket))
						{continue;}
					pw.println(str);
					pw.flush();
				}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值