title: Socket - C server , Android client
body: <p><strong>Hello i have made a C server and an android client made in java.  , im trying to let the server send the string vibrate and let the phone vibrate for some time. i can see that the client is connected but i dont think the server actualy send any data at all  , after i press enter to quit the program for getchar() this error pops up in logcat.</strong></p>
  
  <p>logcat error </p>
  
  <pre><code>01-28 14:10:50.400: W/System.err(30663): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
  01-28 14:10:50.400: W/System.err(30663):    at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:552)
  01-28 14:10:50.400: W/System.err(30663):    at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
  01-28 14:10:50.400: W/System.err(30663):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
  01-28 14:10:50.400: W/System.err(30663):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
  01-28 14:10:50.400: W/System.err(30663):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
  01-28 14:10:50.400: W/System.err(30663):    at java.io.InputStreamReader.read(InputStreamReader.java:244)
  01-28 14:10:50.410: W/System.err(30663):    at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
  01-28 14:10:50.410: W/System.err(30663):    at java.io.BufferedReader.readLine(BufferedReader.java:390)
  01-28 14:10:50.410: W/System.err(30663):    at com.example.rat.RatService$ClientThread.run(RatService.java:76)
  01-28 14:10:50.410: W/System.err(30663):    at java.lang.Thread.run(Thread.java:864)
  01-28 14:10:50.410: W/System.err(30663): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
  01-28 14:10:50.420: W/System.err(30663):    at libcore.io.Posix.recvfromBytes(Native Method)
  01-28 14:10:50.420: W/System.err(30663):    at libcore.io.Posix.recvfrom(Posix.java:131)
  01-28 14:10:50.420: W/System.err(30663):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
  01-28 14:10:50.420: W/System.err(30663):    at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
  01-28 14:10:50.420: W/System.err(30663):    ... 8 more
  </code></pre>
  
  <p>C server code.</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
    WSADATA wsa;
    SOCKET s, new_socket;
    struct sockaddr_in server, client;
    int c;
    char *message;
  
    if (WSAStartup(MAKEWORD(2, 2), &amp;wsa) != 0)
    {
      printf("ERROR initlizaing winsock2 dll : %d", WSAGetLastError());
      return 1; // exit program with error.
    }
  
    printf("INitialised.\n");
  
    // create a socket
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s == INVALID_SOCKET)
    {
      printf("error at creating socket :%d", WSAGetLastError());
      WSACleanup();
      return 1;
    }
  
    printf("socket created");
  
    //Sockad_addr in strucsture prepare
    memset(&amp;server, 0, sizeof server);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(5000);
  
    // bind the socket
  
    if (bind(s, (struct sockaddr *) &amp;server, sizeof(server)) == SOCKET_ERROR)
    {
      printf("Bind fialed with errror code : %d", WSAGetLastError());
      closesocket(s);
      WSACleanup();
      return 1;
    }
  
    listen(s, 3);
  
    printf("waiting for incoming connections");
    // this part is what i dont understand.
    c = sizeof(struct sockaddr_in);
    new_socket = accept(s, (struct sockaddr *) &amp;client, &amp;c);
    if (new_socket == INVALID_SOCKET)
    {
      printf("accept fialed with error code : %d", WSAGetLastError());
  
      closesocket(s);
      WSACleanup();
      return 1;
    }
  
    printf("Connection acceptëd");
  
    //reply to CLIENTCREATESTRUCT
    message = "vibrate";
    send(new_socket, message, strlen(message), 0);
    getchar();
    closesocket(s);
    WSACleanup();
    return 0;
  }
  </code></pre>
  
  <p>ANdroid java client  , </p>
  
  <pre><code>public class Service extends Service{
      private boolean isRunning = false;
      private static final  String SERVER_IP = "192.168.178.11";
      private static final int SERVERPORT = 5000;
      private Socket socket;
      Thread ClientThread = null;
      String st = null;
  
  
  
      public void onCreate() {
  
      }
  
  
      public int onStartCommand(Intent intent, int flags, int startId){
          super.onStartCommand(intent, flags, startId);
  
          // Annoucment about starting serivce
  
          // Start a Thread Called MyThread
          isRunning = true;
          this.ClientThread = new Thread(new ClientThread());
          this.ClientThread.start();
  
          // Keep running until it explicitly stopped.
          // stopped so returns sicky
          return START_STICKY;
      }
      // code
  
  
      public void onDestroy(){
          super.onDestroy();
          // stop background Thread
          isRunning = false;
  
  
      }
  
      public class ClientThread implements Runnable {
          public void run() {
  
  
              try{
                  InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
  
                  socket = new Socket(serverAddr,SERVERPORT);
  
  
                  BufferedReader input =  new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
  
                  String st = input.readLine();
  
                  if (st.equals("vibrate")){
                      Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                      v.vibrate(3000);
                  }
                  st = null;
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
          } 
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  }
  </code></pre>
  
  <p>Android Client Manifest </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.rat"
      android:versionCode="1"
      android:versionName="1.0" &gt;
      &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="19" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.Service.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;service android:name=".Service" /&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>