【问题标题】:"java.net.BindException: Can't assign requested address" on client's side客户端的“java.net.BindException:无法分配请求的地址”
【发布时间】:2013-10-03 16:20:15
【问题描述】:

服务器:

import java.net.*;
import java.io.*;

import javax.swing.text.html.HTMLEditorKit;

public class TCPReceiver implements Runnable
{
    private Settings s;
    private int portTCP;
    private BlackAndWhite2GUI gui;
    private ServerSocket ss = null;
    private InputStream in = null;

    public TCPReceiver(Settings s,BlackAndWhite2GUI gui)
    {
        this.s = s;
        this.gui = gui;
    }        

    @Override 
    public void run()
    {
        startTCP();
    }

    public void startTCP()
    {
        final int BACKLOG=100;
        final int BUFSIZE=32;
        int recVMsgSize;

        Socket client = null;        

        byte[] receiveBuf = new byte[BUFSIZE];
        String content = new String();

        try
        {
           ss=new ServerSocket(s.insideLocalPort,BACKLOG,s.insideLocal); //Port "0" means that the function will automatically set this property
           portTCP = ss.getLocalPort();
           s.outsideLocalPort = (s.insideLocalPort = portTCP);
           gui.updateTable();

           while(true)
           {
               client = ss.accept();//wait for incomming connections
               in = client.getInputStream();

               while((recVMsgSize = in.read(receiveBuf)) != -1)
               {    
                   System.out.println("Receiving TCP window...");
                   content+=(receiveBuf.toString());
               }

               gui.appendMessage(new MessageEntity(client.getInetAddress(),client.getPort(),content));

               in.close();
               client.close();//We are done with this client!
           }
        }
        catch(SocketException e)
        {
            e.printStackTrace();
        }    
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    public int getPort()
    {
        return portTCP;
    }

    public void stopServer()
    {
        try
        {    
            if(in != null) in.close();
            if(ss != null) ss.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }    
    }        
}

客户:

import java.net.*;
import java.io.*;


public class SocketNatTest()
{
      public static void main(String[] args)
      {
          if(args[0].equals("--help")
          {
               showHelp();
               return;
          }

          Inet4Address global = null;
          Inet4Address local = null;          

           try
           {
                global = (Inet4Address)InetAddress.getByName(args[0]);
                local = (Inet4Address)InetAddress.getByName(args[1]);
           }
           catch(UnknownHostException e)
           {
               e.printStackTrace();
           }

           int port = Integer.parseInt(args[2]);

           byte[] content = args[4].getBytes();

           if(args[3].equals("UDP")) sendUDP(global,local,port,content);
           if(args[4].equals("TCP")) sendTCP(global,local,port,content);
     }

      public static void sendTCP(Inet4Address global,Inet4Address local,int port,byte[] content)
      {
          Socket s = null;
          OutputStream os = null;

          try
          {
               s = new Socket(global,port,local,port);
               os = s.getOutputStream();

               os.write(content);

               s.close();
               os.close();
          }
          catch(IOException e)
          {
              e.printStackTrace();
          }
    }                          
}

调用:

java SocketNatTest 83.6.243.[rest of server's inside global ip] 192.168.1.111 52247 TCP Hello

服务器端的Netstat条目:

输出:

  java SocketNatTest 83.6.243.[censored] 192.168.1.111 52247 TCP Hello

java.net.BindException: Can't assign requested address
        at java.net.PlainSocketImpl.socketBind(Native Method)
        at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:353)
        at java.net.Socket.bind(Socket.java:594)
        at java.net.Socket.<init>(Socket.java:390)
        at java.net.Socket.<init>(Socket.java:293)
        at SocketNatTest.sendTCP(SocketNatTest.java:48)
        at SocketNatTest.main(SocketNatTest.java:38)

为什么会出现这样的错误?我想要的只是向局域网内的服务器发送一个简单的信息。

【问题讨论】:

    标签: java sockets nat


    【解决方案1】:

    您提供了一个非本地 IP 地址作为第二个参数,或者“端口”已在本地使用。为什么要指定本地地址和端口?不要那样做。只需删除这两个参数。让系统来决定。

    【讨论】:

    • 应用您的建议后,程序会静默冻结。
    • 所以它连接了,对吗?现在这是一个新问题,对吧?这将是由于在事件线程上进行阻塞操作造成的。
    • 是的,问题仍然存在,因为程序无法运行。 pastebin.com/KfgAX1K6 - 就是你的意思吗?
    • 附言。运行客户端的效果总是一样的,不管服务器是开还是关。
    • 我不知道您的 pastebin 应该是什么,也不知道您为什么认为它“正是 [我] 的意思”。我的意思是我所说的,没有别的。如果您的程序现在冻结,(a) 您已连接,因此该问题已得到回答,并且 (b) 您有一个新问题,需要一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2012-05-30
    • 1970-01-01
    • 2022-01-16
    • 2020-09-09
    • 2016-06-23
    相关资源
    最近更新 更多