|
| 1 | +#include <stdio.h> |
| 2 | +#include <unistd.h> |
| 3 | +#include <string.h> |
| 4 | +#include <sys/socket.h> |
| 5 | +#include <arpa/inet.h> |
| 6 | +#include <getopt.h> |
| 7 | +#include <stdbool.h> |
| 8 | + |
| 9 | +#define PORT 43192 |
| 10 | + |
| 11 | +char *serv_addr = NULL; |
| 12 | +bool client_flag = 0; |
| 13 | + |
| 14 | +int main(int argc, char *argv[]) |
| 15 | +{ |
| 16 | + int socket_desc, client_sock, client_size, op; |
| 17 | + struct sockaddr_in server_addr, client_addr; |
| 18 | + char server_message[256]; |
| 19 | + char client_message[256] = "Hello, server! I am the client you've been waiting for!"; |
| 20 | + |
| 21 | + while ((op = getopt(argc, argv, "hs:c")) != -1) { |
| 22 | + switch (op) { |
| 23 | + case 's': |
| 24 | + serv_addr = optarg; |
| 25 | + break; |
| 26 | + case 'c': |
| 27 | + client_flag = 1; |
| 28 | + break; |
| 29 | + default: |
| 30 | + case '?': |
| 31 | + case 'h': |
| 32 | + printf(argv[0], "A simple tcp client-server example.\n"); |
| 33 | + printf("-s <server's ip address>\n"); |
| 34 | + printf("-c => indicating this is client\n"); |
| 35 | + return 0; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (!serv_addr) { |
| 40 | + printf("Need a server address\n"); |
| 41 | + return -1; |
| 42 | + } |
| 43 | + |
| 44 | + // Clean buffers: |
| 45 | + memset(server_message, '\0', sizeof(server_message)); |
| 46 | + |
| 47 | + // Create socket: |
| 48 | + socket_desc = socket(AF_INET, SOCK_STREAM, 0); |
| 49 | + |
| 50 | + if (socket_desc < 0) { |
| 51 | + printf("Error while creating socket\n"); |
| 52 | + return -1; |
| 53 | + } |
| 54 | + printf("Socket created successfully\n"); |
| 55 | + |
| 56 | + // Set port and IP: |
| 57 | + server_addr.sin_family = AF_INET; |
| 58 | + server_addr.sin_port = htons(PORT); |
| 59 | + server_addr.sin_addr.s_addr = inet_addr(serv_addr); |
| 60 | + if (!client_flag) { |
| 61 | + // Bind to the set port and IP: |
| 62 | + if (bind(socket_desc, (struct sockaddr*)&server_addr, |
| 63 | + sizeof(server_addr)) < 0) { |
| 64 | + printf("Couldn't bind to the port\n"); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + printf("Binding complete\n"); |
| 68 | + |
| 69 | + // Listen for clients: |
| 70 | + if (listen(socket_desc, 1) < 0) { |
| 71 | + printf("Error while listening\n"); |
| 72 | + return -1; |
| 73 | + } |
| 74 | + printf("Listening for incoming connections...\n"); |
| 75 | + |
| 76 | + // Accept an incoming connection: |
| 77 | + client_size = sizeof(client_addr); |
| 78 | + client_sock = accept(socket_desc, |
| 79 | + (struct sockaddr*)&client_addr, |
| 80 | + &client_size); |
| 81 | + |
| 82 | + if (client_sock < 0) { |
| 83 | + printf("Can't accept\n"); |
| 84 | + return -1; |
| 85 | + } |
| 86 | + printf("Client connected at IP: %s and port: %i\n", |
| 87 | + inet_ntoa(client_addr.sin_addr), |
| 88 | + ntohs(client_addr.sin_port)); |
| 89 | + |
| 90 | + // Receive client's message: |
| 91 | + if (recv(client_sock, client_message, |
| 92 | + sizeof(client_message), 0) < 0) { |
| 93 | + printf("Couldn't receive\n"); |
| 94 | + return -1; |
| 95 | + } |
| 96 | + printf("Msg from client: %s\n", client_message); |
| 97 | + |
| 98 | + // Respond to client: |
| 99 | + strcpy(server_message, "Hello! This is the server."); |
| 100 | + |
| 101 | + if (send(client_sock, server_message, |
| 102 | + strlen(server_message), 0) < 0) { |
| 103 | + printf("Can't send\n"); |
| 104 | + return -1; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (client_flag) { |
| 109 | + // Send connection request to server: |
| 110 | + if (connect(socket_desc, (struct sockaddr*)&server_addr, |
| 111 | + sizeof(server_addr)) < 0) { |
| 112 | + printf("Unable to connect\n"); |
| 113 | + return -1; |
| 114 | + } |
| 115 | + printf("Connected with server successfully\n"); |
| 116 | + |
| 117 | + // Send the message to server: |
| 118 | + if (send(socket_desc, client_message, |
| 119 | + strlen(client_message), 0) < 0) { |
| 120 | + printf("Unable to send message\n"); |
| 121 | + return -1; |
| 122 | + } |
| 123 | + |
| 124 | + // Receive the server's response: |
| 125 | + if (recv(socket_desc, server_message, |
| 126 | + sizeof(server_message), 0) < 0) { |
| 127 | + printf("Error while receiving server's msg\n"); |
| 128 | + return -1; |
| 129 | + } |
| 130 | + printf("Server's response: %s\n",server_message); |
| 131 | + } |
| 132 | + |
| 133 | + close(client_sock); |
| 134 | + |
| 135 | + close(socket_desc); |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
0 commit comments