模拟QQ聊天程序_客户端_网络编程
2011-05-11 17:53
585 查看
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame { //创建客户端程序
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
TextField tfTxt = new TextField(); //创建窗口各元素
TextArea taContent = new TextArea();
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() { //创建符合要求的聊天窗口
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH); //窗口内部元素进行布局
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() { //对桌面事件进行监听
@Override
public void windowClosing(WindowEvent e) { //处理关闭小窗后事件
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
this.setVisible(true);
connect();
new Thread(new RecvThread()).start();
}
public void connect() {
try {
s = new Socket("127.0.0.1", 8888); //设置好服务器地址,端口
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
bConnected = true;
System.out.println("connected!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText(""); //清空输入框中的数据
try {
dos.writeUTF(str);
dos.flush(); //刷新流管道中的数据,方便关闭
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while (bConnected) {
String str = dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText() + str + '/n'); //把接受到的数据显示出来
}
} catch (IOException e){
e.printStackTrace();
}
}
}
}
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame { //创建客户端程序
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
TextField tfTxt = new TextField(); //创建窗口各元素
TextArea taContent = new TextArea();
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() { //创建符合要求的聊天窗口
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH); //窗口内部元素进行布局
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() { //对桌面事件进行监听
@Override
public void windowClosing(WindowEvent e) { //处理关闭小窗后事件
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
this.setVisible(true);
connect();
new Thread(new RecvThread()).start();
}
public void connect() {
try {
s = new Socket("127.0.0.1", 8888); //设置好服务器地址,端口
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
bConnected = true;
System.out.println("connected!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText(""); //清空输入框中的数据
try {
dos.writeUTF(str);
dos.flush(); //刷新流管道中的数据,方便关闭
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while (bConnected) {
String str = dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText() + str + '/n'); //把接受到的数据显示出来
}
} catch (IOException e){
e.printStackTrace();
}
}
}
}
相关文章推荐
- 模拟QQ聊天程序_服务器端_网络编程
- java网络编程(三):一个类似QQ的聊天程序
- java 网络编程五 (仿QQ聊天程序)
- Java基础---Java---网络编程---TCP的传输、客户端和服务端的互访、建立一个文本转换器、编写一个聊天程序
- Java基础---Java---网络编程---TCP的传输、客户端和服务端的互访、建立一个文本转换器、编写一个聊天程序
- 仿QQ聊天程序SOCKET编程服务端与客户端,成功通信
- 网络编程 CAsyncSocket类的应用实例 聊天程序 客户端代码
- java网络编程一:模拟qq聊天功能,实现一对一聊天
- 网络编程与多线程的应用--基于socket udp编写一个简单聊天程序
- java编程_socket_套接字_网络编程_简易的GUI多线程聊天程序
- 网络编程学习笔记(二)UDP协议及聊天小程序的实现
- 网络编程UDP模仿QQ聊天功能
- [置顶] JAVA进阶案例 TCP编程之网络聊天工具(客户端)
- Linux网络编程之聊天程序(TCP协议之select)
- 网络编程之简单的多人聊天程序
- Socket网络编程--聊天程序(1)
- c++ 网络编程 socket 聊天客户端/服务器
- socket模拟聊天程序(1)-单客户端
- linux网络编程:用C语言实现的聊天程序(异步通信)
- 基于Udp的Socket网络编程聊天程序