gpt4 book ai didi

java - "java.net.BindException: Can' t 分配请求地址 "on client' s 端

转载 作者:行者123 更新时间:2023-11-29 07:54:03 25 4
gpt4 key购买 nike

服务器:

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 条目:enter image description here

输出:

  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)

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

最佳答案

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

关于java - "java.net.BindException: Can' t 分配请求地址 "on client' s 端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19164213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com