#include <stdio.h> #include<sys/socket.h> #include <sys/types.h> #include<arpa/inet.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #define MYPORT 6666 #define QUEUE 20 #define BUFFER_SIZE 1024 int main() { int server_socket = socket(AF_INET,SOCK_STREAM,0); 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); if(bind(server_socket,(struct sockaddr*)&server_sockaddr, sizeof(server_sockaddr))==-1) { perror("listen"); exit(1); } if(listen(server_socket,QUEUE)==-1){ exit(1); } char buffer[BUFFER_SIZE]; struct sockaddr_in client_addr; socklen_t length = sizeof(client_addr); int conn = accept(server_socket,(struct sockaddr*)&client_addr,&length); if(conn<0) { perror("connert"); exit(1); } while (1) { memset(buffer,0, sizeof(buffer)); if (strcmp(buffer,"exit ")==0){ break; } fputs(buffer,stdout); send(conn,buffer,length,0); } close(conn); close(server_socket); return 0; }