繁体   English   中英

客户端的“ java.net.BindException:无法分配请求的地址”

[英]“java.net.BindException: Can't assign requested address” on client's side

服务器:

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。[服务器内部其余的全局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)

为什么会发生这种错误? 我想要的只是向LAN内的服务器发送简单的信息。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM