UDP编程出现java.io.IOException: Invalid argument 异常

hu0905074231 2012-04-15 04:21:14
import java.io.*;
import java.net.*;
/**
*UDP Echo Client
*/
public class MainClass1
{
public static void main(String[] args)throws Exception
{
String hostname="localhost";
InetAddress ia=InetAddress.getByName(hostname);
//send thread
SenderThread sender=new SenderThread(ia,1919);
sender.start();
//receive thread
Thread receiver=new ReceiverThread();
receiver.start();
}
}
/**
*Sender Thread
*/
class SenderThread extends Thread
{
private InetAddress server;
private DatagramSocket socket;
private boolean stopped=false;
private int port;
//constructor
public SenderThread(InetAddress address,int prot)throws SocketException
{
this.server=address;
this.port=port;
this.socket=new DatagramSocket();
//this.socket.connect(server,port);//connect the socket 远程地址
}
//stop thread
public void halt()
{
this.stopped=true;
}
//get DatagramSocket
public DatagramSocket getSocket()
{
return this.socket;
}
//void run
public void run()
{
try
{
BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
if(stopped)
return;
String theLine =userInput.readLine();
if(theLine.equals("."))
break;
byte[] data=theLine.getBytes();
DatagramPacket pack=new DatagramPacket(data,data.length,server,port);//构造制定长度数据包,用于发送到指定地址的指定端口
socket.send(pack);
Thread.yield();//指定该线程处于可执行状态
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
/**
*Receiver Thread
*/
class ReceiverThread extends Thread
{
DatagramSocket socket;
private boolean stopped=false;
//constructor
public ReceiverThread()throws SocketException
{
socket=new DatagramSocket(1919);
}
//stop thread
public void halt()
{
this.stopped=true;
}
//run
public void run()
{
byte[] buffer=new byte[65507];
while(true)
{
if(stopped)
return;
DatagramPacket dp=new DatagramPacket(buffer,buffer.length);//create a packet to recieive
try
{
socket.receive(dp);
String s=new String(dp.getData(),0,dp.getLength());//get data
System.out.println(s);
Thread.yield();
}
catch(IOException e)
{
System.err.println(e);
}
}
}


}

编译没问题,运行后,我输入任何内容按回车,就报出如下异常:

java.io.IOException: Invalid argument
at java.net.PlainDatagramSocketImpl.send(Native Method)
at java.net.DatagramSocket.send(DatagramSocket.java:629)
at SenderThread.run(MainClass1.java:62)
这是什么情况,求指点!!
...全文
675 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
hu0905074231 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

如果你用的是eclipse,这种错误是可以查出来,就是有一条黄线在该行上面,你就要注意了。说明可能值没赋上
[/Quote]
噢,我现在用的是linux下的vim,习惯了,对于这些小demo一直用vim写的。
sdojqy1122 2012-04-15
  • 打赏
  • 举报
回复
如果你用的是eclipse,这种错误是可以查出来,就是有一条黄线在该行上面,你就要注意了。说明可能值没赋上
hu0905074231 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

prot写错了导致的吧?pack因为port异常,所以是不可用的参数吧。
[/Quote]
太感谢了,就是那个port的单词写成了prot了,唉,太粗心了。一下子导致port的值没有传递过去。再次感谢。
sdojqy1122 2012-04-15
  • 打赏
  • 举报
回复
prot写错了导致的吧?pack因为port异常,所以是不可用的参数吧。
sdojqy1122 2012-04-15
  • 打赏
  • 举报
回复
我只发现你的port的是sendThread的,而且还没有赋初始值啊。
hu0905074231 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

public SenderThread(InetAddress address,int prot)throws SocketException
{
this.server=address;
this.port=port;
this.socket=new DatagramSocket();
//this.s……
[/Quote]
DatagramPacket pack=new DatagramPacket(data,data.length,server,port);//构造制定长度数据包,用于发送到指定地址的指

直接在DatagramPacket里面指定目的端口。
hu0905074231 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

public SenderThread(InetAddress address,int prot)throws SocketException
{
this.server=address;
this.port=port;
this.socket=new DatagramSocket();
//this.s……
[/Quote]
DatagramPacket pack=new DatagramPacket(data,data.length,server,port);//构造制定长度数据包,用于发送到指定地址的指

直接在DatagramPacket里面指定目的端口。
hu0905074231 2012-04-15
  • 打赏
  • 举报
回复
DatagramPacket pack=new DatagramPacket(data,data.length,server,port);//构造制定长度数据包,用于发送到指定地址的指

直接在DatagramPacket里面指定目的端口。
sdojqy1122 2012-04-15
  • 打赏
  • 举报
回复
public SenderThread(InetAddress address,int prot)throws SocketException
{
this.server=address;
this.port=port;
this.socket=new DatagramSocket();
//this.socket.connect(server,port);//connect the socket 远程地址
}
这个port呢?
hu0905074231 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

Java code

//this.socket.connect(server,port);//connect the socket 远程地址


注掉了?
[/Quote]
我觉的这句没用,就注释掉了,注释不注释都会那个异常。
sdojqy1122 2012-04-15
  • 打赏
  • 举报
回复

//this.socket.connect(server,port);//connect the socket 远程地址

注掉了?
hu0905074231 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

无效参数?
[/Quote]
能具体么?
txzsp 2012-04-15
  • 打赏
  • 举报
回复
无效参数?
hu0905074231 2012-04-15
  • 打赏
  • 举报
回复
求大神们看看
hu0905074231 2012-04-15
  • 打赏
  • 举报
回复
/**
*Sender Thread
*/
class SenderThread extends Thread
{
private InetAddress server;
private DatagramSocket socket;
private boolean stopped=false;
private int port;
//constructor
public SenderThread(InetAddress address,int prot)throws SocketException
{
this.server=address;
this.port=port;
this.socket=new DatagramSocket();
//this.socket.connect(server,port);//connect the socket 远程地址
}
//stop thread
public void halt()
{
this.stopped=true;
}
//get DatagramSocket
public DatagramSocket getSocket()
{
return this.socket;
}
//void run
public void run()
{
try
{
BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
if(stopped)
return;
String theLine =userInput.readLine();
if(theLine.equals("."))
break;
byte[] data=theLine.getBytes();
DatagramPacket pack=new DatagramPacket(data,data.length,server,port);//构造制定长度数据包,用于发送到指定地址的指定端口
socket.send(pack);//这里是异常中的第62行
Thread.yield();//指定该线程处于可执行状态
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
第62行我在注释中表明了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧