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();
}
}
}
}
运行结果:
服务器端:
客户端(注意有多个):
实验截图就不一一上了,欢迎留言