ChatClient.java
import java.awt.*;
import java.io.*;
import java.*;
import java.applet.*;
import java.util.Hashtable;
public class ChatClient extends Applet implements Runnable{
  Socket socket=null;
  DataInputStream in=null;//读取服务器端发来的消息
  DataOutputStream out=null;//向服务器端发送的消息
  InputInfo 用户名提交界面=null;
  UserChat 聊天界面=null;
  Hashtable listTable;//用于存放在线用户的用户名的散列表
  Label 提示条;
  Panel north,center;
  Thread thread;
 
  public void init(){
      setSize(1000,800);
      int width=getSize().width;
      int height=getSize().height;
      listTable=new Hashtable();
      setLayout(new BorderLayout());
      用户名提交界面=new InputInfo(listTable);
      int h=用户名提交界面.getSize().height;
      聊天界面=new UserChat("",listTable,width,height-(h+5));
      聊天界面.setVisible(false);
      提示条=new Label("正在连接到服务器...",Label.CENTER);
      提示条.d);
      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 CodeBase().getHost(),6666);
          in=new InputStream());
          out=new OutputStream());
      }
      catch(IOException ee){
          提示条.setText("连接失败");
      }
      //客户端成功连接服务器端
      if(socket!=null){
          InetAddress InetAddress();
          提示条.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.String());
      }
  }
  public void run(){
      while(thread!=null){
          if(用户名提交界面.getchatornot()==true){
              聊天界面.setVisible(true);
              聊天界面.setName(用户名提交界面.getName());
              聊天界面.setSocketConnection(socket,in,out);
              提示条.setText("祝聊天快乐!");
              center.validate();
              break;
          }
          try{
              Thread.sleep(100);
          }
          catch(Exception e){
          }
      }
  }
}
ChatMain.java
import java.io.*;
import java.*;
import java.util.*;
public class ChatMain {
    public static void main(String args[]) {
      ServerSocket server=null;
      Socket you=null;
      Hashtable peopleList;
      peopleList=new Hashtable();
      while(true){
          try{
              //服务器端在端口6666处监听来自客户端的信息
              server=new ServerSocket(6666);
          }
          catch(IOException e1){
socket编程聊天室基本流程              System.out.println("正在监听");
          }
          try{
              //当服务器端接收到客户端的消息后,取得客户端的IP地址。
              you=server.accept();
              InetAddress LocalAddress();
              System.out.println("用户名的IP:"+address);
          }
          catch(IOException e){
             
          }
          //为每一个客户端简历一个线程,用来记录客户端用户的信息
          if(you!=null){
              Server_thread peopleThread=new Server_thread(you,peopleList);
              peopleThread.start();
          }
          else{
              continue;
          }
      }
    }
}
InputInfo.java
import java.awt.*;
import java.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
public class InputInfo extends Panel implements ActionListener,Runnable{
    TextField nameFile=null;
    String name=null;
    Checkbox male=null,female=null;
    CheckboxGroup group=null;
    Button 进入聊天室=null,退出聊天室=null;
    Socket socket=null;
    DataInputStream in=null;
    DataOutputStream out=null;
    Thread thread=null;
    boolean chatornot=false;
    Hashtable listTable;
   
    public InputInfo(Hashtable listTable){
        this.listTable=listTable;
        nameFile=new TextField(10);
        group=new CheckboxGroup();
        male=new Checkbox("男",true,group);
        female=new Checkbox("女",false,group);
        进入聊天室=new Button("进入");
        退出聊天室=new Button("退出");
        进入聊天室.addActionListener(this);
        退出聊天室.addActionListener(this);
        thread=new Thread(this);
        add(new Label("用户名:"));
        add(nameFile);
        add(male);
        add(female);
        add(进入聊天室);
        add(退出聊天室);
        退出聊天室.setEnabled(false);
    }
    public void setchatornot(boolean b){

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。