简体   繁体   中英

Server is unreachable to client Android

I have a client phone and server pc. I am sending some data from phone to my pc. The problem is phone is unable to connect to the server & I get java.net.sockettimeout exception, which means the server is unreachable. However, the server is waiting for client to connect.

ServerCode:

    try
    {
         server = new ServerSocket(PORT_NUM);

         lblMessage.setText("waiting for client to connect");

         socket = server.accept();

         System.out.println("client connected");
         lblMessage.setText("client connect");


        serverInputStream = socket.getInputStream();


    } 
    catch (IOException e) {

        lblMessage.setText("exception: "+ e.toString());
        e.printStackTrace();
    }

ClientCode:

        InetAddress serverAddr = InetAddress.getByName(ip);

        Log.e("My Host", serverAddr.getHostName());


        socket = new Socket(serverAddr, port);

        Log.e("mysocket", socket.toString());

        output = socket.getOutputStream();

        Log.e("Data sending", "socket created");

    } catch (Exception e) {

        Log.e("Socket opening", e.toString());


    } 

This code worked yesterday but now it is not working. I have turned off Windows firewall, turned off antivirus also. Port of both client and server is same , but for some reason client is not connecting to server.

I am using Windows 7 and jelly bean 4.2.2 on my phone.

Regards

Check this out. This one is good example of posting data to the server. If you are specific about the port number, then append post upon completion of the url of your server.

Are you sending any data? like output.write("Hello world".getBytes()) if yes check if the ip address is right

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM