Socket Programming

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Re: Socket Programming

Postby mshankayi » Sat Mar 19, 2011 8:31 pm

@qamerfarooq : can you send the entire projects ? because i can not run it properly because it has been encountered error in my device
Posts: 5
Joined: Sat Mar 19, 2011 8:09 pm


Re: Socket Programming

Postby akhwaja » Fri Apr 08, 2011 8:43 am

can you please guide me how to connect to server socket[in c#] from an android app[client socket].. I have used the following code for client app in android. I have server app built in C# plus i have client app in C# as well.. the client app created in C# successfully connected with the server app but the client app for android gives Socket timeout exception when it tries to connect.. please help what should i do.. my pc is on network and server app is running over tht and my android phone is running over wifi… is tht what causing problem… waiting for your reply
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void connectSocket()
  2. {
  3. // TODO Auto-generated method stub
  4. try
  5. {
  6. //InetAddress serverAddr = InetAddress.getByName(SERVERIP);
  7. InetSocketAddress remoteAddr = new InetSocketAddress(SERVERIP, 8221);
  8. Log.d(“TCP”, “C: Connecting…”);
  9. //Socket socket = new Socket(serverAddr, 8221);
  10. Socket socket = new Socket();
  11. socket.connect(remoteAddr,0);
  12. String message = “Hello from Client android emulator”;
  13. try
  14. {
  15. Log.d(“TCP”, “C: Sending: ‘” + message + “‘”);
  16. PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
  17. out.println(message);
  18. Log.d(“TCP”, “C: Sent.”);
  19. Log.d(“TCP”, “C: Done.”);
  20. }
  21. catch (Exception e)
  22. {
  23. Log.e(“TCP”, “S: Error”, e);
  24. Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
  25. }
  26. finally
  27. {
  28. socket.close();
  29. }
  30. }
  31. catch (UnknownHostException e)
  32. {
  33. // TODO Auto-generated catch block
  34. Log.e(“TCP”, “C: UnknownHostException”, e);
  35. Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
  36. e.printStackTrace();
  37. }
  38. catch (IOException e)
  39. {
  40. // TODO Auto-generated catch block
  41. Log.e(“TCP”, “C: IOException”, e);
  42. Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
  43. e.printStackTrace();
  44. }
  45. }
Parsed in 0.036 seconds, using GeSHi
Once Poster
Once Poster
Posts: 1
Joined: Fri Apr 08, 2011 8:38 am

Re: Socket Programming

Postby mkl » Wed Apr 04, 2012 4:59 pm

Well I got some problems...

I sould send a TCP message to a busineslogic which detect the message and react. So for example if i send the message "request_complete_list" the busineslogic will send me some informations back! I´m working on Windows 7 with the Eclipse + Android SDK. The businesslogic running on Linux.

The other workers worked with some QStrings and used the method .toAscii() because the allowance which i got is to send all messages as Ascii.

They used a command like this:

Code: Select all

I´m sending the message like this:

Code: Select all
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStream Writer(s.getOutputStream())),true);
String test = "request_complete_list\\x04";

The problem is the busineslogic just react if the string is correct. So if I´m sending a wrong message i can´t see the problem...

Maybe some of you know how i can solve the problem?! I think it could be because the message i send with out.println(test) is a String in Unicode? and not in Ascii...

I also tried some workaround:

Code: Select all
ByteBuffer buffer1 = ByteBuffer.allocate(100);
for(int i = 0; i < test.length(); ++i)
char c = test.charAt(i);
int j = (int) c;

But no result...

So please if some of you have any idea just post it...
Once Poster
Once Poster
Posts: 1
Joined: Wed Apr 04, 2012 2:23 pm


Return to General

Who is online

Users browsing this forum: No registered users and 2 guests