Created
June 27, 2018 03:02
-
-
Save fanyer/2b3e70596ba22e167b8571da0c6807d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <stdio.h> | |
| #include <netinet/in.h> | |
| #include <arpa/inet.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| #include <fcntl.h> | |
| #include <sys/shm.h> | |
| #define MYPORT 8887 | |
| #define QUEUE 20 | |
| #define BUFFER_SIZE 1024 | |
| int main() | |
| { | |
| ///定义sockfd | |
| int server_sockfd = socket(AF_INET,SOCK_STREAM, 0); | |
| ///定义sockaddr_in | |
| struct sockaddr_in server_sockaddr; | |
| server_sockaddr.sin_family = AF_INET; | |
| server_sockaddr.sin_port = htons(MYPORT); | |
| server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
| ///bind,成功返回0,出错返回-1 | |
| if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1) | |
| { | |
| perror("bind"); | |
| exit(1); | |
| } | |
| ///listen,成功返回0,出错返回-1 | |
| if(listen(server_sockfd,QUEUE) == -1) | |
| { | |
| perror("listen"); | |
| exit(1); | |
| } | |
| ///客户端套接字 | |
| char buffer[BUFFER_SIZE]; | |
| struct sockaddr_in client_addr; | |
| socklen_t length = sizeof(client_addr); | |
| ///成功返回非负描述字,出错返回-1 | |
| int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length); | |
| if(conn<0) | |
| { | |
| perror("connect"); | |
| exit(1); | |
| } | |
| while(1) | |
| { | |
| memset(buffer,0,sizeof(buffer)); | |
| int len = recv(conn, buffer, sizeof(buffer),0); | |
| if(strcmp(buffer,"exit\n")==0) | |
| break; | |
| fputs(buffer, stdout); | |
| send(conn, buffer, len, 0); | |
| } | |
| close(conn); | |
| close(server_sockfd); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment