正文

服务器 客户端 聊天室2006-06-23 18:13:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/16135.html

分享到:

//

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class charroomserver {
//----------------------------------------------------------****************定义变量
//JFrame
JFrame charFrame;
JTextArea charText;
JTextField charField2;
JButton charButton2;
ScrollPane sPanel;
//JPanel
JPanel jPanel1,jPanel2;
//端口号and net
public final static int DEFAULT_PORT=6666;//默认端口号
ServerSocket serverSock;
Socket clientSock;
//index记录当前对象
int index=0;
//线程,对象
ChatAcceptThread chatAcceptThread[]=new ChatAcceptThread[50];//对象数组

//_________________________________________________________________________________

//------------------------------------------------------------***************方法

//服务端图形
void gUi(){
//new
 charFrame=new JFrame("聊天室服务端");
 jPanel2=new JPanel();
 charButton2=new JButton("退出");
 charText=new JTextArea(20,20);
 sPanel=new ScrollPane();
 
 charField2=new JTextField(20);
 Container chartain=charFrame.getContentPane();
//add
    sPanel.add(charText);charText.setLineWrap(true);
    jPanel2.add(charButton2);
    jPanel2.add(charField2);
// chartain.add(jPanel1,BorderLayout.NORTH);
 chartain.add(sPanel,BorderLayout.CENTER);
 chartain.add(jPanel2,BorderLayout.SOUTH);
 
 charFrame.setBounds(200,200,500,400);
 charFrame.setVisible(true);
 charFrame.pack();
 charFrame.show();
//-------------------------------------------------------***************监听

//_________________________________________________________________________________
}
//监听
void serverListen(){
try{
   serverSock=new ServerSocket(DEFAULT_PORT);
   while(true){
  clientSock=serverSock.accept();
 
 
  chatAcceptThread[index]=new ChatAcceptThread(clientSock);//启动接受连接的线程
  chatAcceptThread[index].start();
  index++;
  }
  }catch(IOException e){
  }
 
}
//_________________________________________________________________________________
public  charroomserver(){
 gUi();
 serverListen();//服务器开始监听

 }
//----------------------------------------------------------************内部类
//线程,多用户连接
 class ChatAcceptThread extends Thread{
  Socket clientSock2;
  BufferedReader in;
  PrintWriter out;
public  ChatAcceptThread(Socket upSock){
 clientSock2=upSock;
 }
//run
public void run(){
  try{
   
    in=new BufferedReader(new InputStreamReader(clientSock2.getInputStream()));
    out=new PrintWriter(clientSock2.getOutputStream());
  while(true){
    String str=in.readLine();
//    System.out.println(str);
    charText.append(str+"\n");
    for(int i=0;i<index;i++)
{  
    if(str.equals("end"))
      break;
     
    chatAcceptThread[i].out.println(str);
    chatAcceptThread[i].out.flush(); }
   
  
   }
   }
   catch(Exception e){
   }
}
}
//______________________________________________________________________________
public static void main(String args[]){
  new charroomserver();
 }
}
//

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class charroom implements Runnable{
//----------------------------------------------------------****************定义变量
//JFrame
JFrame charFrame;
JTextArea charText;
JTextField charField1,charField2,charField3,charField4;
JButton charButton1,charButton2,charButton3;
ScrollPane sPanel;
JLabel charLabel;
//JPanel
JPanel jPanel1,jPanel2;
//Socket变量
Socket server,socket;
//文件,io
BufferedReader in;
PrintWriter out;
BufferedReader wt;
//Thread
Thread charThread=new Thread(this);


//_________________________________________________________________________________

public  charroom(){
 gUi();
 }

//------------------------------------------------------------***************方法
//客服端图形
void gUi(){
//new
 charFrame=new JFrame("聊天室客户端");
 jPanel1=new JPanel();
 jPanel2=new JPanel();
 charLabel=new JLabel("你对");
 charButton1=new JButton("连接");
 charButton2=new JButton("发送");
 charButton3=new JButton("退出");
 charText=new JTextArea(20,20);
 charText.setLineWrap(true);
 charText.setWrapStyleWord(true);
 sPanel=new ScrollPane();
 
 charField1=new JTextField(20);
 charField2=new JTextField(20);
 charField3=new JTextField("昵称",10);
 charField4=new JTextField("大家",10);
 Container chartain=charFrame.getContentPane();
//add
    jPanel1.add(charField3);
    jPanel1.add(charButton1);
    jPanel1.add(charField1);
    jPanel1.add(charButton3);
    sPanel.add(charText);
   
    jPanel2.add(charLabel);
    jPanel2.add(charField4);
    jPanel2.add(charField2);
    jPanel2.add(charButton2);
 chartain.add(jPanel1,BorderLayout.NORTH);
 chartain.add(sPanel,BorderLayout.CENTER);
 chartain.add(jPanel2,BorderLayout.SOUTH);
 
 charFrame.setBounds(200,200,500,400);
 charFrame.setVisible(true);
 charFrame.pack();
 charFrame.show();
//------------------------------------------------------------***********监听
 //发送
  charButton2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    send();
   }
  });
 //连接
 charButton1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    connect();
   }
  });
 //退出
 charButton1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
   out();
  }
  });
 
//_________________________________________________________________________________
 
 }
//_________________
 //Run
public void run(){
try{while(true){

 String str=in.readLine();
    charText.append(str+"\n");
}
 
    }
catch(Exception e){
}   
    }
//联接connect()
public void connect(){
  try{
  server=new Socket(InetAddress.getByName(charField1.getText()).getHostAddress(),6666);
  if(server!=null){//connection successed
    charText.setText(charField3.getText()+"连接成功!");
   }

//    server=new Socket(InetAddress.getLocalHost(),6666);
  in=new BufferedReader(new InputStreamReader(server.getInputStream()));
  out=new PrintWriter(server.getOutputStream());
  wt=new BufferedReader(new InputStreamReader(System.in));
 
  charThread.start();
  }
  catch(Exception e){
  }
 
 
}
//发送send
void send(){
 out.println(charField3.getText()+"对大家说:"+charField2.getText());
 out.flush();
 charField2.setText("");
}
//退出方法
void out(){
 try
  { server.close();
    System.exit(0);}
  catch(Exception event){
   
  }
}


//_________________________________________________________________________________

 public static void main(String args[]){
  new charroom();
 }
}

阅读(4453) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册