⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagramexample.java

📁 JAVA SE6 全方位学习 朱仲杰 编著 机械工业出版社出版
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;

public class DatagramExample extends Applet
{
  InetAddress srv;
  DatagramSocket clnt;
  DatagramPacket packet;
  byte[] buffer = new byte[256];

  String srvName;
  int srvPort;
  Label label1,label2;
  TextField textfield_send,textfield_receive;
  Button button_send;
  
  public void init()
  {
  	setLayout(new GridLayout(2,3));
  	
	label1 = new Label("请输入欲传送字符串:");
	label2 = new Label("Server 返回的字符串:");
	textfield_send = new TextField();
	textfield_receive = new TextField();
	textfield_receive.setEditable(false);
	srvName = "127.0.0.1";
	srvPort = 7;
	makeConnection();

	button_send = new Button("送出");
	button_send.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent ae)
		{
			send_receive();
		}
	}
	);
	add(label1);
	add(textfield_send);
	add(button_send);
	add(label2);
	add(textfield_receive);
  }
  
  private void send_receive()
  {
	try
	{
		buffer = textfield_send.getText().getBytes();
		packet = new DatagramPacket(buffer,buffer.length,srv,srvPort);
		clnt.send(packet);
		
		clnt.receive(packet);
		
		String receive = new String(packet.getData());
		textfield_receive.setText(receive);
	}
	catch (IOException IOE)
	{
		System.err.println(IOE);
	}
  }
  private void makeConnection()
  {
    try
    {
	  clnt = new DatagramSocket();
	  srv = InetAddress.getByName(srvName);
	  
      System.err.println("Connection to server ok\n");
    }
    catch (IOException io)
    {
      System.err.println("Error:"+io);
    }
  }
  public void stop()
  {
    try
    {
    	packet = null;
    	clnt.close();
    	srv = null;
    }
    catch (Exception io) {};
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -