-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_server.cpp
More file actions
72 lines (68 loc) · 1.93 KB
/
echo_server.cpp
File metadata and controls
72 lines (68 loc) · 1.93 KB
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
/**
* ==================================================
* @file concurrent_echo_server.cpp
* @brief 回显服务器
* @author ywj
* @date 2025-06-16 下午4:17
* @version 1.0
* @copyright Copyright (c) 2025 ywj. All Rights Reserved.
* ==================================================
*/
#include<sys/socket.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<memory.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<signal.h>
void error_quit(const char*msg){
perror(msg);
exit(1);
}
int main(int argc,char**argv){
//定义server套接字和client套接字
int sockFd=-1,clientFd=-1;
//定义地址结构体
sockaddr_in clientAddr,servAddr;
//初始化地址结构体
memset(&clientAddr,0,sizeof(clientAddr));
memset(&servAddr,0,sizeof(servAddr));
servAddr.sin_addr.s_addr=htonl(INADDR_ANY);//可以接受任意ip发起的连接
servAddr.sin_family=AF_INET;//ipv4
servAddr.sin_port=htons(9595);
//创建套接字
sockFd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sockFd<0){
error_quit("create socket error!");
}
//绑定地址结构体给套接字
if(bind(sockFd,(sockaddr*)&servAddr,sizeof(servAddr))<0){
error_quit("bind error!");
}
//监听
listen(sockFd,1024);
for(;;){
//阻塞等待连接
if((clientFd=accept(sockFd,NULL,NULL))<0)//不需要关注client地址信息后面两个参数可以置空
{
close(sockFd);
error_quit("accept error!");
}
printf("client connected!\n");
//读取缓冲区
char buf[1024]={0};
int nread=read(clientFd,buf,1024);
if(nread>0){
printf("recvData:%s\n",buf);
//回显给client
write(clientFd,buf,nread);
}else{
// close(sockFd);
close(clientFd);
// error_quit("read error!");
continue;
}
}
return 0;
}