简体   繁体   中英

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:

< 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

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) .

3. Private ip or Public IP has No meaning until you are on INTERNET.. TILL THEN YOU CAN USE BOTH, AS ITS LAN.

4. Private ip ranges

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

5. Public is given by your service provider, which will be anyone OUT of the private ip range. 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.

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. 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. Try another IP that you know your computer can make connections with. Test the IP for security with FTP, rlogin, or something.

I assume you're not trying to hack someone's PC or iMac :-)

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