JAVA代码

mr_haiMeng 2015-08-05 09:04:27
一个简单的两人聊天代码

/* 服务端平台*/
public class Server extends JFrame {
private Socket socket;
private JPanel contentPane;
private JTextField textField;
private boolean isRunning = true;

public boolean isRunning() {
return isRunning;
}

public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Server frame = new Server();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Server() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 344, 320);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

JButton btnNewButton = new JButton("启动服务");
btnNewButton.setBounds(10, 10, 155, 55);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ServerSocket server = new ServerSocket(8080);
System.out.println("服务器启动");
socket = server.accept();
Thread t = new Thread(new ServerRead(Server.this));
t.start();
System.out.println("有客户端连接");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
contentPane.setLayout(null);
contentPane.add(btnNewButton);

JButton button = new JButton("\u53D1\u9001");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
write();
}
});
button.setBounds(248, 233, 70, 23);
contentPane.add(button);

textField = new JTextField();
textField.setBounds(10, 222, 199, 50);
contentPane.add(textField);
textField.setColumns(10);
}

public void read() {
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
System.out.println("服务器接受信息 " + line);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

public void write(){
try {
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
String words = textField.getText();
System.out.println("服务器发送信息");
bw.write(words + "\n");
textField.setText("");
bw.flush();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public class ServerRead implements Runnable {
private Server server;

public ServerRead(Server server) {
this.server = server;
}

@Override
public void run() {
while(server.isRunning()){
server.read();

}

}

}
/* 客户端平台*/
public class Client extends JFrame {

private Socket socket;
private JPanel contentPane;
private JTextField textField;
private JList list;
private DefaultListModel<String> modle;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Client frame = new Client();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Client() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 483, 418);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

textField = new JTextField();
textField.setBounds(52, 307, 301, 63);
contentPane.add(textField);

JButton button = new JButton("\u53D1\u9001");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
write();
}
});
button.setBounds(363, 325, 73, 23);
contentPane.add(button);

list = new JList();
list.setBounds(52, 24, 291, 273);
modle=new DefaultListModel<>();
list.setModel(modle);
contentPane.add(list);


JButton button_1 = new JButton("\u8FDE\u63A5\u670D\u52A1\u5668");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
System.out.println("连接服务器");
socket=new Socket("192.168.0.66", 8080);
System.out.println("连接服务器成功");
Thread t=new Thread(new ClientRead(Client.this));
t.start();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
button_1.setBounds(353, 46, 93, 65);
contentPane.add(button_1);
}
public void read() {
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
modle.addElement("服务器说的"+line);
System.out.println(line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(){
try {
OutputStream os=socket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
String words= textField.getText();
bw.write(words+"\n");
modle.addElement(words);
textField.setText("");
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ClientRead implements Runnable {
private Client client;

public ClientRead(Client client) {
this.client = client;
}

@Override
public void run() {
while (true) {
client.read();
}

}

}
Server运行后


Client运行后
...全文
91 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,713

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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