0% found this document useful (0 votes)
15 views4 pages

TCP Server

C program implementation of tcp

Uploaded by

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

TCP Server

C program implementation of tcp

Uploaded by

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

TCP Server

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAX 100


#define PORT 10000
#define SA struct sockaddr

void commn(int sockfd) {


char buff[MAX];
for (;;) {
bzero(buff, MAX);
read(sockfd, buff, sizeof(buff));
printf("From client: %s", buff);

if (strncmp(buff, "exit", 4) == 0) {
printf("Client has exited.\n");
break;
}

bzero(buff, MAX);
int n =0;
printf("To client: ");
while ((buff[n++] = getchar()) != '\n') ;
write(sockfd, buff, sizeof(buff));

}
}

int main() {
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;

sockfd = socket(AF_INET, SOCK_STREAM, 0);


if (sockfd == -1) {
perror("Socket creation failed");
exit(1);
}
printf("Socket successfully created.\n");

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);

if (bind(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {


perror("Socket bind failed");
exit(1);
}
printf("Socket successfully binded.\n");

if (listen(sockfd, 5) != 0) {
perror("Listen failed");
exit(1);
}
printf("Server listening.\n");

while (1) {
len = sizeof(cli);
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
perror("Server accept failed");
continue;
}
printf("Server accepted the client.\n");

if (fork() == 0) {
close(sockfd);
commn(connfd);
close(connfd);
exit(0);
}
close(connfd);
}
close(sockfd);
return 0;
}

TCP CLIENT
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAX 100


#define PORT 10000
#define SA struct sockaddr

void commn(int sockfd) {


char buff[MAX];
for (;;) {
bzero(buff, MAX);
int n=0;
printf("To server: ");
while ((buff[n++] = getchar()) != '\n');
write(sockfd, buff, sizeof(buff));

bzero(buff, MAX);
read(sockfd, buff, sizeof(buff));
printf("From server: %s", buff);

if (strncmp(buff, "exit", 4) == 0) {
printf("Client Exit...\n");
break;
}
}
}
int main() {
int sockfd;
struct sockaddr_in servaddr;

sockfd = socket(AF_INET, SOCK_STREAM, 0);


if (sockfd == -1) {
perror("Socket creation failed");
exit(1);
}
printf("Socket successfully created.\n");

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);

if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {


perror("Connection with the server failed");
exit(1);
}
printf("Connected to the server.\n");

commn(sockfd);

close(sockfd);
return 0;
}

You might also like