0% found this document useful (0 votes)
7 views1 page

client.c

Uploaded by

213044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

client.c

Uploaded by

213044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[1024];

client_socket = socket(AF_INET, SOCK_STREAM, 0);


if (client_socket < 0) {
printf("Error in socket creation.\n");
return -1;
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(12345);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr))


< 0) {
printf("Connection failed.\n");
return -1;
}

while (1) {
printf("Client: ");
fgets(buffer, sizeof(buffer), stdin);
send(client_socket, buffer, strlen(buffer), 0);

memset(buffer, 0, sizeof(buffer));
int bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received <= 0) {
printf("Connection closed by server.\n");
break;
}

printf("Server: %s\n", buffer);


}

close(client_socket);
return 0;
}

You might also like