简单的java聊天室,出了点问题,欢迎帮忙,哈哈
来源:互联网 发布:禁止windows更新驱动 编辑:程序博客网 时间:2024/05/11 19:14
package muyizhou.chat;
import java.io.*;
import java.net.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.util.*;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.*;
public class CServer extends JFrame{
//保存当前处理客户端请求的处理器线程
static Vector clientProcessors=new Vector(10);
//当前的连接数
static int activeConnects=0;
//构造方法
public CServer(){
}
//主方法
public static void main(String args[]){
CServer chatServer1=new CServer();
chatServer1.setVisible(true);
System.out.println("server starting........");
Socket socket=null;
ServerSocket server=null;
try{
//服务器端开始侦听
server=new ServerSocket(5000);
}catch(IOException e){
System.out.println("Error"+e);
System.exit(1);
}
while(true){
try{
socket=server.accept();
if(socket!=null)
{
System.out.println(socket+"连接");
}
}catch(IOException e){
System.out.println("Error"+e);
}
ClientProcessor c=new ClientProcessor(socket);
c.start();
//sendMsgToClients(msg);
}
}
//关闭所有连接
public static void closeAll(){
while(clientProcessors.size()>0){
ClientProcessor c=(ClientProcessor) clientProcessors.firstElement();
try{
//关闭socket连接和处理线程
c.socket.close();
c.toStop();
}catch(IOException e){
System.out.println("Error:"+e);
}finally{
clientProcessors.removeElement(c);
}
}
}
//向所有用户群发消息
public static synchronized void sendMsgToClients(String msg){
//msg=null;
for(int i=0;i<clientProcessors.size();i++){
ClientProcessor c=(ClientProcessor) clientProcessors.elementAt(i);
System.out.println("send msg"+msg);
c.send(msg);
}
}
public static void disConnect(ClientProcessor client){
disConnect(client,true);
}
public static synchronized void disConnect(ClientProcessor client,
boolean toRemoveFromList){
try{
client.socket.close();
}catch(IOException e){
System.out.println("Error"+e);
}
}
}
//处理客户端发送的请求的线程
class ClientProcessor extends Thread{
Socket socket;
BufferedReader br;
PrintWriter pw;
boolean running=true;
String usr=null;
public void send(String msg){
usr=msg;
try{
usr=br.readLine();
}catch(IOException e){
System.out.println("Error:"+e);
CServer.disConnect(this);
}
pw.println(msg);
}
//构造方法
public ClientProcessor(Socket s){
socket=s;
try{
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw=new PrintWriter(socket.getOutputStream());
//usr=br.readLine();
}catch(IOException e){
System.out.println("Error:"+e);
}
}
public void run(){
while(running){
String line=null;
try{
line=br.readLine();
System.out.println(line);
}catch(IOException e){
System.out.println("Error:"+e);
CServer.disConnect(this);
}
CServer.sendMsgToClients(line);
}
}
public void toStop(){
running=false;
}
}
客户端
package book.net.chat;
import java.io.*;
import java.net.Socket;
import java.net.InetAddress;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.StringTokenizer;
import javax.swing.border.TitledBorder;
public class CClient extends JFrame implements ActionListener{
JLabel nameLabel=new JLabel();
JLabel ipLabel=new JLabel();
JTextField nameTextField=new JTextField(15);
JTextField ipTextField=new JTextField(15);
JTextField msgTextField=new JTextField(30);
JButton connectButton=new JButton();
JButton disconnectButton=new JButton();
JTextArea chatContentTextArea=new JTextArea(15,40);
JButton sendMsgButton=new JButton();
JLabel msgLabel=new JLabel();
java.awt.List peopleList=new java.awt.List(15);
Socket socket=null;
PrintStream ps=null;
ClientListener listener=null;
//构造函数
public CClient(){
init();
}
//图形界面
public void init(){
this.setTitle("聊天室客户端");
//初始化按钮和标签
nameLabel.setText("姓名");
ipLabel.setText("IP");
connectButton.setText("连接");
connectButton.addActionListener(this);
disconnectButton.setText("断开");
disconnectButton.addActionListener(this);
//设置聊天内容不可编辑
chatContentTextArea.setEditable(false);
sendMsgButton.setText("发送");
sendMsgButton.addActionListener(this);
msgTextField.setText("请输入聊天信息");
//pane1放置输入姓名和连接两个按钮
JPanel panel1=new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(nameLabel);
panel1.add(nameTextField);
panel1.add(ipLabel);
panel1.add(ipTextField);
panel1.add(connectButton);
panel1.add(disconnectButton);
//用于放置聊天信息显示和聊天人员列表
JPanel panel2=new JPanel();
panel2.setLayout(new FlowLayout());
JScrollPane pane1=new JScrollPane(chatContentTextArea);
pane1.setBorder(new TitledBorder(BorderFactory.createEtchedBorder
(Color.white,new Color(134,134,134)),"聊天内容"));
panel2.add(pane1);
JScrollPane pane2=new JScrollPane(peopleList);
pane2.setBorder(new TitledBorder(BorderFactory.createEtchedBorder
(Color.white,new Color(134,134,134)),"用户列表"));
panel2.add(pane2);
//用于放置发送信息区域
JPanel panel3=new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(msgLabel);
panel3.add(msgTextField);
panel3.add(sendMsgButton);
//将组件添加到界面
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(panel1,BorderLayout.NORTH);
this.getContentPane().add(panel2, BorderLayout.CENTER);
this.getContentPane().add(panel3, BorderLayout.SOUTH);
this.pack();
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
e.printStackTrace();
}
}
//处理按钮事件
public void actionPerformed(ActionEvent event){
Object source=event.getSource();
if(source==connectButton){
//如果点击连接按钮
if(socket==null){
try{
socket=new Socket(ipTextField.getText(),5000);
System.out.println(socket);
ps=new PrintStream(socket.getOutputStream());
//pw=new PrintWriter(socket.getOutputStream());
listener=new ClientListener(this,nameTextField.getText(),socket);
listener.start();
}catch(IOException e){
System.out.println("Error:"+e);
disconnect();
}
}
}else if(source==disconnectButton){
disconnect();
}else if(source==sendMsgButton){
String msg=msgTextField.getText();//nameTextField.getText()+InetAddress.getLocalHost().getHostAddress();+msgTextField.getText();
ps.println(msg);
ps.flush();
//StringBuffer msg=new StringBuffer(nameTextField.getText()
//).append("InetAddress.getLocalHost().getHostAddress()");
//pw.println(msg);//.append(msgTextField.getText()));
}
}
public void disconnect(){
if(socket!=null){
try{
socket.close();
listener.toStop();
socket=null;
}catch(IOException e){
System.out.println("Error:"+e);
}
}
}
public static void main(String args[]){
CClient client=new CClient();
client.setVisible(true);
}
//客户端线程用来监听服务器传来的 信息
class ClientListener extends Thread{
String name=null;
BufferedReader br=null;
PrintStream ps=null;
Socket socket=null;
CClient parent=null;
boolean running=true;
//构造方法
public ClientListener(CClient p,String n,Socket s){
//接受参数
parent=p;
name=n;
socket=s;
try{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
ps=new PrintStream(s.getOutputStream());
}catch(IOException e){
System.out.println("ERROR:"+e);
parent.disconnect();
}
}
//停止监听
public void toStop(){
this.running=false;
}
//线程运行方法
public void run(){
String msg=null;
while(running){
msg=null;
try{
//读取从服务器传来的信息
msg=br.readLine();
System.out.println("receive msg:"+msg);
parent.chatContentTextArea.append(msg);
}catch(IOException e){
System.out.println("ERROR:"+e);
parent.disconnect();
}
}
}
}
}
- 简单的java聊天室,出了点问题,欢迎帮忙,哈哈
- 简单的java聊天室
- java简单的聊天室
- csdn的mySQL数据库出了点问题
- java的简单聊天室程序
- 一个简单的java聊天室
- java简单的控制台聊天室
- Java编写简单的聊天室
- SQL Server2000 企业版的一些简单问题,希望会的人帮忙一下,先谢谢了
- proteus仿真出了点小问题
- 我用MailCode出了点问题
- android,新手出了点小问题
- 安装软件时出了个怪问题,我截了个图。大家帮忙看看
- 今天的任务提前完成了,写点流水账,哈哈
- 朋友要招几个java,让帮忙出点面试题目
- 初接触c语言,请大家帮忙看看问题出在哪了!
- 刚学java 遇到了问题,大家帮忙。。
- java写的简单的聊天室
- Adobe推出Web版Photoshop 可免费使用
- VB 枚举窗口和关闭程序窗口的代码
- 资源共享优势互补 大连IT产业说明会首尔举行
- FLASH赋值 寻找是不是在框架中 国际化
- 原HP中国总裁孙振耀出山 任扬智、海辉董事长?
- 简单的java聊天室,出了点问题,欢迎帮忙,哈哈
- 升级Zend Framework 1.5+ 版本 Pdo_Oci 注意事项
- 美军“黑客”部队初具作战规模
- .com/.net域名注册费10月提高 近增0.4美元
- 心情不好 多吃香蕉或葡萄
- 替换多语言的出现的问题,格式化,DataGrid中的
- 替换多语言的出现的问题,格式化,DataGrid中的
- 自4月1日起阿里妈妈网络广告交易免费
- 中移动3G第一阶段全业务曝光 含34个增值业务