Basic 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
server.c
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; }
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you give an example of how to implement message framing with delimiters in code?
What are some alternatives to using delimiters for message framing?
Why is message framing important in network communication?
Awesome!
Completion rate improved to 8.33
Basic Protocol Design
Swipe to show menu
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
server.c
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; }
Thanks for your feedback!