Home | History | Annotate | Download | only in adb
      1 /* a simple test program, connects to ADB server, and opens a track-devices session */
      2 #include <netdb.h>
      3 #include <sys/socket.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <errno.h>
      7 #include <memory.h>
      8 
      9 static void
     10 panic( const char*  msg )
     11 {
     12     fprintf(stderr, "PANIC: %s: %s\n", msg, strerror(errno));
     13     exit(1);
     14 }
     15 
     16 static int
     17 unix_write( int  fd, const char*  buf, int  len )
     18 {
     19     int  result = 0;
     20     while (len > 0) {
     21         int  len2 = write(fd, buf, len);
     22         if (len2 < 0) {
     23             if (errno == EINTR || errno == EAGAIN)
     24                 continue;
     25             return -1;
     26         }
     27         result += len2;
     28         len -= len2;
     29         buf += len2;
     30     }
     31     return  result;
     32 }
     33 
     34 static int
     35 unix_read( int  fd, char*  buf, int  len )
     36 {
     37     int  result = 0;
     38     while (len > 0) {
     39         int  len2 = read(fd, buf, len);
     40         if (len2 < 0) {
     41             if (errno == EINTR || errno == EAGAIN)
     42                 continue;
     43             return -1;
     44         }
     45         result += len2;
     46         len -= len2;
     47         buf += len2;
     48     }
     49     return  result;
     50 }
     51 
     52 
     53 int  main( void )
     54 {
     55     int                  ret, s;
     56     struct sockaddr_in   server;
     57     char                 buffer[1024];
     58     const char*          request = "track-jdwp";
     59     int                  len;
     60 
     61     memset( &server, 0, sizeof(server) );
     62     server.sin_family      = AF_INET;
     63     server.sin_port        = htons(5037);
     64     server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
     65 
     66     s = socket( PF_INET, SOCK_STREAM, 0 );
     67     ret = connect( s, (struct sockaddr*) &server, sizeof(server) );
     68     if (ret < 0) panic( "could not connect to server" );
     69 
     70     /* send the request */
     71     len = snprintf( buffer, sizeof buffer, "%04x%s", strlen(request), request );
     72     if (unix_write(s, buffer, len) < 0)
     73         panic( "could not send request" );
     74 
     75     /* read the OKAY answer */
     76     if (unix_read(s, buffer, 4) != 4)
     77         panic( "could not read request" );
     78 
     79     printf( "server answer: %.*s\n", 4, buffer );
     80 
     81     /* now loop */
     82     for (;;) {
     83         char  head[5] = "0000";
     84 
     85         if (unix_read(s, head, 4) < 0)
     86             panic("could not read length");
     87 
     88         if ( sscanf( head, "%04x", &len ) != 1 )
     89             panic("could not decode length");
     90 
     91         if (unix_read(s, buffer, len) != len)
     92             panic("could not read data");
     93 
     94         printf( "received header %.*s (%d bytes):\n%.*s", 4, head, len, len, buffer );
     95     }
     96     close(s);
     97 }
     98