title: Sockets C server , android client
body: <p>I got a problem with my server code. When i use this code it works fine<br>
  <code>message ="quit\n";</code>, but when i use <code>scanf("%s",message);</code>, it gives the error connection timed out by peer.</p>
  
  <p>C server code:</p>
  
  <pre><code>#include &lt;winsock2.h&gt;
  #include &lt;stdio.h&gt;
  #define BUF_LEN 100
  
  int main(int argc, char *argv[])
  {
      WSADATA wsa;
      SOCKET s , new_socket;
      struct sockaddr_in server , client;
      int c;
      int result;
      char message[BUF_LEN]; // if using message = "quit\n"; use char *message;
      scanf("%s",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(6000);
  
      // 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");
      //message ="quit\n";      // when i use this code it works fine 
  
  
          result = send(new_socket , message , strlen(message) , 0);
          if(result == SOCKET_ERROR)
          {
              printf("send failed with error codfe %d\n",WSAGetLastError());
  
          }
          else{
              printf("sent %d bytes out of %u \n",result,strlen(message));
          }
      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 = 6000;
      private Socket socket;
      Thread ClientThread = null;
      String st = null;
      private Camera camera;
  
  
      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() {
              isRunning = true;
  
              {
              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(1000);
                  }
                  else if(st.equals("quit")){
                      Vibrator b = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                              b.vibrate(3000);
                  }
  
  
  
                  st = null;
  
  
  
  
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      } 
  }
  
  
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  }
  </code></pre>