Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn First TCP Connection | Networking Foundations in C
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Networking Basics

bookFirst TCP Connection

To establish a connection between two computers using TCP in C, you need to understand the role of several key functions: socket(), bind(), listen(), accept(), and connect(). These functions work together to let a server wait for connections and a client initiate them.

You start by creating a socket with socket(). This function sets up the endpoint for communication. On the server side, after creating the socket, you use bind() to associate the socket with a specific IP address and port on the local machine. This tells the operating system where to listen for incoming connections.

Next, the server calls listen(), marking the socket as passive and ready to accept connection requests. When a client wants to connect, it also creates a socket using socket(). The client then calls connect() to attempt a connection to the server's IP address and port.

Once a connection request arrives, the server uses accept() to extract the first connection request from the queue and create a new socket for communication with the client. This process completes the handshake, and both sides can now exchange data.

server.c

server.c

client.c

client.c

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
#include <stdio.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 8080 int main() { int server_fd, new_socket; struct sockaddr_in address; char buffer[1024] = {0}; char *greeting = "Hello from server!\n"; int addrlen = sizeof(address); server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd == -1) { perror("socket failed"); return 1; } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("bind failed"); return 1; } if (listen(server_fd, 3) < 0) { perror("listen failed"); return 1; } printf("Server listening on port %d...\n", PORT); new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); if (new_socket < 0) { perror("accept failed"); return 1; } send(new_socket, greeting, strlen(greeting), 0); printf("Greeting sent to client.\n"); int valread = read(new_socket, buffer, sizeof(buffer)-1); if (valread > 0) { buffer[valread] = '\0'; printf("Received from client: %s\n", buffer); } close(new_socket); close(server_fd); return 0; }
question mark

Which function is used by a TCP server to wait for incoming client connections in C?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookFirst TCP Connection

Swipe to show menu

To establish a connection between two computers using TCP in C, you need to understand the role of several key functions: socket(), bind(), listen(), accept(), and connect(). These functions work together to let a server wait for connections and a client initiate them.

You start by creating a socket with socket(). This function sets up the endpoint for communication. On the server side, after creating the socket, you use bind() to associate the socket with a specific IP address and port on the local machine. This tells the operating system where to listen for incoming connections.

Next, the server calls listen(), marking the socket as passive and ready to accept connection requests. When a client wants to connect, it also creates a socket using socket(). The client then calls connect() to attempt a connection to the server's IP address and port.

Once a connection request arrives, the server uses accept() to extract the first connection request from the queue and create a new socket for communication with the client. This process completes the handshake, and both sides can now exchange data.

server.c

server.c

client.c

client.c

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
#include <stdio.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 8080 int main() { int server_fd, new_socket; struct sockaddr_in address; char buffer[1024] = {0}; char *greeting = "Hello from server!\n"; int addrlen = sizeof(address); server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd == -1) { perror("socket failed"); return 1; } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("bind failed"); return 1; } if (listen(server_fd, 3) < 0) { perror("listen failed"); return 1; } printf("Server listening on port %d...\n", PORT); new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); if (new_socket < 0) { perror("accept failed"); return 1; } send(new_socket, greeting, strlen(greeting), 0); printf("Greeting sent to client.\n"); int valread = read(new_socket, buffer, sizeof(buffer)-1); if (valread > 0) { buffer[valread] = '\0'; printf("Received from client: %s\n", buffer); } close(new_socket); close(server_fd); return 0; }
question mark

Which function is used by a TCP server to wait for incoming client connections in C?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4
some-alt