admin 管理员组

文章数量: 1184232


2024年2月24日发(作者:kafka groupid作用)

import .*;

import .*;

import .*;

import .*;

import ble;

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("正在连接到服务器...",);

提示条.setForeground();

north=new Panel(new FlowLayout());

center=new Panel();

(用户名提交界面);

(提示条);

(聊天界面);

add(north,);

add(center,);

validate();

}

public void start(){

if(socket!=null&&in!=null&&out!=null){

try{

();

();

();

聊天界面.setVisible(false);

}

catch(Exception ee){}

}

//客户端与服务器端简历连接

try{

socket=new Socket(eBase().getHost(),6666);

in=new DataInputStream(utStream());

out=new DataOutputStream(putStream());

}

catch(IOException ee){

提示条.setText("连接失败");

}

//客户端成功连接服务器端

if(socket!=null){

InetAddress address=tAddress();

提示条.setText("连接:"+address+"成功");

用户名提交界面.setSocketConnection(socket,in,out);

te();

}

if(thread==null){

thread=new Thread(this);

();

}

}

public void stop(){

try{

();

thread=null;

}catch(IOException e){

atus(ng());

}

}

public void run(){

while(thread!=null){

if(用户名提交界面.getchatornot()==true){

聊天界面.setVisible(true);

聊天界面.setName(用户名提交界面.getName());

聊天界面.setSocketConnection(socket,in,out);

提示条.setText("祝聊天快乐!");

te();

break;

}

try{

(100);

}

catch(Exception e){

}

}

}

}

import .*;

import .*;

import .*;

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){

n("正在监听");

}

try{

//当服务器端接收到客户端的消息后,取得客户端的IP地址。

you=();

InetAddress address=alAddress();

n("用户名的IP:"+address);

}

catch(IOException e){

}

//为每一个客户端简历一个线程,用来记录客户端用户的信息

if(you!=null){

Server_thread peopleThread=new Server_thread(you,peopleList);

();

}

else{

continue;

}

}

}

}

import .*;

import .*;

import .*;

import .*;

import ble;

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){

ble=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){

chatornot=b;

}

public boolean getchatornot(){

return chatornot;

}

public String getName(){

return name;

}

public void setName(String s){

name=s;

}

public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out){

=socket;

=in;

=out;

try{

();

}

catch(Exception e){

t(""+e);

}

}

public Socket getSocket(){

return socket;

}

//通过ActionEvent的getSource()方法判断哪个按钮被按下

//并根据用户不用的命令做出相应的处理

public void actionPerformed(ActionEvent e){

if(rce()==进入聊天室){

退出聊天室.setEnabled(true);

if(chatornot==true){

t("您正在聊天:"+name);

}else{

e(t());

String sex=ectedCheckbox().getLabel();

if(socket!=null&&name!=null){

try{

TF("姓名:"+name+"性别:"+sex);

}

catch(IOException ee){

t("没有连通服务器"+ee);

}

}

}

}

if(rce()==退出聊天室){

try{

TF("用户离开:");

}

catch(IOException ee){

}

}

}

//读取服务器端发来的消息,根据消息来判断用户是否能聊天或显示聊天者信息

public void run(){

String message=null;

while(true){

if(in!=null){

try{

message=F();

}catch(IOException e){

t("和服务器断开"+e);

}

}

if(With("可以聊天:")){

chatornot=true;

break;

}

else if(With("聊天者:")){

String people=ing(f(":")+1);

(people, people);

}

else if(With("不可以聊天:")){

chatornot=false;

t("该用户名已被占用");

}

}

}

}

Server_

import putStream;

import tputStream;

import ;

import ption;

import ;

import ation;

import ble;

public class Server_thread extends Thread{

String name=null,sex=null;

Socket socket=null;

File file=null;

DataOutputStream out=null;

DataInputStream in=null;

Hashtable peopleList=null;

Server_thread(Socket t,Hashtable list){

peopleList=list;

socket=t;

try{

in=new DataInputStream(utStream());

out=new DataOutputStream(putStream());

}catch(IOException e){ }

}

public void run(){

while(true){

String s=null;

try{

s=F();

//读取客户端发送的消息,该消息中包含用户提交的个人信息及聊天内容等

//服务器端根据该消息判断用户名时候重复

if(With("姓名:")){

name=ing(f(":")+1, f("性别"));

sex=ing(dexOf(":")+1);

boolean boo=nsKey(name);

//如果用户名不重复,则用户可以进入聊天室

if(boo==false){

(name, this);

TF("可以聊天:");

Enumeration enums=ts();

while(eElements()){

Server_thread th=(Server_thread)ement();

TF("聊天者:"+name+"性别"+sex);

if(th!=this){

TF("聊天者:"++"性别"+);

}

}

}

//如果用户名重复,则用户名不可以进入聊天室

else{

TF("不可以聊天:");

}

}

//用户所发送的聊天内容为公共聊天内容

else if(With("公共聊天内容:")){

String message=ing(f(":")+1);

Enumeration enums=ts();

while(eElements()){

((Server_thread)ement()).TF("聊天内容:"+message);

}

}

else if(With("用户离开:")){

}

}

}

Enumeration enums=ts();

while(eElements()){

try{

Server_thread th=(Server_thread)ement();

if(th!=this&&e()){

TF("用户离线:"+name);

}

}catch(IOException eee){

}

}

(name);

();

n(name+"用户离开了");

break;

}

//用户所发送的聊天内容为私人聊天内容

else if(With("私人聊天内容")){

String 悄悄话=ing(f(":")+1, f("#"));

String toPeople=ing(f("#")+1);

Server_thread toThread=(Server_thread)(toPeople);

if(toThread!=null){

TF("私人聊天内容:"+悄悄话);

}

else{

TF("私人聊天内容:"+toPeople+"已经离线");

}

}

}

catch(IOException ee){

Enumeration enums=ts();

while(eElements()){

try{

Server_thread th=(Server_thread)ement();

if(th!=this&&e()){

TF("用户离线:"+name);

}

}catch(IOException eee){

}

}

(name);

try{

();

}

catch(IOException eee){

}

n(name+"用户离开了");

break;

}

import .*;

import .*;

import .*;

import .*;

import ble;

public class UserChat extends Panel implements ActionListener,Runnable{

Socket socket=null;

DataInputStream in=null;

DataOutputStream out=null;

Thread threadMessage=null;

TextArea 谈话显示区,私聊显示区=null;

TextField 送出信息=null;

Button 确定,刷新谈话区,刷新私聊区;

Label 提示条=null;

String name=null;

Hashtable listTable;

List listComponent=null;

Choice privateChatList;

int width,height;

public UserChat(String name,Hashtable listTable,int width,int height){

setLayout(null);

setBackground();

=width;

=height;

setSize(width+350,height);

ble=listTable;

=name;

threadMessage=new Thread(this);

谈话显示区=new TextArea(20,20);

私聊显示区=new TextArea(20,20);

确定=new Button("发送");

刷新谈话区=new Button("刷新谈话区");

刷新私聊区=new Button("刷新私聊区");

提示条=new Label("双击可私聊",);

送出信息=new TextField(28);

确定.addActionListener(this);

送出信息.addActionListener(this);

刷新谈话区.addActionListener(this);

刷新私聊区.addActionListener(this);

listComponent=new List();

ionListener(this);

privateChatList=new Choice();

("大家(*)");

(0);

add(谈话显示区);

谈话显示区.setBounds(10, 10, (width-120)/2, (height-120));

add(私聊显示区);

私聊显示区.setBounds(10+(width-120)/2, 10, (width-120)/2, (height-120));

add(listComponent);

nds(10+(width-120), 10, 100, (height-160));

add(提示条);

提示条.setBounds(10+(width-120), 10+(height-160), 100, 40);

Panel pSouth=new Panel();

(送出信息);

(确定);

(privateChatList);

(刷新谈话区);

(刷新私聊区);

add(pSouth);

nds(10,20+(height-120),width-20,60);

}

public void setName(String s){

name=s;

}

//和服务器端建立连接

public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out){

=socket;

=in;

=out;

try{

();

}catch(Exception e){

}

}

//通过ActionEventd的getSourse()方法判断哪个按钮被按下

//并根据用户不同的命令作出相应的处理

public void actionPerformed(ActionEvent e){

if(rce()==确定||rce()==送出信息){

String message="";

String people=ectedItem();

people=ing(5, f("("));

message=送出信息.getText();

//如果用户单击发送按钮并且发送的内容为公共聊天,则在谈话区显示聊天内容

//如果用户聊天为私聊,则在私聊区显示聊天内容

if(()>0){

try{

if(("大家")){

TF("公共聊天内容:"+name+"说:"+message);

}

else{

TF("私人聊天内容:"+name+"悄悄地说:"+message);

}

}catch(IOException event){

}

}

}else if(rce()==listComponent){

(ectedItem(), 0);

t();

}

else if(rce()==刷新谈话区){

谈话显示区.setText(null);

}

else if(rce()==刷新私聊区){

私聊显示区.setText(null);

}

}

//读取服务器端发来的消息,并根据此消息在不同的聊天显示区

//显示相应的聊天内容或者显示用户的个人信息及在线信息

public void run(){

while(true){

String s=null;

try{

s=F();

if(With("聊天内容:")){

String content=ing(f(":")+1);

谈话显示区.append("n"+content);

}

if(With("私人聊天内容:")){

String content=ing(f(":")+1);

私聊显示区.append("n"+content);

}

else if(With("聊天者:")){

String people=ing(f(":")+1, f("性别"));

String sex=ing(f("性别")+2);

((String)(people));

}

}

}

t();

}

else if(With("用户离线:")){

String awayPeopleName=ing(f(":")+1);

((String)(awayPeopleName));

t();

谈话显示区.append("n"+(String)(awayPeopleName)+"离线");

(awayPeopleName);

}

(5);

}

catch(IOException e){

All();

t();

();

谈话显示区.setText("和服务器的连接已中断");

break;

}catch(InterruptedException e){

}


本文标签: 用户 消息 客户端 用户名 服务器端