📄 client1.java
字号:
import java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.lang.*;public class client1 extends JFrame implements ActionListener{ Socket clientsocket; DataOutputStream out; DataInputStream in; JButton b1=null; JButton b2=null; JButton b3=null; JTextField t1=null; JTextField t2=null; JTextField t3=null; JTextArea text=null; public client1(){ JFrame Cframe=new JFrame("client"); Container ContentPane=Cframe.getContentPane(); Box baseBox=Box.createVerticalBox(); ContentPane.add(baseBox); Box box1=Box.createHorizontalBox(); box1.setBorder(BorderFactory.createTitledBorder("请输入目标服务器信息")); baseBox.add(box1); JLabel l1=new JLabel("IP:"); l1.setMaximumSize(new Dimension(20,20)); JLabel l2=new JLabel("端口号:"); l2.setMaximumSize(new Dimension(20,20)); t1=new JTextField(16); t2=new JTextField("2006",16); box1.add(l1); box1.add(t1); box1.add(Box.createHorizontalStrut(30)); box1.add(l2); box1.add(t2); Box box2=Box.createHorizontalBox(); baseBox.add(box2); b1=new JButton("开始连接"); b1.setMaximumSize(new Dimension(100,100)); b2=new JButton("断开连接"); b2.setMaximumSize(new Dimension(100,100)); b1.addActionListener(this); b2.addActionListener(this); box2.add(Box.createHorizontalStrut(200)); box2.add(b1); box2.add(Box.createHorizontalStrut(100)); box2.add(b2); box2.add(Box.createHorizontalStrut(200)); Box box3=Box.createHorizontalBox(); box3.setBorder(BorderFactory.createTitledBorder("聊天记录")); baseBox.add(box3); text=new JTextArea(10,50); text.setLineWrap(true); text.setWrapStyleWord(true); box3.add(new JScrollPane(text)); Box box4=Box.createVerticalBox(); box4.setBorder(BorderFactory.createTitledBorder("发送即时讯息")); baseBox.add(box4); t3=new JTextField(50); b3=new JButton("发送"); b3.setAlignmentX(Component.CENTER_ALIGNMENT); b3.setMaximumSize(new Dimension(100,100)); b3.addActionListener(this); box4.add(t3); box4.add(b3); Cframe.pack(); Cframe.setVisible(true); Cframe.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try{ out.writeUTF("bye"); }catch(Exception ee) {} try{ out.close(); in.close(); clientsocket.close(); dispose(); System.exit(0); }catch(Exception ee) {} } }); } public void actionPerformed(ActionEvent e){ if (e.getSource()==b1){ //start connection; String str1=new String(); String str2=new String(); int port; str1=t1.getText(); str2=t2.getText(); port=(int)Integer.parseInt(str2); if (str1.compareTo("")!=0){ try{ clientsocket=new Socket(str1,port); OutputStream os=clientsocket.getOutputStream(); out=new DataOutputStream(os); InputStream is=clientsocket.getInputStream(); in=new DataInputStream(is); Communion1 th=new Communion1(this); th.start(); }catch(IOException ee){ JOptionPane.showMessageDialog(null,"连接服务器失败!"); return; } } else text.append("请输入目标服务器IP!\n"); } if (e.getSource()==b2){ //close connection; try{ out.writeUTF("bye"); }catch(Exception ee) {} try{ out.close(); in.close(); clientsocket.close(); text.append("已断开连接!\n"); }catch(Exception ee) {text.append("未连接或断开连接失败!\n");} } if (e.getSource()==b3){ //send; String str3=new String(); str3=t3.getText(); try{ out.writeUTF(str3); text.append("client:"+str3+"\n"); t3.setText(""); }catch(Exception ee) { text.append("发送失败!\n"); t3.setText("");} } } public static void main(String args[]) { client1 C=new client1(); } }class Communion1 extends Thread{ client1 fp; Communion1(client1 fp){ this.fp=fp; } public void run(){ String msg=null; while(true){ try{ msg=fp.in.readUTF(); if(msg.equals("bye")){ fp.text.append("服务器已经停止!\n"); break; } fp.text.append("server:"+msg+"\n"); }catch(Exception ee) {break;} } try{ fp.out.close(); fp.in.close(); fp.clientsocket.close(); }catch(Exception ee) {} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -