Pregunta

Server:

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();
        }    
    }        
}

Client:

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();
          }
    }                          
}

Invocation:

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

Netstat entry on server's side:enter image description here

Output:

  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)

Why such error occurs? All I want is send a simple information to the server inside LAN.

¿Fue útil?

Solución

You're supplying a non-local IP address as the second argument, or 'port' is already in use locally. Why are you specifying a local address and port at all? Don't do that. Just remove those two parameters. Let the system decide them.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top