聊天


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值