linux tcp通讯demo代码。通过用chatgpt生成的代码。做一个代码记录。
一、基本的通讯demo
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Binding the socket to the port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Listening for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// Accept an incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Receive the client's message
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
// Send a response to the client
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
client.c
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[]) {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[1024] = {0};
// Creating socket file descriptor
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
// Connect to the server
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
// Send a message to the server
send(sock, hello, strlen(hello), 0);
printf("Hello message sent\n");
// Receive a response from the server
valread = read(sock, buffer, 1024);
printf("%s\n", buffer);
return 0;
}
client 代码能重复用
二、server的thread方式实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#define PORT 8080
void *handle_client(void *arg) {
int new_socket = (int)arg;
char buffer[1024] = {0};
char *hello = "Hello from server";
// Receive the client's message
int valread = read(new_socket, buffer, 1024);
printf("Client message: %s\n", buffer);
// Send a response to the client
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
close(new_socket);
pthread_exit(NULL);
}
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Binding the socket to the port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Listening for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
while (1) {
// Accept an incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
pthread_t tid;
if (pthread_create(&tid, NULL, handle_client, (void *)new_socket) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
return 0;
}
三、server 的process方式实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h>
#define PORT 8080
void handle_client(int new_socket) {
char buffer[1024] = {0};
char *hello = "Hello from server";
// Receive the client's message
int valread = read(new_socket, buffer, 1024);
printf("Client message: %s\n", buffer);
// Send a response to the client
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
close(new_socket);
exit(EXIT_SUCCESS);
}
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Binding the socket to the port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Listening for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
while (1) {
// Accept an incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == 0) {
// Child process
handle_client(new_socket);
} else if (pid > 0) {
// Parent process
close(new_socket);
while (waitpid(-1, NULL, WNOHANG) > 0);
} else {
perror("fork");
exit(EXIT_FAILURE);
}
}
return 0;
}
四、server的select方式实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/select.h>
#define PORT 8080
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket, client_sockets[MAX_CLIENTS], max_sd, sd, activity, i, valread;
fd_set rd_set;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char *hello = "Hello from server";
// Initialize all client sockets to 0
for (i = 0; i < MAX_CLIENTS; i++) {
client_sockets[i] = 0;
}
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Binding the socket to the port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Listening for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
while (1) {
// Clear the socket set
FD_ZERO(&(rd_set));
// Add the server socket to the set
FD_SET(server_fd, &(rd_set));
max_sd = server_fd;
// Add child sockets to the set
for (i = 0; i < MAX_CLIENTS; i++) {
sd = client_sockets[i];
if (sd > 0) {
FD_SET(sd, &(rd_set));
}
if (sd > max_sd) {
max_sd = sd;
}
}
// Wait for an activity on the sockets
activity = select(max_sd + 1, &(rd_set), NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) {
printf("select error");
}
// If something happened on the server socket, accept new connection
if (FD_ISSET(server_fd, &(rd_set))) {
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Add the new socket to the array of sockets
for (i = 0; i < MAX_CLIENTS; i++) {
if (client_sockets[i] == 0) {
client_sockets[i] = new_socket;
printf("New connection, socket fd is %d, ip is : %s, port : %d\n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
break;
}
}
}
// Else, it's some IO operation on some other socket
for (i = 0; i < MAX_CLIENTS; i++) {
sd = client_sockets[i];
if (FD_ISSET(sd, &(rd_set))) {
// Check if it was for closing, and also read the incoming message
if ((valread = read(sd, buffer, BUFFER_SIZE)) == 0) {
// Somebody disconnected, get his details and print
getpeername(sd, (struct sockaddr *)&address, (socklen_t *)&addrlen);
printf("Host disconnected, ip %s, port %d\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
// Close the socket and mark as 0 in list for reuse
close(sd);
client_sockets[i] = 0;
} else {
// Echo the message back to the client
buffer[valread] = '\0';
printf("Received message: %s\n", buffer);
send(sd, hello, strlen(hello), 0);
printf("Hello message sent\n");
}
}
}
}
return 0;
}
五、server的poll 方式
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/poll.h>
#define PORT 8080
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket, client_sockets[MAX_CLIENTS], i, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char *hello = "Hello from server";
struct pollfd pollfds[MAX_CLIENTS + 1];
int nfds = 1;
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Binding the socket to the port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Listening for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// Add the server socket to the pollfd array
pollfds[0].fd = server_fd;
pollfds[0].events = POLLIN;
printf("Server listening on port %d\n", PORT);
while (1) {
// Wait for an activity on the sockets
int ready = poll(pollfds, nfds, -1);
if (ready == -1) {
perror("poll");
exit(EXIT_FAILURE);
}
// If something happened on the server socket, accept new connection
if (pollfds[0].revents & POLLIN) {
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Add the new socket to the pollfd array
for (i = 1; i < MAX_CLIENTS + 1; i++) {
if (pollfds[i].fd == 0) {
pollfds[i].fd = new_socket;
pollfds[i].events = POLLIN;
nfds++;
printf("New connection, socket fd is %d, ip is : %s, port : %d\n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
break;
}
}
}
// Else, it's some IO operation on some other socket
for (i = 1; i < nfds; i++) {
if (pollfds[i].revents & POLLIN) {
// Check if it was for closing, and also read the incoming message
if ((valread = read(pollfds[i].fd, buffer, BUFFER_SIZE)) == 0) {
// Somebody disconnected, get his details and print
getpeername(pollfds[i].fd, (struct sockaddr *)&address, (socklen_t *)&addrlen);
printf("Host disconnected, ip %s, port %d\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
// Close the socket and remove it from the pollfd array
close(pollfds[i].fd);
pollfds[i].fd = 0;
nfds--;
} else {
// Echo the message back to the client
buffer[valread] = '\0';
printf("Received message: %s\n", buffer);
send(pollfds[i].fd, hello, strlen(hello), 0);
printf("Hello message sent\n");
}
}
}
}
return 0;
}