#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/epoll.h>
#include<string.h>
#include <stdlib.h>
#define BUFFER_LENGTH 128
int main(){
int listenfd = socket(AF_INET,SOCK_STREAM,0);
if(listenfd == -1) return -1;
struct sock_addr_server ser_addr;
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ser_addr.sin_port = htons(9999);
if (-1 == bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) {
return -2;
}
listen(listenfd,10);
// 10代表同时连接的队列的最大长度
// 设置非阻塞
int flag = fcntl(listenfd,F_GETFL,0);
flag | = O_NONBLOCK;
fcntl(listenfd,F_SETFL,flag);
int connfd = accept(listenfd,(struct sockaddr*)&ser_addr,&len);
//int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
while (1) {
unsigned char buffer[BUFFER_LENGTH] = {0};
// 接收,发送
int ret = recv(clientfd, buffer, BUFFER_LENGTH, 0);
if (ret == 0) {
close(clientfd);
break;
}
printf("buffer : %s, ret: %d\n", buffer, ret);
ret = send(clientfd, buffer, ret, 0); //
}
}