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)
这是什么情况,求指点!!

解决方案 »

  1.   

    /**
      *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行我在注释中表明了
      

  2.   


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

  3.   

        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呢?
      

  4.   

    DatagramPacket pack=new DatagramPacket(data,data.length,server,port);//构造制定长度数据包,用于发送到指定地址的指
    直接在DatagramPacket里面指定目的端口。
      

  5.   

    DatagramPacket pack=new DatagramPacket(data,data.length,server,port);//构造制定长度数据包,用于发送到指定地址的指
    直接在DatagramPacket里面指定目的端口。
      

  6.   

    DatagramPacket pack=new DatagramPacket(data,data.length,server,port);//构造制定长度数据包,用于发送到指定地址的指
    直接在DatagramPacket里面指定目的端口。
      

  7.   

    我只发现你的port的是sendThread的,而且还没有赋初始值啊。
      

  8.   

    prot写错了导致的吧?pack因为port异常,所以是不可用的参数吧。
      

  9.   

    太感谢了,就是那个port的单词写成了prot了,唉,太粗心了。一下子导致port的值没有传递过去。再次感谢。
      

  10.   

    如果你用的是eclipse,这种错误是可以查出来,就是有一条黄线在该行上面,你就要注意了。说明可能值没赋上
      

  11.   

    噢,我现在用的是linux下的vim,习惯了,对于这些小demo一直用vim写的。