关于java UDP方面的一些问题
我想实现一个功能:服务器接收客户端的消息,如果接收到数据“1”,则向客户端发送数据“a”。客户端方面:客户端连续扫描局域网IP,然后每次扫描都向服务器发送数据“1”,然后...
我想实现一个功能:服务器接收客户端的消息,如果接收到数据“1”,则向客户端发送数据“a”。客户端方面:客户端连续扫描局域网IP,然后每次扫描都向服务器发送数据“1”,然后接收到来自服务器的“a”。其实我想实现的是类似飞鸽的程序。但是我这个测试代码有问题,总是阻塞。比如你只启动一个机器,它既是客户端,又是服务器。比如本机IP:192.168.0.100,如果我先扫描99这个IP,那么会卡在客户端的d.receive这个地方。我知道设置超时可以,但是如果有个上线的主机向我这里发送数据没有成功。那么直接超时就算是跳过去了吗?有没有方法判断连接主机是否存在?不要用ping命令还有什么arp命令。代码如下:
客户端:
import java.net.*;
public class Client {
public static void main(String[] args) throws Exception {
DatagramSocket d = new DatagramSocket();
byte[] temp = new byte[4];
temp[0] = (byte) 192;
temp[1] = (byte) 168;
temp[2] = 0;
for (int i = 102; i <=103; i++) {
temp[3]=(byte) i;
d.connect(InetAddress.getByAddress(temp),8000);
DatagramPacket p = new DatagramPacket("1".getBytes(), 1, InetAddress.getByAddress(temp),8000);
d.send(p);
System.out.println("客户端向192.168.0."+temp[3]+"发送数据..");
DatagramPacket p1 = new DatagramPacket(new byte[1],1);
d.receive(p1);
System.out.println("从服务器接收到"+new String(p1.getData()));
}
}
}
服务器:
import java.io.IOException;
import java.net.*;
public class Server {
public static void main(String[] args) throws SocketException, UnknownHostException, IOException {
DatagramSocket d = new DatagramSocket(8000);
int i=0;
while(true) {
System.out.println("服务器开始接收第"+(++i)+"次数据..");
DatagramPacket p = new DatagramPacket(new byte[1], 1);
d.receive(p);
String a=new String(p.getData());
System.out.println("收到数据"+a);
if(a.equals("1")) {
p.setData("a".getBytes());
d.send(p);
System.out.println("服务器向"+p.getAddress().getHostAddress()+"发送数据..");
}
}
}
}
我只想知道有什么办法解决阻塞,用线程也是这样,很无奈
对了那个客户端的一段代码 for (int i = 102; i <=103; i++) {
应该改成 for (int i = 2; i <=254; i++) { 展开
客户端:
import java.net.*;
public class Client {
public static void main(String[] args) throws Exception {
DatagramSocket d = new DatagramSocket();
byte[] temp = new byte[4];
temp[0] = (byte) 192;
temp[1] = (byte) 168;
temp[2] = 0;
for (int i = 102; i <=103; i++) {
temp[3]=(byte) i;
d.connect(InetAddress.getByAddress(temp),8000);
DatagramPacket p = new DatagramPacket("1".getBytes(), 1, InetAddress.getByAddress(temp),8000);
d.send(p);
System.out.println("客户端向192.168.0."+temp[3]+"发送数据..");
DatagramPacket p1 = new DatagramPacket(new byte[1],1);
d.receive(p1);
System.out.println("从服务器接收到"+new String(p1.getData()));
}
}
}
服务器:
import java.io.IOException;
import java.net.*;
public class Server {
public static void main(String[] args) throws SocketException, UnknownHostException, IOException {
DatagramSocket d = new DatagramSocket(8000);
int i=0;
while(true) {
System.out.println("服务器开始接收第"+(++i)+"次数据..");
DatagramPacket p = new DatagramPacket(new byte[1], 1);
d.receive(p);
String a=new String(p.getData());
System.out.println("收到数据"+a);
if(a.equals("1")) {
p.setData("a".getBytes());
d.send(p);
System.out.println("服务器向"+p.getAddress().getHostAddress()+"发送数据..");
}
}
}
}
我只想知道有什么办法解决阻塞,用线程也是这样,很无奈
对了那个客户端的一段代码 for (int i = 102; i <=103; i++) {
应该改成 for (int i = 2; i <=254; i++) { 展开
展开全部
我把我的代码借鉴给你..不知道能不能够帮助你..!!!自己原创的
UDPServer.java:
import java.net.*;
import java.util.*;
public class UDPServer extends Thread {
private static final int PORT=10000 ;
private static final int DATA_LEN=2046 ;
private byte []buff =new byte[DATA_LEN];
UDPClient.java:
import java.net.*;
import java.util.*;
public class UDPClient extends Thread {
private static final int PORT=10000 ;
private static final int DATA_LEN=2046 ;
private byte []buff =new byte[DATA_LEN];
private DatagramSocket socket ;
private DatagramPacket inpacket =new DatagramPacket(buff,buff.length);
private DatagramPacket outpacket ;
public void run() {
int i =0;
try{
socket=new DatagramSocket();
outpacket =new DatagramPacket(new byte[0] ,0,
InetAddress.getByName("127.0.0.1"),PORT);
Scanner sc =new Scanner(System.in);
while(sc.hasNextLine()){
byte [] buff1 =sc.nextLine().getBytes();
outpacket.setData(buff1);
socket.send(outpacket);
socket.receive(inpacket);
System.out.println(new String(buff,0,inpacket.getLength()));
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String []args){
new UDPClient().start();
}
/*public UDPServer (DatagramSocket socket ,DatagramPacket inpacket,
DatagramPacket outpacket) {
this.socket = socket ;
this.inpacket = inpacket ;
this.outpacket = outpacket ;
}*/
};
MulticastSocketTest.java:
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
//让该类实现Runnable接口,该类的实例可作为线程的target
public class MulticastSocketTest implements Runnable
{
//使用常量作为本程序的多点广播IP地址
private static final String BROADCAST_IP
= "230.0.0.1";
//使用常量作为本程序的多点广播目的的端口
public static final int BROADCAST_PORT = 30000;
//定义每个数据报的最大大小为4K
private static final int DATA_LEN = 4096;
//定义本程序的MulticastSocket实例
private MulticastSocket socket = null;
private InetAddress broadcastAddress = null;
private Scanner scan = null;
//定义接收网络数据的字节数组
byte[] inBuff = new byte[DATA_LEN];
//以指定字节数组创建准备接受数据的DatagramPacket对象
private DatagramPacket inPacket =
new DatagramPacket(inBuff , inBuff.length);
//定义一个用于发送的DatagramPacket对象
private DatagramPacket outPacket = null;
public void init()throws IOException
{
try
{
//创建用于发送、接收数据的MulticastSocket对象
//因为该MulticastSocket对象需要接收,所以有指定端口
socket = new MulticastSocket(BROADCAST_PORT);
broadcastAddress = InetAddress.getByName(BROADCAST_IP);
//将该socket加入指定的多点广播地址
socket.joinGroup(broadcastAddress);
//设置本MulticastSocket发送的数据报被回送到自身
socket.setLoopbackMode(false);
//初始化发送用的DatagramSocket,它包含一个长度为0的字节数组
outPacket = new DatagramPacket(new byte[0] , 0 ,
broadcastAddress , BROADCAST_PORT);
//启动以本实例的run()方法作为线程体的线程
new Thread(this).start();
//创建键盘输入流
scan = new Scanner(System.in);
//不断读取键盘输入
while(scan.hasNextLine())
{
//将键盘输入的一行字符串转换字节数组
byte[] buff = scan.nextLine().getBytes();
//设置发送用的DatagramPacket里的字节数据
outPacket.setData(buff);
//发送数据报
socket.send(outPacket);
}
}
finally
{
socket.close();
}
}
public void run()
{
try
{
while(true)
{
//读取Socket中的数据,读到的数据放在inPacket所封装的字节数组里。
socket.receive(inPacket);
//打印输出从socket中读取的内容
System.out.println("聊天信息:" + new String(inBuff , 0 ,
inPacket.getLength()));
}
}
//捕捉异常
catch (IOException ex)
{
ex.printStackTrace();
try
{
if (socket != null)
{
//让该Socket离开该多点IP广播地址
socket.leaveGroup(broadcastAddress);
//关闭该Socket对象
socket.close();
}
System.exit(1);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
throws IOException
{
new MulticastSocketTest().init();
}
}
private DatagramSocket socket ;
private DatagramPacket inpacket ;
private DatagramPacket outpacket ;
public void run() {
int i =0;
try{
socket=new DatagramSocket(PORT);
while(true){
inpacket=new DatagramPacket(buff,buff.length);
socket.receive(inpacket) ;
System.out.println(new String(buff,0,inpacket.getLength()));
byte [] sedData =new String("数据信息:"+i).getBytes();
outpacket =new DatagramPacket(sedData,sedData.length,inpacket.getSocketAddress());
socket.send(outpacket);
i++ ;
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String []args){
new UDPServer().start();
}
/*public UDPServer (DatagramSocket socket ,DatagramPacket inpacket,
DatagramPacket outpacket) {
this.socket = socket ;
this.inpacket = inpacket ;
this.outpacket = outpacket ;
}*/
};
UDPServer.java:
import java.net.*;
import java.util.*;
public class UDPServer extends Thread {
private static final int PORT=10000 ;
private static final int DATA_LEN=2046 ;
private byte []buff =new byte[DATA_LEN];
UDPClient.java:
import java.net.*;
import java.util.*;
public class UDPClient extends Thread {
private static final int PORT=10000 ;
private static final int DATA_LEN=2046 ;
private byte []buff =new byte[DATA_LEN];
private DatagramSocket socket ;
private DatagramPacket inpacket =new DatagramPacket(buff,buff.length);
private DatagramPacket outpacket ;
public void run() {
int i =0;
try{
socket=new DatagramSocket();
outpacket =new DatagramPacket(new byte[0] ,0,
InetAddress.getByName("127.0.0.1"),PORT);
Scanner sc =new Scanner(System.in);
while(sc.hasNextLine()){
byte [] buff1 =sc.nextLine().getBytes();
outpacket.setData(buff1);
socket.send(outpacket);
socket.receive(inpacket);
System.out.println(new String(buff,0,inpacket.getLength()));
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String []args){
new UDPClient().start();
}
/*public UDPServer (DatagramSocket socket ,DatagramPacket inpacket,
DatagramPacket outpacket) {
this.socket = socket ;
this.inpacket = inpacket ;
this.outpacket = outpacket ;
}*/
};
MulticastSocketTest.java:
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
//让该类实现Runnable接口,该类的实例可作为线程的target
public class MulticastSocketTest implements Runnable
{
//使用常量作为本程序的多点广播IP地址
private static final String BROADCAST_IP
= "230.0.0.1";
//使用常量作为本程序的多点广播目的的端口
public static final int BROADCAST_PORT = 30000;
//定义每个数据报的最大大小为4K
private static final int DATA_LEN = 4096;
//定义本程序的MulticastSocket实例
private MulticastSocket socket = null;
private InetAddress broadcastAddress = null;
private Scanner scan = null;
//定义接收网络数据的字节数组
byte[] inBuff = new byte[DATA_LEN];
//以指定字节数组创建准备接受数据的DatagramPacket对象
private DatagramPacket inPacket =
new DatagramPacket(inBuff , inBuff.length);
//定义一个用于发送的DatagramPacket对象
private DatagramPacket outPacket = null;
public void init()throws IOException
{
try
{
//创建用于发送、接收数据的MulticastSocket对象
//因为该MulticastSocket对象需要接收,所以有指定端口
socket = new MulticastSocket(BROADCAST_PORT);
broadcastAddress = InetAddress.getByName(BROADCAST_IP);
//将该socket加入指定的多点广播地址
socket.joinGroup(broadcastAddress);
//设置本MulticastSocket发送的数据报被回送到自身
socket.setLoopbackMode(false);
//初始化发送用的DatagramSocket,它包含一个长度为0的字节数组
outPacket = new DatagramPacket(new byte[0] , 0 ,
broadcastAddress , BROADCAST_PORT);
//启动以本实例的run()方法作为线程体的线程
new Thread(this).start();
//创建键盘输入流
scan = new Scanner(System.in);
//不断读取键盘输入
while(scan.hasNextLine())
{
//将键盘输入的一行字符串转换字节数组
byte[] buff = scan.nextLine().getBytes();
//设置发送用的DatagramPacket里的字节数据
outPacket.setData(buff);
//发送数据报
socket.send(outPacket);
}
}
finally
{
socket.close();
}
}
public void run()
{
try
{
while(true)
{
//读取Socket中的数据,读到的数据放在inPacket所封装的字节数组里。
socket.receive(inPacket);
//打印输出从socket中读取的内容
System.out.println("聊天信息:" + new String(inBuff , 0 ,
inPacket.getLength()));
}
}
//捕捉异常
catch (IOException ex)
{
ex.printStackTrace();
try
{
if (socket != null)
{
//让该Socket离开该多点IP广播地址
socket.leaveGroup(broadcastAddress);
//关闭该Socket对象
socket.close();
}
System.exit(1);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
throws IOException
{
new MulticastSocketTest().init();
}
}
private DatagramSocket socket ;
private DatagramPacket inpacket ;
private DatagramPacket outpacket ;
public void run() {
int i =0;
try{
socket=new DatagramSocket(PORT);
while(true){
inpacket=new DatagramPacket(buff,buff.length);
socket.receive(inpacket) ;
System.out.println(new String(buff,0,inpacket.getLength()));
byte [] sedData =new String("数据信息:"+i).getBytes();
outpacket =new DatagramPacket(sedData,sedData.length,inpacket.getSocketAddress());
socket.send(outpacket);
i++ ;
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String []args){
new UDPServer().start();
}
/*public UDPServer (DatagramSocket socket ,DatagramPacket inpacket,
DatagramPacket outpacket) {
this.socket = socket ;
this.inpacket = inpacket ;
this.outpacket = outpacket ;
}*/
};
展开全部
看你的程序好像要实现多点传输(多播组), 可以参考MulticastSocket,也是基于UDP协议的 ,可以自动广播数据到连接上的客户端,不用等待每个客户端都要复制。那样如果数据量大,客户端多就太耗资源了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java 新IO就是NIO是专门用来非阻塞通信的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询