Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Basic Protocol Design | Advanced Networking Techniques
Practice
Projects
Quizzes & Challenges
Visat
Challenges
/
C Networking Basics

bookBasic Protocol Design

When you want two programs to talk over a network, you need to agree on a set of rules for how they communicate. These rules are called a protocol. Even for simple applications, you must decide how messages are structured and how each side knows when a message begins and ends. This is known as message framing. If you do not clearly define message boundaries, the receiving side might get confused, especially if multiple messages arrive together or one message is split across several packets.

A common solution is to use a delimiter, a special character or sequence of characters, that marks the end of each message. For example, you can use a newline character ('\n') so that each message is sent as a single line of text. The receiver reads until it sees the newline, then knows it has a complete message. This approach is simple and works well for many basic protocols, such as those used for chat or command-line based servers.

client.c

client.c

server.c

server.c

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define SERVER_PORT 9090 #define BUFFER_SIZE 1024 int main() { int sockfd; struct sockaddr_in serv_addr; char buffer[BUFFER_SIZE]; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket"); exit(1); } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERVER_PORT); serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("connect"); close(sockfd); exit(1); } printf("Connected to server. Type messages (type 'quit' to exit):\n"); while (1) { printf("> "); if (!fgets(buffer, BUFFER_SIZE, stdin)) break; if (strncmp(buffer, "quit", 4) == 0) break; // Ensure message ends with newline size_t len = strlen(buffer); if (buffer[len - 1] != '\n') { buffer[len] = '\n'; buffer[len + 1] = '\0'; } send(sockfd, buffer, strlen(buffer), 0); ssize_t n = recv(sockfd, buffer, BUFFER_SIZE - 1, 0); if (n <= 0) break; buffer[n] = '\0'; printf("Server replied: %s", buffer); } close(sockfd); return 0; }
question mark

What is a common technique for marking the end of a message in a simple protocol?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookBasic Protocol Design

Pyyhkäise näyttääksesi valikon

When you want two programs to talk over a network, you need to agree on a set of rules for how they communicate. These rules are called a protocol. Even for simple applications, you must decide how messages are structured and how each side knows when a message begins and ends. This is known as message framing. If you do not clearly define message boundaries, the receiving side might get confused, especially if multiple messages arrive together or one message is split across several packets.

A common solution is to use a delimiter, a special character or sequence of characters, that marks the end of each message. For example, you can use a newline character ('\n') so that each message is sent as a single line of text. The receiver reads until it sees the newline, then knows it has a complete message. This approach is simple and works well for many basic protocols, such as those used for chat or command-line based servers.

client.c

client.c

server.c

server.c

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define SERVER_PORT 9090 #define BUFFER_SIZE 1024 int main() { int sockfd; struct sockaddr_in serv_addr; char buffer[BUFFER_SIZE]; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket"); exit(1); } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERVER_PORT); serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("connect"); close(sockfd); exit(1); } printf("Connected to server. Type messages (type 'quit' to exit):\n"); while (1) { printf("> "); if (!fgets(buffer, BUFFER_SIZE, stdin)) break; if (strncmp(buffer, "quit", 4) == 0) break; // Ensure message ends with newline size_t len = strlen(buffer); if (buffer[len - 1] != '\n') { buffer[len] = '\n'; buffer[len + 1] = '\0'; } send(sockfd, buffer, strlen(buffer), 0); ssize_t n = recv(sockfd, buffer, BUFFER_SIZE - 1, 0); if (n <= 0) break; buffer[n] = '\0'; printf("Server replied: %s", buffer); } close(sockfd); return 0; }
question mark

What is a common technique for marking the end of a message in a simple protocol?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 3
some-alt