[660] | 1 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // file : tcp_client.c |
---|
| 3 | // author : Alain Greiner |
---|
| 4 | // date : march 2020 |
---|
| 5 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 6 | // This file describes a single thread TCP client chat application. |
---|
| 7 | // The client send the first message. |
---|
| 8 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 9 | |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <stdlib.h> |
---|
| 12 | #include <string.h> |
---|
| 13 | #include <almosmkh.h> |
---|
| 14 | #include <unistd.h> |
---|
| 15 | #include <sys/socket.h> |
---|
| 16 | |
---|
| 17 | #define BUF_SIZE 256 |
---|
| 18 | |
---|
| 19 | #define LOCAL_PORT 13 |
---|
| 20 | #define LOCAL_ADDR 0xAAAAAAAA // client |
---|
| 21 | |
---|
| 22 | #define REMOTE_PORT 13 |
---|
| 23 | #define REMOTE_ADDR 0xBBBBBBBB // server |
---|
| 24 | |
---|
| 25 | //////////////////////////// |
---|
| 26 | void client_chat( int fdid ) |
---|
| 27 | { |
---|
| 28 | char buffer[BUF_SIZE]; // string buffer (for send and receive) |
---|
| 29 | int size; // string length (including NUL) |
---|
| 30 | int nbytes; // number of characters actually sent or received |
---|
| 31 | |
---|
| 32 | // chat loop |
---|
| 33 | while( 1 ) |
---|
| 34 | { |
---|
| 35 | // display client prompt |
---|
| 36 | printf("\n[from client] "); |
---|
| 37 | |
---|
| 38 | // build client message |
---|
| 39 | size = get_string( buffer , BUF_SIZE ); |
---|
| 40 | |
---|
| 41 | /* |
---|
| 42 | strcpy( buffer , "blip\n" ); |
---|
| 43 | printf("%s", buffer ); |
---|
| 44 | size = 5; |
---|
| 45 | |
---|
| 46 | if( send( fdid , buffer , size , 0 ) != size ) |
---|
| 47 | { |
---|
| 48 | printf("\n[server] chat error : cannotl sent message\n"); |
---|
| 49 | return; |
---|
| 50 | } |
---|
| 51 | */ |
---|
| 52 | |
---|
| 53 | // exit chat function when local message is the "exit" string |
---|
| 54 | if( strncmp( "exit" , buffer , 4 ) == 0 ) |
---|
| 55 | { |
---|
| 56 | return; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | // send client message |
---|
| 60 | nbytes = send( fdid , buffer , size , 0 ); |
---|
| 61 | |
---|
| 62 | if( nbytes != size ) |
---|
| 63 | { |
---|
| 64 | printf("\n[server] send error => chat return to main\n"); |
---|
| 65 | return; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | // display server prompt |
---|
| 69 | printf("\n[from server] "); |
---|
| 70 | |
---|
| 71 | // receive server message |
---|
| 72 | nbytes =recv ( fdid, buffer, BUF_SIZE, 0 ); |
---|
| 73 | |
---|
| 74 | if( nbytes < 0 ) |
---|
| 75 | { |
---|
| 76 | printf("\n\n[server] receive error => return to main\n" ); |
---|
| 77 | return; |
---|
| 78 | } |
---|
| 79 | else if( nbytes == 0 ) |
---|
| 80 | { |
---|
| 81 | printf("\n\n[server] receive EOF => return to main\n" ); |
---|
| 82 | return; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | // display server message |
---|
| 86 | printf("%s\n", buffer ); |
---|
| 87 | |
---|
| 88 | } // end chat loop |
---|
| 89 | |
---|
| 90 | } // end client_chat() |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | //////////////// |
---|
| 94 | int main( void ) |
---|
| 95 | { |
---|
| 96 | int pid; // process identifier |
---|
| 97 | int fdid; // file index of local socket |
---|
| 98 | int error; |
---|
| 99 | |
---|
| 100 | struct sockaddr_in client_sin; // local client socket |
---|
| 101 | struct sockaddr_in server_sin; // remote server socket |
---|
| 102 | |
---|
| 103 | unsigned long long start_cycle; |
---|
| 104 | |
---|
| 105 | // get start cycle |
---|
| 106 | get_cycle( &start_cycle ); |
---|
| 107 | printf("\n[client] starts at cycle %d\n", (unsigned int)start_cycle ); |
---|
| 108 | |
---|
| 109 | pid = getpid(); |
---|
| 110 | |
---|
| 111 | // 1. create TCP socket |
---|
| 112 | fdid = socket( AF_INET, |
---|
| 113 | SOCK_STREAM, |
---|
| 114 | 0 ); |
---|
| 115 | |
---|
| 116 | if( fdid < 0 ) |
---|
| 117 | { |
---|
| 118 | printf("\n[client error] cannot create socket\n"); |
---|
| 119 | exit( 0 ); |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | printf("\n[client] created socket[%x,%d]\n", pid, fdid ); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | // 2. bind local socket |
---|
| 127 | client_sin.sin_domain = HTONS( AF_INET ); |
---|
| 128 | client_sin.sin_addr = HTONL( LOCAL_ADDR ); |
---|
| 129 | client_sin.sin_port = HTONS( LOCAL_PORT ); |
---|
| 130 | |
---|
| 131 | error = bind( fdid, |
---|
| 132 | (sockaddr_t *)(&client_sin), |
---|
| 133 | sizeof(sockaddr_t) ); |
---|
| 134 | if( error ) |
---|
| 135 | { |
---|
| 136 | printf("\n[client error] bind failure on socketi[%x,%d]\n", pid, fdid ); |
---|
| 137 | exit( 0 ); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | printf("\n[client] socket[%x,%d] bound : [%x,%x]\n", |
---|
| 141 | pid, fdid, client_sin.sin_addr, (unsigned int)client_sin.sin_port ); |
---|
| 142 | |
---|
| 143 | // 3. connect to remote socket |
---|
| 144 | server_sin.sin_domain = HTONS( AF_INET ); |
---|
| 145 | server_sin.sin_addr = HTONL( REMOTE_ADDR ); |
---|
| 146 | server_sin.sin_port = HTONS( REMOTE_PORT ); |
---|
| 147 | |
---|
| 148 | error = connect( fdid, |
---|
| 149 | (sockaddr_t *)(&server_sin), |
---|
| 150 | sizeof(sockaddr_t) ); |
---|
| 151 | if( error ) |
---|
| 152 | { |
---|
| 153 | printf("\n[client error] connect failure on socket[%x,%d]\n", pid, fdid ); |
---|
| 154 | exit( 0 ); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | printf("\n[client] socket[%x,%d] connected to server [%x,%x]\n", |
---|
| 158 | pid, fdid, server_sin.sin_addr, server_sin.sin_port ); |
---|
| 159 | |
---|
| 160 | // 4. call chat function |
---|
| 161 | client_chat( fdid ); |
---|
| 162 | |
---|
| 163 | // 5. close local socket |
---|
| 164 | close( fdid ); |
---|
| 165 | |
---|
| 166 | printf("\n[client] closed socket[%x,%d]\n", pid, fdid ); |
---|
| 167 | |
---|
| 168 | exit(0); |
---|
| 169 | |
---|
| 170 | return 0; |
---|
| 171 | } |
---|