├── .gitignore ├── README.md ├── alpha_bank.c ├── chat.c ├── client.c ├── getip.c └── server.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sockets-programming-in-C 2 | Awesome server and client development in C language 3 | 4 | # How to use? 5 | 1) Compile server.c (in my case, I use gcc compiler, however you might use any C compiler) 6 | 2) Execute the compiled server from terminal: ./server 7 | 3) Compile client.c 8 | 4) Execute the compiled client from terminal 9 | 10 | If above steps was successfully done, then client may be able to write to server. 11 | 12 | # Example 13 | alpha_bank.c is a server side for banking system. To use it, the following steps are required: 14 | 15 | 1) Compile and execute alpha_bank.c 16 | 2) The IP and port number should be printed on the terminal of executed alpha_bank.c: 17 | 3) Open another terminal 18 | 4) Using telnet service, connect to the IP and port number, for example, just type: 19 | 20 | telnet 127.0.0.1 8888 21 | 5) Now you are using a simple ATM, there are two bank accounts with card numbers and balances: 22 | 23 | - char* card_number1="0000 0000 0000 0000"; 24 | - unsigned int balance1 = 1000; 25 | - char* card_number2="1000 0000 0000 0000"; 26 | - unsigned int balance2 = 1500; 27 | 6) ATM asks for card number, just enjoy above numbers 28 | 7) Test other functions: BALANCE, WITHDRAW, DEPOSIT between two clients 29 | 30 | Give some stars! 31 | -------------------------------------------------------------------------------- /alpha_bank.c: -------------------------------------------------------------------------------- 1 | - #include 2 | - #include //socket 3 | - #include //inet_addr 4 | - #include //write(socket, message, strlen(message)) 5 | - #include //strlen 6 | - #include //strlen and strcmp 7 | - #include //srand 8 | - #include //for threading, link with lpthread: 9 | - 10 | - /* 11 | - gcc program.c -lpthread 12 | - */ 13 | - 14 | - //connection handler 15 | - void *connection_handler(void *); 16 | - 17 | - char* card_number1="0000 0000 0000 0000"; 18 | - unsigned int balance1 = 1000; 19 | - char* card_number2="1000 0000 0000 0000"; 20 | - unsigned int balance2 = 1500; 21 | - 22 | - //clear screen 23 | - void clrscr(){ 24 | - system("cls||clear"); 25 | - } 26 | - 27 | - int main(){ 28 | - //create a socket, returns -1 of error happened 29 | - /* 30 | - Address Family - AF_INET (this is IP version 4) 31 | - Type - SOCK_STREAM (this means connection oriented TCP protocol) 32 | - Protocol - 0 [ or IPPROTO_IP This is IP protocol] 33 | - */ 34 | - 35 | - srand(time(NULL)); //random seed 36 | - int socket_desc = socket(AF_INET, SOCK_STREAM, 0); 37 | - if (socket_desc == -1) { 38 | - printf("Could not create a socket\n"); 39 | - } 40 | - 41 | - //create a server 42 | - struct sockaddr_in server; 43 | - /* 44 | - // IPv4 AF_INET sockets: 45 | - struct sockaddr_in { 46 | - short sin_family; // e.g. AF_INET, AF_INET6 47 | - unsigned short sin_port; // e.g. htons(3490) 48 | - struct in_addr sin_addr; // see struct in_addr, below 49 | - char sin_zero[8]; // zero this if you want to 50 | - }; 51 | - 52 | - struct in_addr { 53 | - unsigned long s_addr; // load with inet_pton() 54 | - }; 55 | - 56 | - struct sockaddr { 57 | - unsigned short sa_family; // address family, AF_xxx 58 | - char sa_data[14]; // 14 bytes of protocol address 59 | - }; 60 | - */ 61 | - 62 | - char *IP = "127.0.0.1"; 63 | - int OPEN_PORT = 1000 + rand()%9000; //first, you may want to find the open port before typing dummy 8000 64 | - 65 | - server.sin_addr.s_addr = inet_addr(IP); 66 | - server.sin_family = AF_INET; 67 | - server.sin_port = htons(OPEN_PORT); //specify the open port_number 68 | - 69 | - if ( bind(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0 ) { 70 | - puts("Bind failed\n"); 71 | - return 1; 72 | - } 73 | - puts("Bind done"); 74 | - printf("IP = [%s:%d]\n", IP, OPEN_PORT); 75 | - 76 | - //listen for new connections: 77 | - listen(socket_desc, 3); 78 | - puts("Waiting for new connections..."); 79 | - 80 | - int c = sizeof(struct sockaddr_in); 81 | - //client to be connected 82 | - struct sockaddr_in client; 83 | - //new socket for client 84 | - int new_socket, *new_sock; 85 | - while ( (new_socket = accept( socket_desc, (struct sockaddr*)&client, (socklen_t*)&c )) ) { 86 | - puts("Connection accepted"); 87 | - //get the IP address of a client 88 | - char *CLIENT_IP = inet_ntoa(client.sin_addr); 89 | - int CLIENT_PORT = ntohs(client.sin_port); 90 | - printf(" CLIENT = {%s:%d}\n", CLIENT_IP, CLIENT_PORT); 91 | - //reply to the client 92 | - //char* message = "Hello, client! I have received your connection, now I will assign a handler for you!\n"; 93 | - //write(new_socket, message, strlen(message)); 94 | - 95 | - pthread_t sniffer_thread; 96 | - new_sock = malloc(1); 97 | - *new_sock = new_socket; 98 | - 99 | - if( pthread_create(&sniffer_thread, NULL, connection_handler, (void*)new_sock) < 0 ){ 100 | - perror("could not create thread"); 101 | - return 1; 102 | - } 103 | - //Now join the thread , so that we dont terminate before the thread 104 | - //pthread_join( sniffer_thread , NULL); 105 | - puts(" Handler assigned"); 106 | - } 107 | - if ( new_socket<0 ) { 108 | - perror("Accept failed"); 109 | - return 1; 110 | - } 111 | - 112 | - return 0; 113 | - } 114 | - 115 | - 116 | - 117 | - /* 118 | - _ _ _ ____ _ 119 | - / \ | |_ __ | |__ __ _| __ ) __ _ _ __ | | __ 120 | - / _ \ | | '_ \| '_ \ / _` | _ \ / _` | '_ \| |/ / 121 | - / ___ \| | |_) | | | | (_| | |_) | (_| | | | | < 122 | - /_/ \_\_| .__/|_| |_|\__,_|____/ \__,_|_| |_|_|\_\ 123 | - |_| 124 | - */ 125 | - //connection handler 126 | - void *connection_handler(void *socket_desc){ 127 | - //get the socket descriptor 128 | - int sock = *(int*)socket_desc; 129 | - int read_size; 130 | - char* message, client_message[2000]; 131 | - //send some art to the client to enjoy :-) 132 | - message = " _ _ _ ____ _\n";write(sock, message, strlen(message)); 133 | - message = " / \\ | |_ __ | |__ __ _| __ ) __ _ _ __ | | __\n";write(sock, message, strlen(message)); 134 | - message = " / _ \\ | | '_ \\| '_ \\ / _` | _ \\ / _` | '_ \\| |/ /\n";write(sock, message, strlen(message)); 135 | - message = " / ___ \\| | |_) | | | | (_| | |_) | (_| | | | | <\n";write(sock, message, strlen(message)); 136 | - message = "/_/ \\_\\_| .__/|_| |_|\\__,_|____/ \\__,_|_| |_|_|\\_\\\n";write(sock, message, strlen(message)); 137 | - message = " |_|\n";write(sock, message, strlen(message)); 138 | - message = "--------------Welcome to AlphaBank ATM--------------\n";write(sock, message, strlen(message)); 139 | - message = "\n";write(sock, message, strlen(message)); 140 | - 141 | - CONFIRM: 142 | - //todo: enter visa card and its pin 143 | - 144 | - MENU: message = "1. Balance\n2. Deposit\n3. Withdraw\n4. Transfer\nPlease, choose the transaction number: "; 145 | - write(sock , message , strlen(message)); 146 | - read_size = recv(sock, client_message, 2000, 0); 147 | - switch (client_message[0]) { 148 | - case '1':goto BALANCE;break; 149 | - case '2':goto DEPOSIT;break; 150 | - case '3':goto WITHDRAW;break; 151 | - case '4':goto TRANSFER;break; 152 | - } 153 | - 154 | - BALANCE: 155 | - message = "------------------------------------------------------\n"; 156 | - write(sock, message, strlen(message)); 157 | - message = "Please, enter your card number: "; 158 | - write(sock, message, strlen(message)); 159 | - read_size = recv(sock, client_message, 2000, 0); 160 | - 161 | - int isEqual = 1; 162 | - for (size_t i = 0; i < strlen(card_number1); i++) { 163 | - if (card_number1[i]!=client_message[i]) { 164 | - isEqual = 0; 165 | - break; 166 | - } 167 | - } 168 | - 169 | - if(isEqual==0){ 170 | - isEqual = 2; 171 | - for (size_t i = 0; i < strlen(card_number2); i++) { 172 | - if (card_number2[i]!=client_message[i]) { 173 | - isEqual = 0; 174 | - break; 175 | - } 176 | - } 177 | - } 178 | - 179 | - if ( isEqual == 0 ) { 180 | - message = "No such card number in our bank\n"; 181 | - write(sock, message, strlen(message)); 182 | - }else{ 183 | - 184 | - if (isEqual==1) { 185 | - char cated[2000]; 186 | - sprintf(cated, "Balance: %d USD\n", balance1); 187 | - write(sock, cated, strlen(cated)); 188 | - message = "------------------------------------------------------\n"; 189 | - write(sock, message, strlen(message)); 190 | - }else 191 | - if (isEqual==2) { 192 | - char cated[2000]; 193 | - sprintf(cated, "Balance: %d USD\n", balance2); 194 | - write(sock, cated, strlen(cated)); 195 | - message = "------------------------------------------------------\n"; 196 | - write(sock, message, strlen(message)); 197 | - } 198 | - 199 | - 200 | - } 201 | - 202 | - message = "Enter 1 to go back to menu, 0 to exit: "; 203 | - write(sock, message, strlen(message)); 204 | - read_size = recv(sock, client_message, 2000, 0); 205 | - if (client_message[0]=='1') { 206 | - goto MENU; 207 | - }else 208 | - goto EXIT; 209 | - DEPOSIT: 210 | - message = "------------------------------------------------------\n"; 211 | - write(sock, message, strlen(message)); 212 | - message = "Please, enter your card number: "; 213 | - write(sock, message, strlen(message)); 214 | - read_size = recv(sock, client_message, 2000, 0); 215 | - 216 | - isEqual = 1; 217 | - for (size_t i = 0; i < strlen(card_number1); i++) { 218 | - if (card_number1[i]!=client_message[i]) { 219 | - isEqual = 0; 220 | - break; 221 | - } 222 | - } 223 | - 224 | - if(isEqual==0){ 225 | - isEqual = 2; 226 | - for (size_t i = 0; i < strlen(card_number2); i++) { 227 | - if (card_number2[i]!=client_message[i]) { 228 | - isEqual = 0; 229 | - break; 230 | - } 231 | - } 232 | - } 233 | - 234 | - if ( isEqual == 0 ) { 235 | - message = "No such card number in our bank\n"; 236 | - write(sock, message, strlen(message)); 237 | - }else{ 238 | - 239 | - if (isEqual==1) { 240 | - message = "Please, enter the amount of money to deposit: "; 241 | - write(sock, message, strlen(message)); 242 | - read_size = recv(sock, client_message, 2000, 0); 243 | - int cash = atoi(client_message); 244 | - balance1 += cash; 245 | - message = "Please, insert cash to the ATM...\nDone!\n"; 246 | - write(sock, message, strlen(message)); 247 | - message = "------------------------------------------------------\n"; 248 | - write(sock, message, strlen(message)); 249 | - }else 250 | - if (isEqual==2) { 251 | - message = "Please, enter the amount of money to deposit: "; 252 | - write(sock, message, strlen(message)); 253 | - read_size = recv(sock, client_message, 2000, 0); 254 | - int cash = atoi(client_message); 255 | - balance2 += cash; 256 | - message = "Please, insert cash to the ATM...\nDone!\n"; 257 | - write(sock, message, strlen(message)); 258 | - message = "------------------------------------------------------\n"; 259 | - write(sock, message, strlen(message)); 260 | - } 261 | - } 262 | - 263 | - message = "Enter 1 to go back to menu, 0 to exit: "; 264 | - write(sock, message, strlen(message)); 265 | - read_size = recv(sock, client_message, 2000, 0); 266 | - if (client_message[0]=='1') { 267 | - goto MENU; 268 | - }else 269 | - goto EXIT; 270 | - 271 | - WITHDRAW: 272 | - message = "------------------------------------------------------\n"; 273 | - write(sock, message, strlen(message)); 274 | - message = "Please, enter your card number: "; 275 | - write(sock, message, strlen(message)); 276 | - read_size = recv(sock, client_message, 2000, 0); 277 | - 278 | - isEqual = 1; 279 | - for (size_t i = 0; i < strlen(card_number1); i++) { 280 | - if (card_number1[i]!=client_message[i]) { 281 | - isEqual = 0; 282 | - break; 283 | - } 284 | - } 285 | - 286 | - if(isEqual==0){ 287 | - isEqual = 2; 288 | - for (size_t i = 0; i < strlen(card_number2); i++) { 289 | - if (card_number2[i]!=client_message[i]) { 290 | - isEqual = 0; 291 | - break; 292 | - } 293 | - } 294 | - } 295 | - 296 | - if ( isEqual == 0 ) { 297 | - message = "No such card number in our bank\n"; 298 | - write(sock, message, strlen(message)); 299 | - }else{ 300 | - if (isEqual==1) { 301 | - message = "Please, enter the amount of money to withdraw: "; 302 | - write(sock, message, strlen(message)); 303 | - read_size = recv(sock, client_message, 2000, 0); 304 | - int cash = atoi(client_message); 305 | - if (cash 2 | #include //socket 3 | #include //inet_addr 4 | #include //write(socket, message, strlen(message)) 5 | #include //strlen 6 | #include //strlen 7 | 8 | #include //for threading, link with lpthread: 9 | /* 10 | gcc program.c -lpthread 11 | */ 12 | 13 | 14 | //connection handler 15 | void *connection_handler(void *); 16 | 17 | int main(){ 18 | //create a socket, returns -1 of error happened 19 | /* 20 | Address Family - AF_INET (this is IP version 4) 21 | Type - SOCK_STREAM (this means connection oriented TCP protocol) 22 | Protocol - 0 [ or IPPROTO_IP This is IP protocol] 23 | */ 24 | int socket_desc = socket(AF_INET, SOCK_STREAM, 0); 25 | if (socket_desc == -1) { 26 | printf("Could not create a socket\n"); 27 | } 28 | 29 | //create a server 30 | struct sockaddr_in server; 31 | /* 32 | // IPv4 AF_INET sockets: 33 | struct sockaddr_in { 34 | short sin_family; // e.g. AF_INET, AF_INET6 35 | unsigned short sin_port; // e.g. htons(3490) 36 | struct in_addr sin_addr; // see struct in_addr, below 37 | char sin_zero[8]; // zero this if you want to 38 | }; 39 | 40 | struct in_addr { 41 | unsigned long s_addr; // load with inet_pton() 42 | }; 43 | 44 | struct sockaddr { 45 | unsigned short sa_family; // address family, AF_xxx 46 | char sa_data[14]; // 14 bytes of protocol address 47 | }; 48 | */ 49 | 50 | char *IP = "127.0.0.1"; 51 | unsigned short OPEN_PORT = 8888; //first, you may want to find the open port before typing dummy 8000 52 | 53 | server.sin_addr.s_addr = inet_addr(IP); 54 | server.sin_family = AF_INET; 55 | server.sin_port = htons(OPEN_PORT); //specify the open port_number 56 | 57 | if ( bind(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0 ) { 58 | puts("Bind failed\n"); 59 | return 1; 60 | } 61 | puts("Bind done"); 62 | 63 | //listen for new connections: 64 | listen(socket_desc, 3); 65 | puts("Waiting for new connections..."); 66 | 67 | int c = sizeof(struct sockaddr_in); 68 | //client to be connected 69 | struct sockaddr_in client; 70 | //new socket for client 71 | int new_socket, *new_sock; 72 | while ( (new_socket = accept( socket_desc, (struct sockaddr*)&client, (socklen_t*)&c )) ) { 73 | puts("Connection accepted"); 74 | //get the IP address of a client 75 | char *CLIENT_IP = inet_ntoa(client.sin_addr); 76 | int CLIENT_PORT = ntohs(client.sin_port); 77 | printf(" CLIENT = {%s:%d}\n", CLIENT_IP, CLIENT_PORT); 78 | //reply to the client 79 | char* message = "Hello, client! I have received your connection, now I will assign a handler for you!\n"; 80 | write(new_socket, message, strlen(message)); 81 | 82 | pthread_t sniffer_thread; 83 | new_sock = malloc(1); 84 | *new_sock = new_socket; 85 | 86 | if( pthread_create(&sniffer_thread, NULL, connection_handler, (void*)new_sock) < 0 ){ 87 | perror("could not create thread"); 88 | return 1; 89 | } 90 | //Now join the thread , so that we dont terminate before the thread 91 | //pthread_join( sniffer_thread , NULL); 92 | puts(" Handler assigned"); 93 | } 94 | if ( new_socket<0 ) { 95 | perror("Accept failed"); 96 | return 1; 97 | } 98 | 99 | return 0; 100 | } 101 | 102 | 103 | 104 | //connection handler 105 | void *connection_handler(void *socket_desc){ 106 | //get the socket descriptor 107 | int sock = *(int*)socket_desc; 108 | int read_size; 109 | char message[2000], client_message[2000]; 110 | 111 | //Receive a message from client 112 | while( recv(sock , client_message , 2000 , 0) > 0 ){ 113 | printf("[CLIENT]: %s\n[ME]: ", client_message); 114 | gets(message); 115 | write(sock, message, strlen(message)); 116 | bzero(client_message, strlen(client_message)); 117 | bzero(message, strlen(message) ); 118 | } 119 | 120 | if(read_size == 0){ 121 | puts("Client disconnected"); 122 | fflush(stdout); 123 | } 124 | else if(read_size == -1){ 125 | perror("recv failed"); 126 | } 127 | 128 | //Free the socket pointer 129 | free(socket_desc); 130 | return 0; 131 | } 132 | -------------------------------------------------------------------------------- /client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include //socket 3 | #include //inet_addr 4 | #include //strlen 5 | #include //close(socket_desc) 6 | 7 | int main(){ 8 | 9 | //create a socket, returns -1 of error happened 10 | /* 11 | Address Family - AF_INET (this is IP version 4) 12 | Type - SOCK_STREAM (this means connection oriented TCP protocol) 13 | Protocol - 0 [ or IPPROTO_IP This is IP protocol] 14 | */ 15 | int socket_desc = socket(AF_INET, SOCK_STREAM, 0); 16 | if (socket_desc == -1) { 17 | printf("Could not create a socket\n"); 18 | } 19 | 20 | //create a server 21 | struct sockaddr_in server; 22 | /* 23 | // IPv4 AF_INET sockets: 24 | struct sockaddr_in { 25 | short sin_family; // e.g. AF_INET, AF_INET6 26 | unsigned short sin_port; // e.g. htons(3490) 27 | struct in_addr sin_addr; // see struct in_addr, below 28 | char sin_zero[8]; // zero this if you want to 29 | }; 30 | 31 | struct in_addr { 32 | unsigned long s_addr; // load with inet_pton() 33 | }; 34 | 35 | struct sockaddr { 36 | unsigned short sa_family; // address family, AF_xxx 37 | char sa_data[14]; // 14 bytes of protocol address 38 | }; 39 | */ 40 | 41 | char *IP = "127.0.0.1"; 42 | unsigned short OPEN_PORT = 8888; //first, you may want to find the open port before typing dummy 8000 43 | 44 | server.sin_addr.s_addr = inet_addr(IP); 45 | server.sin_family = AF_INET; 46 | server.sin_port = htons(OPEN_PORT); //specify the open port_number 47 | 48 | //connect to the server 49 | if (connect(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0) { 50 | puts("Connect error\n"); 51 | return 1; 52 | } 53 | puts("Connected\n"); 54 | 55 | //send some data 56 | char* message = "GET / HTTP/1.1\r\n\r\n"; 57 | if (send(socket_desc, message, strlen(message), 0) < 0) { 58 | puts("Send failed\n"); 59 | return 1; 60 | } 61 | puts("Data send\n"); 62 | 63 | //receive data from server 64 | char server_reply[2000]; 65 | while( recv(socket_desc, server_reply,2000,0) ) { 66 | puts("Reply received:\n"); 67 | puts(server_reply); 68 | } 69 | puts("Receive failed\n"); 70 | 71 | //close the socket PLEASE!!! 72 | //close(socket_desc); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /getip.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int 10 | main(int argc, char *argv[]) 11 | { 12 | struct ifaddrs *ifaddr, *ifa; 13 | int family, s; 14 | char host[NI_MAXHOST]; 15 | 16 | if (getifaddrs(&ifaddr) == -1) { 17 | perror("getifaddrs"); 18 | exit(EXIT_FAILURE); 19 | } 20 | 21 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { 22 | family = ifa->ifa_addr->sa_family; 23 | 24 | if (family == AF_INET) { 25 | s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), 26 | host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); 27 | if (s != 0) { 28 | printf("getnameinfo() failed: %s\n", gai_strerror(s)); 29 | exit(EXIT_FAILURE); 30 | } 31 | printf(": %s \t
%s\n", ifa->ifa_name, host); 32 | } 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include //socket 3 | #include //inet_addr 4 | #include //write(socket, message, strlen(message)) 5 | #include //strlen 6 | #include //strlen 7 | 8 | #include //for threading, link with lpthread: 9 | /* 10 | gcc program.c -lpthread 11 | */ 12 | 13 | 14 | //connection handler 15 | void *connection_handler(void *); 16 | 17 | int main(){ 18 | //create a socket, returns -1 of error happened 19 | /* 20 | Address Family - AF_INET (this is IP version 4) 21 | Type - SOCK_STREAM (this means connection oriented TCP protocol) 22 | Protocol - 0 [ or IPPROTO_IP This is IP protocol] 23 | */ 24 | int socket_desc = socket(AF_INET, SOCK_STREAM, 0); 25 | if (socket_desc == -1) { 26 | printf("Could not create a socket\n"); 27 | } 28 | 29 | //create a server 30 | struct sockaddr_in server; 31 | /* 32 | // IPv4 AF_INET sockets: 33 | struct sockaddr_in { 34 | short sin_family; // e.g. AF_INET, AF_INET6 35 | unsigned short sin_port; // e.g. htons(3490) 36 | struct in_addr sin_addr; // see struct in_addr, below 37 | char sin_zero[8]; // zero this if you want to 38 | }; 39 | 40 | struct in_addr { 41 | unsigned long s_addr; // load with inet_pton() 42 | }; 43 | 44 | struct sockaddr { 45 | unsigned short sa_family; // address family, AF_xxx 46 | char sa_data[14]; // 14 bytes of protocol address 47 | }; 48 | */ 49 | 50 | char *IP = "127.0.0.1"; 51 | unsigned short OPEN_PORT = 8888; //first, you may want to find the open port before typing dummy 8000 52 | 53 | server.sin_addr.s_addr = inet_addr(IP); 54 | server.sin_family = AF_INET; 55 | server.sin_port = htons(OPEN_PORT); //specify the open port_number 56 | 57 | if ( bind(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0 ) { 58 | puts("Bind failed\n"); 59 | return 1; 60 | } 61 | puts("Bind done"); 62 | 63 | //listen for new connections: 64 | listen(socket_desc, 3); 65 | puts("Waiting for new connections..."); 66 | 67 | int c = sizeof(struct sockaddr_in); 68 | //client to be connected 69 | struct sockaddr_in client; 70 | //new socket for client 71 | int new_socket, *new_sock; 72 | while ( (new_socket = accept( socket_desc, (struct sockaddr*)&client, (socklen_t*)&c )) ) { 73 | puts("Connection accepted"); 74 | //get the IP address of a client 75 | char *CLIENT_IP = inet_ntoa(client.sin_addr); 76 | int CLIENT_PORT = ntohs(client.sin_port); 77 | printf(" CLIENT = {%s:%d}\n", CLIENT_IP, CLIENT_PORT); 78 | //reply to the client 79 | char* message = "Hello, client! I have received your connection, now I will assign a handler for you!\n"; 80 | write(new_socket, message, strlen(message)); 81 | 82 | pthread_t sniffer_thread; 83 | new_sock = malloc(1); 84 | *new_sock = new_socket; 85 | 86 | if( pthread_create(&sniffer_thread, NULL, connection_handler, (void*)new_sock) < 0 ){ 87 | perror("could not create thread"); 88 | return 1; 89 | } 90 | //Now join the thread , so that we dont terminate before the thread 91 | //pthread_join( sniffer_thread , NULL); 92 | puts(" Handler assigned"); 93 | } 94 | if ( new_socket<0 ) { 95 | perror("Accept failed"); 96 | return 1; 97 | } 98 | 99 | return 0; 100 | } 101 | 102 | 103 | 104 | //connection handler 105 | void *connection_handler(void *socket_desc){ 106 | //get the socket descriptor 107 | int sock = *(int*)socket_desc; 108 | int read_size; 109 | char* message, client_message[2000]; 110 | //send some message to the client 111 | message = "Greetings, I am your connection handler\n"; 112 | write(sock, message, strlen(message)); 113 | 114 | message = "Now type something and i shall repeat what you type \n"; 115 | write(sock , message , strlen(message)); 116 | 117 | //Receive a message from client 118 | while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ){ 119 | //Send the message back to client 120 | message = "You wrote: "; 121 | //Send the message back to client 122 | write(sock , message , strlen(message)); 123 | write(sock , client_message , read_size); 124 | //bzero(client_message, read_size); 125 | } 126 | 127 | if(read_size == 0){ 128 | puts("Client disconnected"); 129 | fflush(stdout); 130 | } 131 | else if(read_size == -1){ 132 | perror("recv failed"); 133 | } 134 | 135 | //Free the socket pointer 136 | free(socket_desc); 137 | return 0; 138 | } 139 | --------------------------------------------------------------------------------