JavaSocket UDP Byte类型的收发

本文介绍了一个使用Java实现的UDP通信示例程序,演示了如何发送和接收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();
        }
    }
}

 

这里还有一个将两个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;
    }
    
}

 

 

 一点注意:在s.receive一句之前加上这句: 


socket.setSoTimeout(5000); //设置5秒的timeout 

这样可以防止,在没有东西receive的时候,程序被block

转载于:https://www.cnblogs.com/super119/archive/2011/01/03/1924563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值