服务器:
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)
为什么会出现这样的错误?我想要的只是向局域网内的服务器发送一个简单的信息。
答案 0 :(得分:5)
您提供的是非本地IP地址作为第二个参数,或者“端口”已在本地使用。为什么要指定本地地址和端口?不要那样做。只需删除这两个参数即可。让系统决定它们。