我正在尝试将客户端的 Socket 绑定到任何特定的本地端口,在这段代码中我使用了 20000。
如下所示的正常连接工作得很好。但不允许我选择本地端口。
hostSocket = new Socket(host,80);
所以我尝试了这个:
hostSocket = new Socket(host, 80, InetAddress.getLocalHost(), 20000);
还有这个:
hostSocket = new Socket();
hostSocket.bind(new InetSocketAddress("localhost", 20000));
hostSocket.connect(new InetSocketAddress(host,80));
但是他们都给我留下了这个例外......在第二种情况下,异常发生在连接调用上。我不太确定我错过了什么,我会喜欢一些指针。
java.net.SocketException: Invalid argument or cannot assign requested address
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at com.mb.proxy.netflix.NetflixPrefetchingAgent.connect(NetflixPrefetchingAgent.java:98)
at com.mb.proxy.netflix.NetflixPrefetchingAgent.run(NetflixPrefetchingAgent.java:164)
at java.lang.Thread.run(Thread.java:679)
Exception in thread "Thread-19" java.lang.NullPointerException
at com.mb.proxy.netflix.NetflixPrefetchingAgent.prefetchChunk(NetflixPrefetchingAgent.java:272)
at com.mb.proxy.netflix.NetflixPrefetchingAgent.run(NetflixPrefetchingAgent.java:176)
at java.lang.Thread.run(Thread.java:679)