Java编写简单的聊天室

客户端:

package chatroot;



import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;


public class ChatClient {
Socket socket=null;
//以线程方式启动客户端
public ChatClient() throws Exception{
socket =new Socket("127.0.0.1",8888);
new Thread(new ReceiveThread()).start();
}
//send方法
public void send(String str) throws IOException{
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
dos.writeUTF(str);
}
//关闭连接
public void disconnection() throws IOException{
if(socket!=null){
socket.close();
}
}
//内部类(多线程方式)
class ReceiveThread implements Runnable{


@Override
public void run() {
// TODO Auto-generated method stub
if(socket==null){
return ;
}
try {
DataInputStream dataInputStream=new DataInputStream(socket.getInputStream());
String str=dataInputStream.readUTF();
while(str!=null&&str.length()!=0){
System.out.println("别人说:"+str);
str=dataInputStream.readUTF();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
public static void main(String[] args) throws Exception {
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
ChatClient chatClient=new ChatClient();

System.out.println("请输入:");
String str=bReader.readLine();
while(str!=null&&str.length()!=0){
chatClient.send(str);
System.out.println("请输入:");
str=bReader.readLine();
System.out.println("自己说:"+str);
}
chatClient.disconnection();

}

}


浏览器端:

package chatroot;


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 Chatservlet {
private ServerSocket serverSocket=null;
//存客户端
private ArrayList list=new ArrayList();
//聊天室的服务端
public Chatservlet(int port) throws IOException{
serverSocket=new ServerSocket(port);
}
//启动聊天服务器
public void startServer() throws Exception{
System.out.println("服务器启动");
while(true){
Socket socket=serverSocket.accept();
list.add(new Clientconn(socket));
System.out.println("一个新客户端上线"+socket.getInetAddress());
System.out.println("当前客户端人数"+list.size());
}
}
//内部类,管理客户端的连接,以多线程方式
class Clientconn implements Runnable{
Socket s=null;
public Clientconn(Socket s) {
this.s=s;
new Thread(this).start();
}
//发送消息的方法
public void send(String str) throws IOException{
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
//关闭客户端连接的方法
public void dispose() throws IOException{
if(s!=null){
s.close();
}
list.remove(this);
System.out.println("一个客户端退出");
System.out.println("当前在线人数:"+list.size());
}
@Override
public void run() {
//发送消息
try {
DataInputStream dis=new DataInputStream(s.getInputStream());
//先读消息进来
String str=dis.readUTF();
while(str!=null&&str.length()!=0){
System.out.println("客户端说:"+str);
for(Clientconn cc:list){
if(this!=cc){
cc.send(str);
}
}
str = dis.readUTF();
}
this.dispose();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

public static void main(String[] args) throws Exception {
Chatservlet chatservlet=new Chatservlet(8888);
chatservlet.startServer();

}
}


你可能感兴趣的:(Java编写简单的聊天室)