简体   繁体   English

Android和PC之间通过WIFI的客户端服务器

[英]Client-Server between Android and PC over WIFI

I am testing the communication between the Android and my Laptop. 我正在测试Android和我的笔记本电脑之间的通信。

I have a Lan connection and I shared the Internet through the laptops WiFi 我有局域网连接,并且通过笔记本电脑的WiFi共享了Internet

Now I have created a Server on laptop with the code 现在,我用代码在笔记本电脑上创建了一个服务器

            public void run()
    {
        try {                       
            server_soc = new ServerSocket(1234);


            display.append("Server Started on port : " + server_soc);
            display.append("\nWaiting for client to connect ");

            while(true){
                cli=server_soc.accept();

                display.append("\nClient connected ");

                Accept_Client accept_client=new Accept_Client(cli);
                accept_client.start();
            }

the server starts successfully. 服务器成功启动。 服务器

I have to connect an android client (android 4.0.4) to this server. 我必须将android客户端(android 4.0.4)连接到此服务器。 I am not able to connect to this Server. 我无法连接到该服务器。 What should be the IP used to connect to this server ? 用于连接到该服务器的IP应该是什么?

Client (android) code : 客户端(android)代码:

        Connect.setOnClickListener(new OnClickListener() {      
    @Override
    public void onClick(View arg0) {

        try {

            Server = new Socket("10.0.2.2",1234);
            Server_Msg.setText("connected to Server ");         


        } catch (Exception e) {

            e.printStackTrace();
        } 
    }
});

I tried all the IPs :192.168.137.1, 123.238.143.130 What should be the IP ? 我尝试了所有IP:192.168.137.1,123.238.143.130 IP应该是什么?

I have even set up port forwarding in my wireless router. 我什至在我的无线路由器中设置了端口转发。 Do I need to do anything in firewall ? 我需要在防火墙中做任何事情吗? Please suggest ! 请提出建议!

To connect from the emulator to a WiFi server on localhost , use the IP 10.0.2.2 To connect from an Android device to a server you're running on your own network, use the IP of the Wifi server. 要将仿真器连接到localhost上的WiFi服务器,请使用IP 10.0.2.2若要将Android设备连接到您自己网络上运行的服务器,请使用Wifi服务器的IP。

Make sure the Internet permissions are set in the Android Manifest file: As a child node to the node, insert: 确保在Android Manifest文件中设置了Internet权限:作为该节点的子节点,插入:

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

In case of Server-client system using real android device you just have to make sure that the port you are listening to is added in firewall. 如果服务器-客户端系统使用的是真正的android设备,则只需确保将您正在侦听的端口添加到防火墙中即可。

The server will listen to that port and all the connections coming on that port from any IP will be connected. 服务器将侦听该端口,并且将连接来自该端口的所有IP的所有连接。

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

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