JavaSocket UDP Byte类型的收发
package com.zchome;
import java.net.*;
import java.io.*;
public class testudp {
public static void main(String[] args) {
DatagramSocket s = null;
try {
s = new DatagramSocket();
byte[] control_buffer = new byte[4];
byte[] content_buffer = null;
byte[] result_buffer = null;
byte[] receive_buffer = new byte[100];
for (int nloopindex = 0; nloopindex < 10; nloopindex++) {
//define first 4 Bytes
control_buffer[0] = 0x00;
control_buffer[1] = 0x01;
control_buffer[2] = 0x01;
control_buffer[3] = 0x00;
String IPAddress = "192.168.0.2";
content_buffer = IPAddress.getBytes();
result_buffer = Toolkit.getInstance().addByteBuffers(control_buffer, content_buffer);
InetAddress ia = InetAddress.getByName("192.168.0.9");
DatagramPacket dgp = new DatagramPacket(result_buffer, result_buffer.length, ia, 10002);
s.send(dgp);
dgp = new DatagramPacket(receive_buffer, receive_buffer.length, ia, 10002);
s.receive(dgp);
System.out.println(new String(dgp.getData()));
}
}
catch (IOException e) {
System.out.println(e.toString());
}
finally {
if (s != null)
s.close();
}
}
}
import java.net.*;
import java.io.*;
public class testudp {
public static void main(String[] args) {
DatagramSocket s = null;
try {
s = new DatagramSocket();
byte[] control_buffer = new byte[4];
byte[] content_buffer = null;
byte[] result_buffer = null;
byte[] receive_buffer = new byte[100];
for (int nloopindex = 0; nloopindex < 10; nloopindex++) {
//define first 4 Bytes
control_buffer[0] = 0x00;
control_buffer[1] = 0x01;
control_buffer[2] = 0x01;
control_buffer[3] = 0x00;
String IPAddress = "192.168.0.2";
content_buffer = IPAddress.getBytes();
result_buffer = Toolkit.getInstance().addByteBuffers(control_buffer, content_buffer);
InetAddress ia = InetAddress.getByName("192.168.0.9");
DatagramPacket dgp = new DatagramPacket(result_buffer, result_buffer.length, ia, 10002);
s.send(dgp);
dgp = new DatagramPacket(receive_buffer, receive_buffer.length, ia, 10002);
s.receive(dgp);
System.out.println(new String(dgp.getData()));
}
}
catch (IOException e) {
System.out.println(e.toString());
}
finally {
if (s != null)
s.close();
}
}
}
这里还有一个将两个byte数组相加的方法:
/*
* Toolkit.java
*
* Created on 2004年8月18日, 上午10:53
*/
package com.zchome;
/**
*
* @author 张超
*/
public class Toolkit {
/** Creates a new instance of Toolkit */
private Toolkit() {
}
private static Toolkit _instance = null;
public static Toolkit getInstance() {
if (_instance == null)
_instance = new Toolkit();
return _instance;
}
public byte[] addByteBuffers(byte[] first, byte[] second)
{
int nloopindex = 0;
int first_length = first.length;
int second_length = second.length;
byte[] return_bytes = new byte[first_length + second_length];
for (nloopindex = 0; nloopindex < first_length; nloopindex++)
{
return_bytes[nloopindex] = first[nloopindex];
}
for (nloopindex = 0; nloopindex < second_length; nloopindex++)
{
return_bytes[first_length + nloopindex] = second[nloopindex];
}
return return_bytes;
}
}
* Toolkit.java
*
* Created on 2004年8月18日, 上午10:53
*/
package com.zchome;
/**
*
* @author 张超
*/
public class Toolkit {
/** Creates a new instance of Toolkit */
private Toolkit() {
}
private static Toolkit _instance = null;
public static Toolkit getInstance() {
if (_instance == null)
_instance = new Toolkit();
return _instance;
}
public byte[] addByteBuffers(byte[] first, byte[] second)
{
int nloopindex = 0;
int first_length = first.length;
int second_length = second.length;
byte[] return_bytes = new byte[first_length + second_length];
for (nloopindex = 0; nloopindex < first_length; nloopindex++)
{
return_bytes[nloopindex] = first[nloopindex];
}
for (nloopindex = 0; nloopindex < second_length; nloopindex++)
{
return_bytes[first_length + nloopindex] = second[nloopindex];
}
return return_bytes;
}
}
一点注意:在s.receive一句之前加上这句:
socket.setSoTimeout(5000); //设置5秒的timeout
这样可以防止,在没有东西receive的时候,程序被block