1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| /* tcpechosvr.c */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <errno.h> #include <string.h>
#define SRVIP "127.0.0.1" #define SRVPORT 10005 #define MAX_NUM 1024
int main() { int serverSock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(serverSock < 0) { printf("socket creation failed\n"); exit(-1); } printf("socket create successfully.\n");
struct sockaddr_in serverAddr; memset(&serverAddr,0,sizeof(serverAddr)); serverAddr.sin_family=AF_INET; serverAddr.sin_port = htons((u_short)SRVPORT); serverAddr.sin_addr.s_addr = inet_addr(SRVIP);
if(bind(serverSock, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr))==-1) { printf("Bind error.IP[%s], Port[%d]\n", SRVIP, serverAddr.sin_port); exit(-1); } printf("Bind successful.IP[%s], Port[%d]\n", SRVIP, serverAddr.sin_port);
/* 把sock链接进入监听哈希表listening_hash & 创建半连接队列的实例,初始化全连接队列 */ if(listen(serverSock,10)==-1) { printf("Listen error!\n"); } printf("Listening on port[%d]\n", serverAddr.sin_port);
char recvBuf[MAX_NUM]={0}; while(1) { struct sockaddr clientAddr; int size = sizeof(clientAddr);
/* 从全连接队列中取 */ int clientSock = accept(serverSock, &clientAddr, &size); printf("***SYS*** New client touched.\n"); while(1) { //一直接收客户端socket的send操作 if(recv(clientSock, recvBuf, MAX_NUM, 0) == -1) { printf("read error.\n"); } else { printf("receiv from client:%s\n",recvBuf); }
// 向客户端发送套接字 if(send(clientSock, recvBuf , sizeof(recvBuf), MSG_NOSIGNAL) == -1) { printf("Send error!\n"); } else { printf("Send to client:%s\n", recvBuf); } if( strcmp(recvBuf, "bye") == 0) { break; } memset(recvBuf, 0, sizeof(recvBuf)); } close(clientSock); } close(serverSock); return 0; }
|