sock.connect IOException on android emulator on Macintosh laptop

  • Replies:0
Ashok449
  • Forum posts: 2

Apr 6, 2014, 10:39:54 PM via Website

Hello All,

I'm working on Android socket client application, and Server is developed in Java.
My socket client is running on ANDROID EMULATOR (Macintosh) and Server is Running on PC.(Macintosh)

Socker client app get IP address and message from Edit Test and will be sent on button, Here is the code. On button click I'm getting IOexception at `sock.connect(sockaddr);`, Why I'm getting this ?
`Log.i("MyApp", "sockaddr: "+addrs.toString() ); is /10.30.114.14:6666` is it because of extra '/' character before IP !?
Server is okay, I have tested with Telnet .


I have permissions in manifesto file

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

I appreciate your support, Thanks.

1public class WiFiConnectActivity extends Activity {
2
3 /** Called when the activity is first created. */
4 EditText ip, msg;
5 ToggleButton connect;
6
7 InetAddress addr=null;
8 SocketAddress sockaddr=null;
9 int port = 5657;
10 Socket sock = null;
11 DataOutputStream dataOutputStream ;
12
13
14 Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener()
15 {
16
17 public void onClick(View v) {
18 String str = ip.getText().toString();
19 // Toast.makeText(getApplicationContext(),str , Toast.LENGTH_LONG).show();
20 // connect.setChecked (true);
21
22
23 try{
24 Log.i("MyApp", "ipaaddress " + str);
25 addr = InetAddress.getByName(str);
26 // Log.i("MyApp", "inetaddr:" +addr.getHostAddress());
27
28 // InetSocketAddress addrs = new InetSocketAddress(str, port);
29 // System.out.println(address.getAddress().getHostAddress());
30 sockaddr = new InetSocketAddress(str, port);
31 sock = new Socket();
32 if(sock != null)
33 {
34 Log.i("MyApp", "Socket() is okay ");
35 Log.i("MyApp", "sockaddr: "+sockaddr.toString() );
36 // Log.i("MyApp", "sockaddr1: "+addrs.getAddress() );
37
38 }
39
40 sock.connect(sockaddr);//, 20000);
41
42 // Toast.makeText(getApplicationContext(), "Sending Data", Toast.LENGTH_LONG).show();
43
44 dataOutputStream = new DataOutputStream(sock.getOutputStream());
45 dataOutputStream.writeUTF(msg.getText().toString());//msg);
46 }
47 catch (UnknownHostException e)
48 {
49
50 e.printStackTrace();
51 }
52 catch (SocketTimeoutException e)
53 {
54
55 }
56 catch (IOException e)
57 {
58 e.printStackTrace();
59 }
60 finally
61 {
62 //close socket
63 if (sock != null)
64 {
65 try
66 {
67 sock.close();
68 }
69 catch (IOException e)
70 {
71 e.printStackTrace();
72 }
73 }
74 if (dataOutputStream != null)
75 {
76 try
77 {
78 dataOutputStream.close();
79 }
80 catch (IOException e)
81 {
82 // TODO Auto-generated catch block
83 e.printStackTrace();
84 }
85 }
86 }
87
88
89 }
90};
91
92 @Override
93 public void onCreate(Bundle savedInstanceState) {
94 super.onCreate(savedInstanceState);
95 setContentView(R.layout.main);
96 ip = (EditText)findViewById(R.id.editText1);
97 msg = (EditText)findViewById(R.id.editText2);
98 msg.setHint("ur message");
99 connect = (ToggleButton) findViewById(R.id.connect);
100 //connect.setText("Connect");
101 connect.setOnClickListener(buttonConnectOnClickListener);
102 // new Thread(new ClientThread()).start();
103
104 }
105
106
107
108}

Reply