【问题标题】:Wifi Socket address not valid,AndroidWifi Socket地址无效,Android
【发布时间】:2012-07-02 20:57:40
【问题描述】:

每当我创建一个ServerSocket 并通过调用getLocalSocketAddress() 查看套接字地址时,我都会看到:

0.0.0.0/0.0.0.0:xxxx(xxxx是随机端口号)

我的服务器代码是:

try{
    Boolean end = false;
    ServerSocket ss = new ServerSocket(0);
    System.out.println("Program running, Server address:" + ss.getLocalSocketAddress().toString());
    while(!end){
        //Server is waiting for client here, if needed

        Socket s = ss.accept();
        System.out.println("Socket Connected !");  
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
        String st = input.readLine();
        System.out.println("Tcp Example From client: "+st);
        output.println("Good bye and thanks for all the fish :)");
        s.close();

    }
    ss.close();  
 } catch (Exception ex) {
     ex.printStackTrace();
 }

【问题讨论】:

  • 注意:1.你应该使用Log而不是System.out.println() 2.ss超出范围,例如编译器应该抱怨它是一个未知变量。
  • 很抱歉没有粘贴整个代码。没有编译器错误,只是因为整个代码没有进入我的问题中的代码部分,这就是为什么我没有复制整个代码

标签: java android sockets


【解决方案1】:

不要在ServerSocket中分配“0”

端口是从 0-65535 ,但要使用它的 1 - 65534.. MoreOver 尝试使用 1024 以上的端口,因为它被其他知名服务使用,如 Telnet、FTP、HTTP 等。 .

查看我的代码...它可能会帮助您执行代码.....

服务器端代码示例:

    public class ServerTest {

        ServerSocket s;

        public void go() {

            try {
                s = new ServerSocket(4445);

                while (true) {

                    Socket incoming = s.accept();
                    Thread t = new Thread(new MyCon(incoming));
                    t.start();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }

        }

        class MyCon implements Runnable {

            Socket incoming;

            public MyCon(Socket incoming) {

                this.incoming = incoming;
            }

            @Override
            public void run() {

                try {
                    PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
                            true);
                    InputStreamReader isr = new InputStreamReader(
                            incoming.getInputStream());
                    BufferedReader br = new BufferedReader(isr);
                    String inp = null;

                    boolean isDone = true;

                    System.out.println("TYPE : BYE");
                    System.out.println();
                    while (isDone && ((inp = br.readLine()) != null)) {

                        System.out.println(inp);
                        if (inp.trim().equals("BYE")) {
                            System.out
                                    .println("THANKS FOR CONNECTING...Bye for now");
                            isDone = false;
                            s.close();
                        }

                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    try {
                        s.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    e.printStackTrace();
                }

            }

        }

        public static void main(String[] args) {

            new ServerTest().go();

        }

}

【讨论】:

  • 我认为现在问题不在于服务器代码,我正在尝试在 android 设备和我的计算机之间交换数据包。那么在客户端代码中应该使用什么 Socket 地址呢?
  • @Kumar Vivek Mitra:根据文档:public ServerSocket (int port) - Constructs a new ServerSocket instance bound to the given port. The backlog is set to 50. If port == 0, a port will be assigned by the OS.
  • @bicska88 我有一个疑问..客户端如何知道服务器在哪个端口上运行...如果它是由操作系统分配的。
  • @Kumar Vivek Mitra:你说的是对的,我想指出的是,这绝对是不是问题的原因。
  • @David Kroukamp,为什么要使用公共 IP,他是否有静态公共 IP 地址,我敢打赌,如果它是他的家用电脑..它将使用 NAT,如果是这种情况,他需要使用一些将他的动态 ip 设置为静态的机制...现在,如果他想运行该应用程序来试用代码..他可以使用任何 IP、公共或私人 IP,除非他在互联网上使用该应用程序,否则这将无关紧要。 ..
猜你喜欢
  • 2012-01-31
  • 2012-05-27
  • 2011-12-20
  • 2013-05-19
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
相关资源
最近更新 更多