0

I am trying to implement java socket client in android and my server is my laptop. I want my android mobile to connect to laptop by java socket. But i am getting exception on client side: "SocketException" and it looks to me because of the socket() call not able to create socket properly. Below is my android client side code for review. Is there any solution to the resolve the exception:

public class SimpleActivityExampleActivity extends Activity  {

/** Called when the activity is first created. */
private String usrName;
private String vendorName;
private String message;
public Socket socket;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }


public void submitTheForm(View view) {

    final EditText edittext1 = (EditText) findViewById(R.id.editText1);
    final EditText edittext2 = (EditText) findViewById(R.id.EditText01);
    final EditText edittext3 = (EditText) findViewById(R.id.editText2);
    usrName = edittext1.getText().toString(); 
    vendorName = edittext2.getText().toString();
    message = edittext3.getText().toString();
    message = usrName+ "," + vendorName + "," + message;

    byte[] msg = message.getBytes();
    try {
    InetAddress serverAddr = InetAddress.getByName("192.168.1.2");
        Log.d("ClientActivity", "C: Connecting...");

        socket = new Socket(serverAddr, 2200);
    socket.getOutputStream().write(msg);
    socket.getOutputStream().flush();
    } catch (UnknownHostException e) {

        Toast.makeText(SimpleActivityExampleActivity.this, "unknownhostException", Toast.LENGTH_SHORT).show();
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SocketException e) {
        // TODO Auto-generated catch block

        Toast.makeText(SimpleActivityExampleActivity.this, "Sockexception", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }catch (IOException e) {
        // TODO Auto-generated catch block
        //System.out.println("in IOexception for sure");
        Toast.makeText(SimpleActivityExampleActivity.this, "IOexception", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
    catch (Exception e){
        Toast.makeText(SimpleActivityExampleActivity.this, "Exception", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }finally {
        if(socket != null){
            try {
                socket.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
} 

submitTheForm() function is invoked on a button click from main.xml file "android:onClick = "submitTheForm"

Casey
  • 12,070
  • 18
  • 71
  • 107
Jaimin Jetly
  • 63
  • 1
  • 1
  • 5

2 Answers2

0

Is it possible that you forgot to add the Internet permission to your manifest file?

<uses-permission android:name="android.permission.INTERNET" />
Knickedi
  • 8,742
  • 3
  • 43
  • 45
0

Have you set the

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

permission?

Found this here

Community
  • 1
  • 1
Theblacknight
  • 575
  • 5
  • 12