java实现局域网聊天

kpp19920121 2014-10-01 03:54:25
package test05;

public class GameSize {
public static final int GAME_X=200;
public static final int GAME_Y=300;
public static final int GAME_WIDTH=400;
public static final int GAME_HIGH=530;
}



package test05;
import java.net.*;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;



/**
* 1.主线程负责打开服务器以及接受客户端的链接
* 2.每接受一个客户端的链接就开辟一个线程
*
*
*/
public class ChatServer {
/*
* 主线程
*/
public static void main(String[] args) {
ServerSocket ss=null;
ChatServer cs=new ChatServer();
try{
ss=new ServerSocket(8888);
cs.accept(ss);
}catch(BindException e){
System.out.println("端口已被占用,请关闭端口");
}catch(IOException e){
e.printStackTrace();
}
}


/*
* 接受客户端的连接
*/
public void accept(ServerSocket ss) throws IOException{
Socket socket=null;
boolean bAccept=true;
try{
while(bAccept){

socket=ss.accept();
new Thread(new ClientThread(ss,socket)).start();
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(socket!=null){
socket.close();
socket=null;
}

}
}

class ClientThread implements Runnable{
boolean flag=true;
ServerSocket ss=null;
Socket socket=null;
DataInputStream dis=null;

public ClientThread(ServerSocket ss,Socket socket){
this.ss=ss;
this.socket=socket;
}

public void run() {
try{

dis=new DataInputStream(socket.getInputStream());
while(flag){
System.out.println(dis.readUTF());
}
}catch(EOFException e){
try {
socket.close();
System.out.println("客户端已关闭");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(socket!=null){
socket.close();
socket=null;
}

}catch(IOException e){

}
}

}

}

}


package test05;
import java.awt.*;
/**
* 1.窗口的设计
* 2.按钮的添加
*
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.*;

public class ChatClient extends Frame{
TextField tf=null;
TextArea ta=null;
Button button=null;
Socket socket=null;
DataOutputStream dos=null;

/**
* 主方法:加载窗口
*
*/
public static void main(String[] args){
new ChatClient().launchFrame();
}


/*
* 窗口的属性:
* 1.加载窗口时与服务器建立连接
*/
public void launchFrame(){
setBounds(GameSize.GAME_X,GameSize.GAME_Y,GameSize.GAME_WIDTH,GameSize.GAME_HIGH);
setVisible(true);
setTitle("我的聊天");
this.setResizable(false);
this.setLayout(null);
addConpomnent();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
CloseAll(socket,dos);
System.exit(0);
}

});
connection();
}


/*
* 向服务器端申请链接
* 初始化dos
*/
public void connection(){
try{
socket=new Socket("localhost",8888);
dos=new DataOutputStream(socket.getOutputStream());

System.out.println("一个客户端请求链接");

}catch(UnknownHostException e){
CloseAll(socket,dos);
System.out.println("未找到主机");
}catch(IOException e){
CloseAll(socket,dos);
System.out.println("IO有错误");
}
}

/*
* 流的关闭
*/
public void CloseAll(Closeable...io){
for(Closeable temp:io){
try{
if(temp!=null){
temp.close();
temp=null;
}

}catch(IOException e){
e.printStackTrace();
}
}
}



/*
* 向服务器端发送信息
*/
public void sendMessage(String str){
try{


dos.writeUTF(str);
dos.flush();
}catch(IOException e){
CloseAll(socket,dos);
e.printStackTrace();
}
}

/*
* 添加conpoment
*/
public void addConpomnent(){
tf=new TextField();
tf.setBounds(10,
GameSize.GAME_HIGH*4/5+30,
GameSize.GAME_WIDTH*4/5,
GameSize.GAME_HIGH-GameSize.GAME_HIGH*4/5);
tf.addActionListener(new tfAction());
add(tf);


ta=new TextArea();
ta.setBounds(10,
30,
GameSize.GAME_WIDTH,
GameSize.GAME_HIGH*4/5);

add(ta);


button=new Button("发送");
button.setBounds(GameSize.GAME_WIDTH*4/5,
GameSize.GAME_HIGH*4/5+30,
GameSize.GAME_WIDTH-GameSize.GAME_WIDTH*4/5,
GameSize.GAME_HIGH-GameSize.GAME_HIGH*4/5);
button.addActionListener(new buttonAction());
add(button);
}


/*
* textArea的监听
*/
class tfAction implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
tf=(TextField)e.getSource();
ta.setText(ta.getText()+tf.getText()+'\n');
sendMessage(tf.getText());
tf.setText(null);


}

}


/*
* button的监听
*/
class buttonAction implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
Button b=(Button)e.getSource();
ta.setText(ta.getText()+tf.getText()+'\n');
sendMessage(tf.getText());
tf.setText(null);

}

}

}

...全文
258 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
SerenoShen 2014-10-03
  • 打赏
  • 举报
回复 1
楼主是想表达什么啊~~~~~~

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧