java网络编程(模拟群聊)

java网络编程(模拟群聊)_第1张图片

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

①客户端写入代码,此处将客户端中的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 list=new ArrayList();
	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();
		}
		}
	}
}

 

你可能感兴趣的:(java网络编程(模拟群聊))