聊天

来源:互联网 发布:计算机科学与技术 知乎 编辑:程序博客网 时间:2024/04/28 06:53


ChatClient 



public class ChatClient extends Frame {



public static void main(String[] args) {


ChatClient cc1 =new ChatClient("客户端聊天窗口1");


}


private TextField tfChatClient;
private TextArea taChatClient;
private DataOutputStream dos = null;
private DataInputStream dis = null ;
private Socket s = null ;
private boolean receiveCB ;


ChatClient(String s) {
// TODO Auto-generated constructor stub
super(s);

// chat02 加入两个功能 文本框 和 文本区域
tfChatClient = new TextField();
taChatClient = new TextArea();


// 排放的位置
add(tfChatClient, BorderLayout.SOUTH);
add(taChatClient, BorderLayout.NORTH);
this.pack();

// chat03 实现的是关闭窗口
this.addWindowListener(new WindowAdapter() {


public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
disClientServer() ;
System.exit(-1);
}
});

clientServer() ;
tfChatClient.addActionListener(new TFChatClient()) ;

ReceiveClient rc =new ReceiveClient() ;
Thread t = new Thread (rc) ;
t.start() ;
this.setVisible(true);


}
//chat05 TCP的连接

public void clientServer() {


try {
s = new Socket("localhost" ,8888) ;
dos = new DataOutputStream(s.getOutputStream()) ;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void disClientServer() {


try {
if(dos != null)
dos.close() ;
if(dis != null)
dis.close() ;
if(s != null)
s.close() ;
} catch (IOException e) {
e.printStackTrace();
}

}
// chat04主要的功能版本是写从textfield 传到 textarea中
class TFChatClient implements ActionListener {


public void actionPerformed(ActionEvent e) {
/*
* 1先获取textfield中的数据
* 2再把数据传送到 textarea中
* 3把文件最后设置为空
*/
String str = tfChatClient.getText().trim() ;
// taChatClient.setText(str) ;
tfChatClient.setText("") ;


try {
dos.writeUTF(str) ;
dos.flush() ;
} catch (Exception e2) {
}
}


}


class ReceiveClient implements Runnable {

public void run() {
try {
receiveCB =false ;
dis =new DataInputStream(s.getInputStream()) ;
receiveCB =true ;
while (receiveCB){
String str = dis.readUTF() ;
//System.out.println(str);
taChatClient.setText(taChatClient.getText() + str +'\n') ;

}
} catch (SocketException e) {
System.out.println("程序已关闭");
}catch (IOException e) {
e.printStackTrace();
}

}


}


ChatServer 


public class ChatServer {
public static void main(String[] args) {
// 实现TCP的连接
/*
* 设置两个boolean类型的变量来控制一下事件的发生
*/
ChatServer cs = new ChatServer();
cs.Strat();
}


Collection<Client> clients = new ArrayList<Client>();


public void Strat() {


ServerSocket chatss = null;


try {
chatss = new ServerSocket(8888);
}
catch (BindException e) {
System.out.println("端口已被占用!");
} catch (SocketException e){
System.out.println("端口已被占用!");
} catch (IOException e1) {
e1.printStackTrace();
}


boolean booleanStart1 = false;

try {

booleanStart1 = true;


while (booleanStart1) {
Socket s = chatss.accept();
Client client = new Client(s);
new Thread(client).start();
clients.add(client);
}

} catch (NullPointerException e){
System.out.println("端口已被占用!");
}catch (IOException e) {
System.out.println("程序已关闭");
}
}

class Client implements Runnable {

private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean booleanStart2 = false;

Client(Socket s) {

this.s = s;

try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
booleanStart2 = true;

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

}

public void send(String str) {
try {
dos.writeUTF(str);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void run() {

try {
// System.out.println("dsfsda");
while (booleanStart2) {
String str = dis.readUTF();
System.out.println(str);
for (int i = 0; i < clients.size(); i++) {
Client c = ((ArrayList<Client>) clients).get(i);
c.send(str);
}
}
}catch(EOFException e){
} catch (IOException e) {
e.printStackTrace();
} finally {

try {
if (dis != null)
dis.close();
if (dos != null)
dos.close();
if (s != null)
s.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

}

0 0