#include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h>
int ss = -1; ss = socket(AF_INET,SOCK_STREAM,0); //獲得句柄 if(ss < 0){ fprintf(stderr,"socket create error\n"); return false; } server_fd = ss; sockaddr_in server_addr; server_addr.sin_family = AF_INET; //協議類型 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);//ip地址 server_addr.sin_port = htons(server_port);//端口號 int ret = bind(ss,(sockaddr*)&server_addr,sizeof(server_addr)); //綁定地址與端口 if(ret < 0){ fprintf(stderr,"error in bind\n"); return false; } ret = listen(ss,max_wait_num);//開始監聽 if(ret < 0){ fprintf(stderr,"error in listen\n" ); return false; }
int sc = accept(ss,(sockaddr*)&from,&len); fprintf(stderr, "get one connection:[%d]\n", sc);