简体   繁体   English

TCP套接字编程在台式机和Android平板电脑之间不起作用

[英]TCP Socket programming not working between desktop and android tablet

i've done an application which send datas from server to clients....the code (server & client) works perfectly in desktop application, but when i put client on android tablet and server on desktop., its not working 我已经完成了一个将数据从服务器发送到客户端的应用程序。...代码(服务器和客户端)在桌面应用程序中可以完美运行,但是当我将客户端放在android平板电脑和服务器在桌面上时,它无法正常工作

i'm using wifi for communication between android and desktop 我正在使用wifi在android和桌面之间进行通信

permissions which i've set is as shown below 我设置的权限如下所示

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

can anyone please tell me what's the problem behind this..... 谁能告诉我这是什么问题.....

I'm getting this exception 我收到这个例外

java.net.ConnectException: failed to connect to /192.168.1.74 (port 4545): connect failed: ECONNREFUSED (Connection refused)

Server.java 服务器.java

public class Server
{
  private int TAB_SERVER_PORT = 4545;
  private static PrintWriter TABout;
  private HashSet <PrintWriter> TABhs=new HashSet<PrintWriter>();
  public static void main(String args[])
  {
    new Thread(new TABServerThread()).start();
  } 

    private class TABServerThread implements Runnable {
        private ServerSocket TABserver;
        @Override
        public void run() {
            try {
                TABserver = new ServerSocket(TAB_SERVER_PORT);
                System.out.println("Server Start the server at port " + TAB_SERVER_PORT + " and waiting for clients...");
                while (true) {
                    Socket socket = TABserver.accept();
                    System.out.println("Server Accept socket connection: "+ socket.getLocalAddress());
                    new Thread(new TABClientHandler(socket)).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private class TABClientHandler implements Runnable {
        private Socket clientSocket;
        private Scanner in;
        public TABClientHandler(Socket clietSocket) {
            this.clientSocket = clietSocket;
        }
        @Override
        public void run() {
            try {
                TABout = new PrintWriter(clientSocket.getOutputStream());
                TABhs.add(TABout);
                in = new Scanner(clientSocket.getInputStream());
                String line;
                while ((line = in.nextLine()) != null) {
                    if (line.equals("Reply")){
                        TABout.print("Server replies");
                        TABout.flush();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}

Client.java 客户端.java

public class Client implements Runnable {
            private int TAB_SERVER_PORT = 4545;
            private Socket tabletclient;
            private PrintWriter tabletout;
            private Scanner in;

            @Override
            public void run() {
                    String line;
                    boolean flag=true;
                    System.out.println("Waiting for server connection....");
                    while(flag){
                    try {
                        tabletclient = new Socket("192.168.1.74", TAB_SERVER_PORT);
                        tabletout = new PrintWriter(tabletclient.getOutputStream());
                        in = new Scanner(tabletclient.getInputStream());
                        try
                        {
                            if((line = in.nextLine())!=null)
                            {
                                System.out.println("Client Server says: " + line);
                                if (line.equals("Hello client")) {
                                    tabletout.println("Reply");
                                    tabletout.flush();
                                }
                            }
                        }catch(Exception d){
                            System.out.println("Connection from server has lost.........");
                        }
                        } catch (UnknownHostException e) {
                        } catch (IOException e) {
                        }
                     }
            }

 public static void main(String[] args) {
               new Thread(new Client()).start();

            }
        }

It must be the firewall that blocks the connections. 它必须是阻止连接的防火墙。 You can disable it and try again. 您可以禁用它,然后重试。 Should any more issues, please use wireshark to capture some packets to check where the problem is. 如果还有其他问题,请使用wireshark捕获一些数据包以检查问题出在哪里。

You can try "netstat -n" on both Linux or Windows to see if your server socket is binding and listening there. 您可以在Linux或Windows上都尝试“ netstat -n”,以查看服务器套接字是否已绑定并正在那里侦听。

Change your android application into a Client and Desktop application into a server. 将您的android应用程序更改为客户端,将桌面应用程序更改为服务器。 This way is much easier and safe(in my opinion of course).Your mobile devices ip can change any time due to... you know it is mobile.Your clients cant't guess the ip of their mobile server. 这种方式更加简单和安全(我认为当然)。由于...您知道它是移动的,因此您的移动设备ip可以随时更改。您的客户端无法猜测其移动服务器的ip。

move your Client.java to your android app and Server.java to your desktop app. 将Client.java移至android应用,将Server.java移至桌面应用。

Try to use "ifconfig" (if you working on Linux)to check your IP address or "ipconfig" if you working on Windows system; 尝试使用“ ifconfig”(如果您在Linux上工作)来检查IP地址,或者尝试使用“ ipconfig”(如果您在Windows系统上工作); make sure your IP address is exactly "192.168.1.74". 确保您的IP地址恰好是“ 192.168.1.74”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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