ADT
        ast-experimental
        enum
        forall-pointer-decay
        pthread-emulation
        qualifiedEnum
      
      
        
          | 
            Last change
 on this file since 91a72ef was             f0d67e5, checked in by Thierry Delisle <tdelisle@…>, 5 years ago           | 
        
        
          | 
             
Restored io examples to example folder 
 
           | 
        
        
          
            
              - 
Property                 mode
 set to                 
100644
               
             
           | 
        
        
          | 
            File size:
            2.1 KB
           | 
        
      
      
| Line |   | 
|---|
| 1 | #include <stdio.h>
 | 
|---|
| 2 | #include <stdlib.h>
 | 
|---|
| 3 | #include <string.h>
 | 
|---|
| 4 | 
 | 
|---|
| 5 | #include <netdb.h>
 | 
|---|
| 6 | #include <unistd.h>
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include <sys/types.h>
 | 
|---|
| 9 | #include <sys/socket.h>
 | 
|---|
| 10 | 
 | 
|---|
| 11 | int main(int argc, char * argv[]) {
 | 
|---|
| 12 |       if(argc != 2) {
 | 
|---|
| 13 |             printf("usage:    %s portnumber\n", argv[0]);
 | 
|---|
| 14 |             exit( EXIT_FAILURE );
 | 
|---|
| 15 |       }
 | 
|---|
| 16 |       int port = atoi(argv[1]);
 | 
|---|
| 17 |       if(port < 1) {
 | 
|---|
| 18 |             printf("Invalid port : %d (from %s)\n", port, argv[1]);
 | 
|---|
| 19 |             exit( EXIT_FAILURE );
 | 
|---|
| 20 |       }
 | 
|---|
| 21 | 
 | 
|---|
| 22 |       int sock = socket(AF_INET, SOCK_STREAM, 0);
 | 
|---|
| 23 |       if(sock < 0) {
 | 
|---|
| 24 |             perror( "socket" );
 | 
|---|
| 25 |             exit( EXIT_FAILURE );
 | 
|---|
| 26 |       }
 | 
|---|
| 27 | 
 | 
|---|
| 28 |       struct hostent * server = gethostbyname("localhost");
 | 
|---|
| 29 |       if(server == NULL) {
 | 
|---|
| 30 |             perror("localhost not found");
 | 
|---|
| 31 |             exit( EXIT_FAILURE );
 | 
|---|
| 32 |       }
 | 
|---|
| 33 | 
 | 
|---|
| 34 |       struct sockaddr_in serv_addr;
 | 
|---|
| 35 |       memset(&serv_addr, 0, sizeof(serv_addr));
 | 
|---|
| 36 |       serv_addr.sin_family = AF_INET;
 | 
|---|
| 37 |       memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
 | 
|---|
| 38 |       serv_addr.sin_port = htons(port);
 | 
|---|
| 39 | 
 | 
|---|
| 40 |       int ret = connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
 | 
|---|
| 41 |       if(ret < 0) {
 | 
|---|
| 42 |             perror( "connect" );
 | 
|---|
| 43 |             exit( EXIT_FAILURE );
 | 
|---|
| 44 |       }
 | 
|---|
| 45 | 
 | 
|---|
| 46 |       char buffer[256];
 | 
|---|
| 47 |       struct iovec iov = { buffer, 0 };
 | 
|---|
| 48 |       struct msghdr msg;
 | 
|---|
| 49 |       msg.msg_name = NULL;
 | 
|---|
| 50 |       msg.msg_namelen = 0;
 | 
|---|
| 51 |       msg.msg_control = NULL;
 | 
|---|
| 52 |       msg.msg_controllen = 0;
 | 
|---|
| 53 |       msg.msg_iov = &iov;
 | 
|---|
| 54 |       msg.msg_iovlen = 1;
 | 
|---|
| 55 |       msg.msg_flags = 0;
 | 
|---|
| 56 | 
 | 
|---|
| 57 | 
 | 
|---|
| 58 |       int rd;
 | 
|---|
| 59 |       while(0 != (rd = read(STDIN_FILENO, buffer, 256))) {
 | 
|---|
| 60 |             if(rd < 0) {
 | 
|---|
| 61 |                   perror( "read" );
 | 
|---|
| 62 |                   exit( EXIT_FAILURE );
 | 
|---|
| 63 |             }
 | 
|---|
| 64 | 
 | 
|---|
| 65 |             iov.iov_len = rd;
 | 
|---|
| 66 |             int sent = sendmsg(sock, &msg, 0);
 | 
|---|
| 67 |             if( sent < 0 ) {
 | 
|---|
| 68 |                   perror( "read" );
 | 
|---|
| 69 |                   exit( EXIT_FAILURE );
 | 
|---|
| 70 |             }
 | 
|---|
| 71 | 
 | 
|---|
| 72 |             if(sent != rd) {
 | 
|---|
| 73 |                   printf("Expected to send %d bytes, sent %d\n", rd, sent);
 | 
|---|
| 74 |                   exit( EXIT_FAILURE );
 | 
|---|
| 75 |             }
 | 
|---|
| 76 |       }
 | 
|---|
| 77 | 
 | 
|---|
| 78 |       ret = close(sock);
 | 
|---|
| 79 |       if(ret < 0) {
 | 
|---|
| 80 |             perror( "close" );
 | 
|---|
| 81 |             exit( EXIT_FAILURE );
 | 
|---|
| 82 |       }
 | 
|---|
| 83 | 
 | 
|---|
| 84 |       exit( EXIT_SUCCESS );
 | 
|---|
| 85 | }
 | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.