服務器和客戶端都是用多進程來接收和發送數據。c++
#include <cstdio> #include <cstdlib> #include <cstring> #include <unistd.h> #include <csignal> #include <sys/wait.h> #include <arpa/inet.h> #include <sys/socket.h> const int BUF_SIZE = 30; void error_handling(const char *messgae); void read_childproc(int sig); // 接收一個參數,argv[1]爲端口號 int main(int argc, char *argcv[]) { int server_sock, client_sock; struct sockaddr_in server_addr, client_addr; pid_t pid; struct sigaction act; socklen_t addr_size; int str_len, state; char buf[BUF_SIZE]; if (argc != 2) { printf("Usgae : %s <port>\n", argcv[0]); exit(1); } act.sa_handler = read_childproc; sigemptyset(&act.sa_mask); act.sa_flags = 0; state = sigaction(SIGCHLD, &act, 0); server_sock = socket(PF_INET, SOCK_STREAM, 0); memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(atoi(argcv[1])); if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { error_handling("bind() error"); } if (listen(server_sock, 5) == -1) { error_handling("listen() error"); } while (1) { addr_size = sizeof(client_addr); client_sock =accept(server_sock, (struct sockaddr*)&server_addr, &addr_size); if (client_sock == -1) { continue; } else { puts("new client connected..."); } pid = fork(); if (pid == 0) { close(server_sock); while ((str_len = read(client_sock, buf, BUF_SIZE)) != 0) { write(client_sock, buf, str_len); } close(client_sock); puts("client disconnected..."); return 0; } else { close(client_sock); } } close(server_sock); return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> const int BUF_SIZE = 30; void error_handling(const char *messgae); void read_routine(int sock, char *buf); void write_routine(int sock, char *buf); // 接收兩個參數,argv[1]爲IP地址,argv[2]爲端口號 int main(int argc, char *argv[]) { int sock; pid_t pid; char buf[BUF_SIZE]; struct sockaddr_in server_addr; if (argc != 3) { printf("Usage : %s <IP> <port>\n", argv[0]); exit(1); } sock = socket(PF_INET, SOCK_STREAM, 0); memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(argv[1]); server_addr.sin_port = htons(atoi(argv[2])); if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { error_handling("connect() error"); } pid = fork(); if (pid == 0) { write_routine(sock, buf); } else { read_routine(sock, buf); } close(sock); return 0; }
#include <cstdio> #include <cstdlib> #include <csignal> #include <cstring> #include <unistd.h> #include <sys/wait.h> #include <sys/socket.h> const int BUF_SIZE = 30; // 處理錯誤 void error_handling(const char *message) { printf("%s", message); exit(1); } // 讀取進程退出狀態 void read_childproc(int sig) { pid_t pid; int status; pid = waitpid(-1, &status, WNOHANG); printf("removed proc id: %d\n", pid); } // 客戶端接收數據 void read_routine(int sock, char *buf) { while (1) { ssize_t str_len = read(sock, buf, BUF_SIZE); if (str_len == 0) { return; } buf[str_len] = 0; printf("Message from server: %s", buf); } } // 客戶端發送數據 void write_routine(int sock, char *buf) { while (1) { fgets(buf, BUF_SIZE, stdin); if (!strcmp(buf, "q\n") || !strcmp(buf, "Q\n")) { shutdown(sock, SHUT_WR); return; } write(sock, buf, strlen(buf)); } }
githubgit
《TCP/IP網絡編程》github