java聊天室 博客_java网络聊天室

本文介绍了使用Java构建一个简单的网络聊天室,支持多用户登录并进行群聊。当用户登录或退出时,系统会通知所有在线用户并更新在线用户列表。代码中包括服务器端的实现,通过ServerSocket接收客户端连接,维护用户列表并广播消息。
摘要由CSDN通过智能技术生成

功能简述:

1.可以多用户登陆聊天室,用户可以选择私聊或者对所有人聊天。(这里时间关系仅实现群聊,私聊只要将私聊消息和群聊消息封装开来即可实现)

2.有人登陆聊天室时由系统通知所有在线用户,并刷新在线用户列表。

3.有人退出聊天室时由系统通知所有在线用户,并刷新在线用户列表。

登陆框:

a4c26d1e5885305701be709a3d33442f.png

聊天窗口

a4c26d1e5885305701be709a3d33442f.png

服务器控制台信息

a4c26d1e5885305701be709a3d33442f.png

服务器代码:

package exercise.chatroom;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.EOFException;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.SocketException;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JButton;

public class ChatServer {

List

clients = new

ArrayList();

Client c = null;

ArrayList

userList = new

ArrayList();

public static void main(String[] args){

new

ChatServer().start();

}

public void start(){

boolean started = false;

ServerSocket ss = null;

DataInputStream dis =

null;

try{

ss = new

ServerSocket(8888);

started =

true;

System.out.println("服务器上线了!");

}catch(Exception e) {

e.printStackTrace();

}

try{

while(started){

Socket s = ss.accept();

c = new Client(s);//启动线程,实行run()方法

System.out.println("有一客户端登陆了!");

new

Thread(c).start();//启动start方法,循环.start是Thread中的方法与这上面的start无关

clients.add(c);

//dis.close();

}

}catch(Exception e) {

//e.printStackTrace();

}

finally{

try {

ss.close();

} catch

(IOException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

}

}

class Client implements Runnable {

private Socket s;

private DataInputStream dis

=null;

private DataOutputStream

dos=null;

private boolean bConnected

=false;

public Client(Socket s){

this.s=s;

try {

dis= new DataInputStream(s.getInputStream());

dos =new DataOutputStream(s.getOutputStream());

bConnected =true;

}catch

(IOException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

}

public void send(String

str)throws Exception{

dos.writeUTF(str);

}

public void run(){

try{

while(bConnected){

String

str = dis.readUTF();

if(str.charAt(0)

== 'L'){

userList.add("X"+str.substring(1));

String

str1 = "R" + "[系统消息]:" + str.substring(1) + "上线了!";

System.out.println(str1.substring(1));

for(int

i=0;i

c

= clients.get(i);

c.send(str1);

}

System.out.print("目前在线用户[

");

for(int

j=0;j

String

s = userList.get(j);

System.out.print(s.substring(1)+"

");

for(int

i=0;i

c

= clients.get(i);

c.send(s);

}

}

System.out.println("]");

}

else

if(str.charAt(0) == 'S'){

userList.remove("X"+str.substring(1));

String

str1 = "R" + "[系统消息]:" + str.substring(1) + "下线了!";

System.out.println(str1.substring(1));

for(int

i=0;i

c

= clients.get(i);

c.send(str1);

}

System.out.print("目前在线用户[

");

for(int

j=0;j

String

s = userList.get(j);

System.out.print(s.substring(1)+"

");

for(int

i=0;i

c

= clients.get(i);

c.send(s);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
部分代码如下:client: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package client; /** * * @author Administrator */ import java.awt.*; import java.io.*; import java.net.*; import java.applet.*; import java.util.Hashtable; public class ClientChat extends Applet implements Runnable { Socket socket=null; DataInputStream in=null; DataOutputStream out=null; InputNameTextField 用户提交昵称界面=null; ChatArea 用户聊天界面=null; Hashtable listTable; Label 提示条; Panel north, center; Thread thread; public void init() { int width=getSize().width; int height=getSize().height; listTable=new Hashtable(); setLayout(new BorderLayout()); 用户提交昵称界面=new InputNameTextField(listTable); int h=用户提交昵称界面.getSize().height; 用户聊天界面=new ChatArea("",listTable,width,height-(h+5)); 用户聊天界面.setVisible(false); 提示条=new Label("正在连接到服务器,请稍等...",Label.CENTER); 提示条.setForeground(Color.red); north=new Panel(new FlowLayout(FlowLayout.LEFT)); center=new Panel(); north.add(用户提交昵称界面); north.add(提示条); center.add(用户聊天界面); add(north,BorderLayout.NORTH); add(center,BorderLayout.CENTER); validate(); } public void start() { if(socket!=null&&in!=null&&out!=null) { try { socket.close(); in.close(); out.close(); 用户聊天界面.setVisible(false); } catch(Exception ee) { } } try { socket = new Socket(this.getCodeBase().getHost(), 6666); in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); } catch (IOException ee) { 提示条.setText("连接失败"); } if(socket!=null) { InetAddress address=socket.getInetAddress(); 提示条.setText("连接:"+address+"成功"); 用户提交昵称界面.setSocketConnection(socket,in,out); north.validate(); } if(thread==null) { thread=new Thread(this); thread.start(); } } public void stop() { try { socket.close(); thread=null; } catch(IOException e) { this.showStatus(e.toString()); } } public void run() { while(thread!=null) { if(用户提交昵称界面.get能否聊天()==true) { 用户聊天界面.setVisible(true); 用户聊天界面.setName(用户提交昵称界面.getName()); 用户聊天界面.setSocketConnection(socket,in,out); 提示条.setText("祝聊天愉快!"); center.validate(); break; } try { Thread.sleep(100); } catch(Exception e) { } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值