TCP服务端与客户端DEMO
最近在整理SOCKET这块知识,发现好久不写,都忘了差不多了,这里给出服务端与客户端的DEMO,以便后面使用, 代码猛戳这里.
服务器
#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 < signal.h> #include < pthread.h> #include < fcntl.h> #include < sys/select.h> void sigint_handle(int sig); int socket_init(); void socket_deinit(int fd); void socket_process(int clientfd); void socket_accept(int fd); unsigned char quit = 0; int main(int argc, char* argv[]) { if (argc != 2) { printf("Usage: %s < Listen Port>\n", argv[0]); return -1; } signal(SIGINT, sigint_handle); int listenfd = socket_init(atoi(argv[1])); if (listenfd < 0) { exit(-1); } socket_accept(li stenfd); socket_deinit(listenfd); void sigint_handle(int sig) { quit = 1; } int socket_init(int port) { int fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { printf("socket create failed.\n"); goto _error; } //reuse address int opt = SO_REUSEADDR; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); //bind struct sockaddr_in addr; bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { printf("socket bind failed.\n"); goto _error; } //listen if(listen(fd, 10) < 0) { printf("socket listen failed.\n"); goto _error; } return fd; _error: socket_deinit(fd); return -1; } void socket_accept(int fd) { struct sockaddr_in client_addr; socklen_t addrlen; fd_set fds; struct timeval timeout = {3, 0}; while (!quit) { FD_ZERO(&fds); FD_SET(fd, &fds); int ret = select(fd+1, &fds, &fds, NULL, &timeout); if (ret < 0) { break; } else if (ret == 0) { continue; } else { int clientfd = accept(fd, (struct sockaddr*)&client_addr, &addrlen); if (clientfd < 0) { printf("socket accept failed.\n"); break; } printf("Accept: %s:%d\n", inet_ntoa(client_addr.sin_addr), htons(client_addr.sin_port)); socket_process(clientfd); } } } static void* thread_proc(void* arg) { int clientfd = (int)arg; if (clientfd < 0) return NULL; send(clientfd, "Welcome", 8, 0); close(clientfd); return NULL; } void socket_process(int clientfd) { pthread_t pid; int error = pthread_create(&pid, NULL, thread_proc, (void*)clientfd); if (error) { printf("pthread create failed:%d\n", error); } } void socket_deinit(int fd) { if (fd >= 0) close(fd); }
客户端
#include < stdio.h> #include < unistd.h> #include < stdlib.h> #include < sys/socket.h> #include < arpa/inet.h> #include < sys/types.h> #include < netinet/in.h> #include < fcntl.h> #include < strings.h> int socket_init(); void socket_deinit(); void socket_connect(int fd, char* ip, int port); int main(int argc, char* argv[]) { if (argc != 3) { printf("Usage: %s < Server IP> < Server Port>\n", argv[0]); return -1; } int sockfd = socket_init(); socket_connect(sockfd, argv[1], atoi(argv[2])); printf("Connected to %s\n", argv[1]); char buf[1024] = {0}; int len = recv(sockfd, buf, sizeof(buf), 0); if (len >= 0) { buf[len] = '\0'; } printf("RECV: %s\n", buf); socket_deinit(sockfd); return 0; } void socket_connect(int fd, char* ip, int port) { struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = inet_addr(ip); if (connect(fd, (struct sockaddr*)&addr, addrlen) < 0) { perror("socket connect failed"); exit(-1); } } int socket_init() { int fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror("socket create failed."); exit(-1); } return fd; } void socket_deinit(int fd) { if (fd >= 0) close(fd); }