您的位置:首页 > Web前端

新浪旧事-Java向飞秋(FeiQ)发送消息

2012-04-13 09:24 363 查看
package test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

public class TestFeiQ {
public static void main(String[] args) {
DatagramSocket socket;
InetAddress address;
long IPMSG_SENDMSG = 0x00000020;
String SENDER = "乱 7 8 糟";
String HOST = "Localhost";
String MSG_CONTENT = "Hello 世界!";
try {
socket = new DatagramSocket();
address = InetAddress.getByName("192.168.0.145");// 发送给消息的地址

byte[] buffer = ("1:" + new Date().getTime() + ":" + SENDER + ":"
+ HOST + ":" + IPMSG_SENDMSG + ":" + MSG_CONTENT)
.getBytes("gbk");
DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
address, 2425);
socket.send(packet); // 发送报文
packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);// 接收回应
String message = new String(packet.getData()); // 得到报文信息
System.out.println(message); // 显示对方返回的信息
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: