标签:
服务端:
1 #include <sys/socket.h> 2 #include <unistd.h> 3 #include <sys/types.h> 4 #include <stdint.h> 5 #include <assert.h> 6 #include <fcntl.h> 7 #include <string.h> 8 #include <bits/socket.h> 9 #include <arpa/inet.h> 10 #include <stdio.h> 11 #include <errno.h> 12 #include <stdlib.h> 13 14 const char* ip = "192.168.16.105"; 15 uint16_t port = 8888; 16 char buff[1024]; 17 18 void make_sock_noblocking(int sock_fd) 19 { 20 int flags = fcntl(sock_fd, F_GETFL, 0); 21 flags |= O_NONBLOCK; 22 fcntl(sock_fd, F_SETFL, flags); 23 } 24 25 int main() 26 { 27 int listen_fd = socket(PF_INET, SOCK_STREAM, 0); 28 29 struct sockaddr_in sock_addr; 30 memset(&sock_addr, 0, sizeof(sock_addr)); 31 sock_addr.sin_family = AF_INET; 32 sock_addr.sin_port = htons(port); 33 inet_pton(AF_INET, ip, &(sock_addr.sin_addr)); 34 35 int optval = 1; 36 setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, 37 (const void *)&optval , sizeof(int)); 38 39 int ret = bind(listen_fd, (sockaddr*)&sock_addr, sizeof(sock_addr)); 40 41 listen(listen_fd, 1024); 42 43 //make_sock_noblocking(listen_fd); 44 45 struct sockaddr_in client_addr; 46 memset(&sock_addr, 0, sizeof(client_addr)); 47 socklen_t len = sizeof(client_addr); 48 int conn_fd = accept(listen_fd, (sockaddr*)&client_addr, &(len)); 49 50 printf("%d--------%s\n", conn_fd, strerror(errno)); 51 52 int read_bytes = read(conn_fd, buff, sizeof(buff)); 53 54 printf("%d %s\n", read_bytes, strerror(errno)); 55 56 close(listen_fd); 57 close(conn_fd); // RST 58 59 // shutdown(conn_fd, SHUT_RDWR); // RST 60 61 // shutdown(conn_fd, SHUT_WR); // 不会产生RST 62 63 sleep(10); 64 //exit(0); 65 }
客户端:
1 #include <sys/socket.h> 2 #include <unistd.h> 3 #include <sys/types.h> 4 #include <stdint.h> 5 #include <assert.h> 6 #include <fcntl.h> 7 #include <string.h> 8 #include <bits/socket.h> 9 #include <arpa/inet.h> 10 #include <stdio.h> 11 #include <errno.h> 12 #include <signal.h> 13 14 const char* ip = "192.168.16.105"; 15 uint16_t port = 8888; 16 const char* msg = "bitch!"; 17 18 int main() 19 { 20 struct sigaction sa; 21 memset(&sa, ‘\0‘, sizeof(sa)); 22 sa.sa_handler = SIG_IGN; 23 sa.sa_flags = 0; 24 if (sigaction(SIGPIPE, &sa, NULL)) { 25 return 0; 26 } 27 28 int sock_fd = socket(PF_INET, SOCK_STREAM, 0); 29 30 struct sockaddr_in sock_addr; 31 memset(&sock_addr, 0, sizeof(sock_addr)); 32 sock_addr.sin_family = AF_INET; 33 sock_addr.sin_port = htons(port); 34 inet_pton(AF_INET, ip, &(sock_addr.sin_addr)); 35 36 int ret = connect(sock_fd, (sockaddr*)&sock_addr, sizeof(sock_addr)); 37 38 if(ret < 0) 39 { 40 printf("%s\n", strerror(errno)); 41 } 42 // 43 // struct linger my_linger; 44 // memset(&my_linger, 0, sizeof(my_linger)); 45 // my_linger.l_onoff = 1; 46 // my_linger.l_linger = 0; 47 // 48 // setsockopt(sock_fd, SOL_SOCKET, SO_LINGER, &my_linger, sizeof(my_linger)); 49 50 int write_bytes = write(sock_fd, msg, strlen(msg)); 51 52 if (write_bytes > 0) 53 { 54 printf("%d----%s\n", write_bytes, strerror(errno)); 55 } 56 57 sleep(3); 58 59 write_bytes = write(sock_fd, msg, strlen(msg)); 60 61 if (write_bytes > 0) 62 { 63 printf("%d----%s\n", write_bytes, strerror(errno)); 64 } 65 sleep(3); 66 67 write_bytes = write(sock_fd, msg, strlen(msg)); 68 69 printf("%d----%s\n", write_bytes, strerror(errno)); 70 71 close(sock_fd); 72 73 return 0; 74 }
标签:
原文地址:http://www.cnblogs.com/lijiatu/p/5767268.html