Simple Chat Program
To build a simple chat program in C using sockets, you need two main components: a server that listens for incoming connections and a client that connects to the server. Both sides use TCP sockets to reliably send and receive messages. The server waits for a connection from a client, then both sides take turns sending and receiving text messages in a loop. Each message is transmitted as a sequence of bytes over the network, and the TCP protocol ensures that messages are delivered in the correct order without loss or duplication. You can structure your chat program so that after the connection is established, the server and client alternate: one side reads user input from the terminal and sends it, while the other receives and displays the message. This basic setup forms the foundation for more advanced chat applications.
chat_server.c
chat_client.c
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 12345 #define BUFFER_SIZE 1024 int main() { int server_fd, client_fd; struct sockaddr_in server_addr, client_addr; char buffer[BUFFER_SIZE]; socklen_t addr_len = sizeof(client_addr); server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd < 0) { perror("socket failed"); exit(EXIT_FAILURE); } server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(PORT); if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { perror("bind failed"); close(server_fd); exit(EXIT_FAILURE); } if (listen(server_fd, 1) < 0) { perror("listen failed"); close(server_fd); exit(EXIT_FAILURE); } printf("Server listening on port %d...\n", PORT); client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &addr_len); if (client_fd < 0) { perror("accept failed"); close(server_fd); exit(EXIT_FAILURE); } printf("Client connected!\n"); while (1) { // Receive message from client memset(buffer, 0, BUFFER_SIZE); int bytes_received = recv(client_fd, buffer, BUFFER_SIZE - 1, 0); if (bytes_received <= 0) { printf("Client disconnected.\n"); break; } printf("Client: %s\n", buffer); // Get message from server user printf("You: "); fflush(stdout); memset(buffer, 0, BUFFER_SIZE); if (fgets(buffer, BUFFER_SIZE, stdin) == NULL) { break; } // Remove newline buffer[strcspn(buffer, "\n")] = 0; // Send message to client if (send(client_fd, buffer, strlen(buffer), 0) < 0) { perror("send failed"); break; } } close(client_fd); close(server_fd); return 0; }
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 8.33
Simple Chat Program
Svep för att visa menyn
To build a simple chat program in C using sockets, you need two main components: a server that listens for incoming connections and a client that connects to the server. Both sides use TCP sockets to reliably send and receive messages. The server waits for a connection from a client, then both sides take turns sending and receiving text messages in a loop. Each message is transmitted as a sequence of bytes over the network, and the TCP protocol ensures that messages are delivered in the correct order without loss or duplication. You can structure your chat program so that after the connection is established, the server and client alternate: one side reads user input from the terminal and sends it, while the other receives and displays the message. This basic setup forms the foundation for more advanced chat applications.
chat_server.c
chat_client.c
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 12345 #define BUFFER_SIZE 1024 int main() { int server_fd, client_fd; struct sockaddr_in server_addr, client_addr; char buffer[BUFFER_SIZE]; socklen_t addr_len = sizeof(client_addr); server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd < 0) { perror("socket failed"); exit(EXIT_FAILURE); } server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(PORT); if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { perror("bind failed"); close(server_fd); exit(EXIT_FAILURE); } if (listen(server_fd, 1) < 0) { perror("listen failed"); close(server_fd); exit(EXIT_FAILURE); } printf("Server listening on port %d...\n", PORT); client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &addr_len); if (client_fd < 0) { perror("accept failed"); close(server_fd); exit(EXIT_FAILURE); } printf("Client connected!\n"); while (1) { // Receive message from client memset(buffer, 0, BUFFER_SIZE); int bytes_received = recv(client_fd, buffer, BUFFER_SIZE - 1, 0); if (bytes_received <= 0) { printf("Client disconnected.\n"); break; } printf("Client: %s\n", buffer); // Get message from server user printf("You: "); fflush(stdout); memset(buffer, 0, BUFFER_SIZE); if (fgets(buffer, BUFFER_SIZE, stdin) == NULL) { break; } // Remove newline buffer[strcspn(buffer, "\n")] = 0; // Send message to client if (send(client_fd, buffer, strlen(buffer), 0) < 0) { perror("send failed"); break; } } close(client_fd); close(server_fd); return 0; }
Tack för dina kommentarer!