简体   繁体   English

使用套接字与服务器通信

[英]Communication with server using Socket

I am trying to send a message to a server and get the response. 我正在尝试向服务器发送消息并获得响应。 When i try to open a socket I get an exception : 当我尝试打开套接字时,出现异常:

I have added to AndroidManifest.xml the following lines: 我在AndroidManifest.xml中添加了以下几行:

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

Before the application tags. 在申请标签之前。
Here is the code i have: 这是我的代码:

                String IP = "81.218.150.49";
                int port = 32001;
                Socket my_socket = new Socket(IP, port); //Here i get exception !!

                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(my_socket.getOutputStream(), "UTF8"));
                wr.write("mobiwize server login test testpswd\r\n");

                // Send data
                wr.flush();

                // Get response
                StringBuffer sb = new StringBuffer("");
                BufferedReader rd = new BufferedReader(new InputStreamReader(my_socket.getInputStream()));
                String line ="";
                String NL = System.getProperty("line.separator");
                while ((line = rd.readLine()) != null) {
                    sb.append(line + NL);
                }
                httpStuff.setText(sb);

                rd.close();
                wr.close();
            } catch (Exception e) {
            }

I would really thank for any help! 我真的要感谢您的帮助!

Try this.... 尝试这个....

1. If you want to access this Server 1.如果要访问此服务器

with IP: 81.218.150.49 through Internet, then it must
   be your static ip, rather than an dynamic one.

2. Try to run this code with a private ip address or public ip address which is assigned to your pc in LAN (ie. Without internet..JUST WITH WIRELESS CONNECTION) . 2.尝试使用在局域网中分配给您的PC的专用IP地址或公共IP地址运行此代码(即, Without internet..JUST WITH WIRELESS CONNECTION) ..只需进行Without internet..JUST WITH WIRELESS CONNECTION)

3. Private ip or Public IP has No meaning until you are on INTERNET.. TILL THEN YOU CAN USE BOTH, AS ITS LAN. 3.私有ip或公用IP在您进入Internet之前没有任何意义。直到您可以同时使用它们作为其LAN。

4. Private ip ranges 4.专用IP范围

Class A : 10.0.0.0 - 10.0.0.255 Class B : 172.16.0.0 - 172.31.255.255 Class C : 192.168.0.0 - 192.168.255.255 A级:10.0.0.0-10.0.0.255 B级:172.16.0.0-172.31.255.255 C级:192.168.0.0-192.168.255.255

5. Public is given by your service provider, which will be anyone OUT of the private ip range. 5.公共是由您的服务提供商提供的,它将是私有IP范围之外的任何人。 If your ip is not static, there is hardly or none of your chances to access the server over internet, there are sites that gives static ip out of your dynamic ips. 如果您的IP不是静态的,则几乎没有机会或根本没有机会通过Internet访问服务器,有一些站点将动态IP赋予了静态IP。

I had the same issue when used in Android while it was running fine in J2ee Java program, I suggest you to use a method call for this new socket thing, use the same code. 在J2ee Java程序中运行良好时,在Android中使用时遇到了相同的问题,建议您对新的套接字使用方法调用,并使用相同的代码。 Hopefully it will help coz it did to me .. 希望它将对我有帮助。

I suspect you got security error. 我怀疑您遇到安全错误。 The IP address you listed does not allow you to make the socket connection. 您列出的IP地址不允许您建立套接字连接。 Try another IP that you know your computer can make connections with. 尝试使用另一个您可以与计算机建立连接的IP。 Test the IP for security with FTP, rlogin, or something. 使用FTP,rlogin或其他方法测试IP的安全性。

I assume you're not trying to hack someone's PC or iMac :-) 我认为您不是要入侵他人的PC或iMac :-)

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

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