0% found this document useful (0 votes)
5 views5 pages

Chat

The document contains C code for a simple TCP server and client implementation. The server listens for client connections, receives messages, and responds until the client sends 'exit'. The client connects to the server, sends messages, and also terminates the connection by sending 'exit'.

Uploaded by

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

Chat

The document contains C code for a simple TCP server and client implementation. The server listens for client connections, receives messages, and responds until the client sends 'exit'. The client connects to the server, sends messages, and also terminates the connection by sending 'exit'.

Uploaded by

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

Server:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

int main(){

char *ip = "127.0.0.1";

int port = 5566;

int server_sock, client_sock;

struct sockaddr_in server_addr, client_addr;

socklen_t addr_size;

char buffer[1024];

int n;

server_sock = socket(AF_INET, SOCK_STREAM, 0);

if (server_sock < 0){

perror("[-]Socket error");

exit(1);

printf("[+]TCP server socket created.\n");

memset(&server_addr, '\0', sizeof(server_addr));

server_addr.sin_family = AF_INET;

server_addr.sin_port = port;

server_addr.sin_addr.s_addr = inet_addr(ip);

n = bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));


if (n < 0){

perror("[-]Bind error");

exit(1);

printf("[+]Bind to the port number: %d\n", port);

listen(server_sock, 5);

printf("Listening...\n");

while(1){

addr_size = sizeof(client_addr);

client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addr_size);

printf("[+]Client connected.\n");

// Keep communication going until the client sends 'exit'

while(1) {

bzero(buffer, 1024);

recv(client_sock, buffer, sizeof(buffer), 0);

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

if (strcmp(buffer, "exit") == 0) {

printf("Ending chat. Client requested to exit.\n");

break;

bzero(buffer, 1024);

strcpy(buffer, "HI, THIS IS SERVER. HAVE A NICE DAY!!!");

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

send(client_sock, buffer, strlen(buffer), 0);

}
close(client_sock);

printf("[+]Client disconnected.\n\n");

return 0;

Client:
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

int main(){

char *ip = "127.0.0.1";

int port = 5566;

int sock;

struct sockaddr_in addr;

socklen_t addr_size;

char buffer[1024];

int n;

sock = socket(AF_INET, SOCK_STREAM, 0);

if (sock < 0){

perror("[-]Socket error");

exit(1);

printf("[+]TCP server socket created.\n");


memset(&addr, '\0', sizeof(addr));

addr.sin_family = AF_INET;

addr.sin_port = port;

addr.sin_addr.s_addr = inet_addr(ip);

connect(sock, (struct sockaddr*)&addr, sizeof(addr));

printf("Connected to the server.\n");

// Keep sending messages until 'exit' is typed by the client

while(1) {

bzero(buffer, 1024);

printf("Client: ");

fgets(buffer, sizeof(buffer), stdin);

buffer[strcspn(buffer, "\n")] = 0; // Remove newline character

send(sock, buffer, strlen(buffer), 0);

if (strcmp(buffer, "exit") == 0) {

printf("Ending chat. You typed 'exit'.\n");

break;

bzero(buffer, 1024);

recv(sock, buffer, sizeof(buffer), 0);

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

close(sock);

printf("Disconnected from the server.\n");


return 0;

You might also like