Skip to content

Instantly share code, notes, and snippets.

@kYroL01
Last active August 31, 2021 14:27
Show Gist options
  • Save kYroL01/94b0bd1531ae9cf3249da8b5720c498a to your computer and use it in GitHub Desktop.
Save kYroL01/94b0bd1531ae9cf3249da8b5720c498a to your computer and use it in GitHub Desktop.
TCP server
/* -*- compile-command: "gcc -Wall -pedantic -g3 server.c -o server" -*- */
/**
TCP server implementation
1. Create a TCP socket.
2. bind(), Bind the socket to server address.
3. listen(), Put the server socket in a passive mode, it waits for the client to make a connection
4. accept(), Connection is established between client and server, ready to transfer data.
5. go back to Step 3.
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#define SIZE_BUFF 1000
#define PORT 8080
// Function designed for chat between client and server.
void func_server(int sockfd)
{
char buff[SIZE_BUFF];
int nread = 0, nwrite = 0;
// infinite loop
for (;;) {
memset(buff, 0, sizeof(buff));
errno = 0;
// read the message from client and copy it in buffer
nread = read(sockfd, buff, sizeof(buff));
if(nread == -1) {
perror("read");
exit(EXIT_FAILURE);
}
// print buffer which contains the client contents
printf("\nMessage From client: %s\n", buff);
printf("Bytes read = %ld\n", strlen(buff));
memset(buff, 0, sizeof(buff));
printf("Message from client received --> sending ACK...\n");
errno = 0;
// and send that buffer to client
nwrite = write(sockfd, "ACK -- Message received", sizeof(buff));
if (nwrite == -1) {
perror("write");
exit(EXIT_FAILURE);
}
}
}
// MAIN
int main()
{
struct sockaddr_in servaddr, cli;
int sockfd, connfd;
unsigned int client_len;
errno = 0;
// Socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) {
printf("Socket creation failed...\n");
perror("Socket");
exit(EXIT_FAILURE);
} else {
printf("Socket successfully created..\n");
}
memset(&servaddr, 0, sizeof(servaddr));
// Assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
errno = 0;
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr))) != 0) {
printf("Socket bind() failed...\n");
perror("Bind");
exit(EXIT_FAILURE);
} else {
printf("Socket successfully binded...\n");
}
errno = 0;
// Now server is ready to listen and verification
if((listen(sockfd, 5)) != 0) {
printf("Server listen() failed...\n");
perror("Listen");
exit(EXIT_FAILURE);
} else {
printf("Server listening...\n");
}
client_len = sizeof(struct sockaddr_in);
errno = 0;
// Accept the data packet from client
connfd = accept(sockfd, (struct sockaddr*) &cli, (socklen_t*) &client_len);
if (connfd < 0) {
printf("Server acccept() failed...\n");
perror("Accept");
exit(0);
} else {
printf("Server acccept the client...\n");
}
// Function for SERVER
func_server(connfd);
// Close the socket
close(sockfd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment