📄 communicationclient.java
字号:
package communicationclient;import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.net.*;import java.io.*;/** * Title: 客户端UDP通讯Applet程序 * Description: 用于客户端,实现与服务器端UDP通讯 * Copyright: Copyright (c) 2002 * Company: 北京师范大学计算机系 * @author 孙一林 * @version 1.0 */public class CommunicationClient extends Applet { boolean isStandalone = false; JLabel jLabel1 = new JLabel(); JTextArea jTextArea1 = new JTextArea(); JLabel jLabel2 = new JLabel(); JTextField jTextField1 = new JTextField(); private DatagramSocket sendSocket,receiveSocket;//声明发送数据报Socket和接收数据报Socket private DatagramPacket sendPacket,receivePacket;//声明发送数据报文包和接收数据报文包 /**Get a parameter value*/ public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } /**Construct the applet*/ public CommunicationClient() { } /**Initialize the applet*/ public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { jLabel1.setText("通讯记录:"); jLabel1.setBounds(new Rectangle(16, 9, 68, 27)); this.setLayout(null); jTextArea1.setText("你好!"); jTextArea1.setBounds(new Rectangle(15, 48, 349, 59)); jLabel2.setText("输入通讯内容:"); jLabel2.setBounds(new Rectangle(17, 125, 92, 37)); jTextField1.setText("通讯内容"); jTextField1.setBounds(new Rectangle(127, 129, 244, 31)); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jTextField1_actionPerformed(e); } }); this.add(jLabel1, null); this.add(jTextArea1, null); this.add(jLabel2, null); this.add(jTextField1, null); } /**Start the applet*/ public void start() { waitForPackets(); } /**Stop the applet*/ public void stop() { } /**Destroy the applet*/ public void destroy() { } /**Get Applet information*/ public String getAppletInfo() { return "Applet Information"; } /**Get parameter info*/ public String[][] getParameterInfo() { return null; } /**方法waitForPacket用来监听来自于服务器的数据报,当获得数据报后,在文本显示区域显示出来*/ public void waitForPackets(){ try{ sendSocket=new DatagramSocket();//实例化一个发送数据报Socket对象 receiveSocket= new DatagramSocket(8001);//实例化一个接收数据报Socket对象,以8001为端口 } catch (SocketException e){//捕获异常 jTextArea1.append("不能打开数据报Socket,或数据报Socket无法与指定端口连接!"); } while (true){ try{ byte buf[]=new byte[100]; receivePacket=new DatagramPacket(buf,buf.length);//实例化一个接收数据报文包对象 receiveSocket.receive(receivePacket);//以receivePacket为参数,接收数据包 jTextArea1.append("\n服务器:\t"); byte[] data=receivePacket.getData(); String receivedString=new String(data); jTextArea1.append(receivedString);//将接收到的数据包中的数据显示出来 } catch(IOException e){ jTextArea1.append("网络通讯出现错误,问题在"+e.toString()); } } } /**Main method*/ public static void main(String[] args) { CommunicationClient applet = new CommunicationClient(); applet.isStandalone = true; Frame frame; frame = new Frame() { protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public synchronized void setTitle(String title) { super.setTitle(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } }; frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(400,320); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } void jTextField1_actionPerformed(ActionEvent e) { try{ jTextArea1.append("\n客户端:"); String string=jTextField1.getText(); jTextArea1.append(string); byte[] databyte=new byte[100]; string.getBytes(0,string.length(),databyte,0); sendPacket=new DatagramPacket(databyte,string.length(),InetAddress.getByName("202.112.88.31"),8000);//发送数据报,其中IP为主机服务器IP地址 sendSocket.send(sendPacket); } catch(IOException ioe){ jTextArea1.append("网络通讯出现错误,问题在"+ioe.toString()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -