forked from OsCinux/TCP_Basic_Socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_process.c
35 lines (32 loc) · 1.02 KB
/
tcp_process.c
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
#include <stdio.h>
#include <string.h>
/*客户端的处理过程*/
void process_conn_client(int s)
{
ssize_t size = 0;
char buffer[1024]; /*数据的缓冲区*/
for(;;){ /*循环处理过程*/
/*从标准输入中读取数据放到缓冲区buffer中*/
size = read(0, buffer, 1024);
if(size > 0){ /*读到数据*/
write(s, buffer, size); /*发送给服务器*/
size = read(s, buffer, 1024); /*从服务器读取数据*/
write(1, buffer, size); /*写到标准输出*/
}
}
}
/*服务器对客户端的处理*/
void process_conn_server(int s)
{
ssize_t size = 0;
char buffer[1024]; /*数据的缓冲区*/
for(;;){ /*循环处理过程*/
size = read(s, buffer, 1024); /*从套接字中读取数据放到 缓冲区buffer中*/
if(size == 0){ /*没有数据*/
return;
}
/*构建响应字符,为接收到客户端字节的数量*/
sprintf(buffer, "%d bytes altogether\n", size);
write(s, buffer, strlen(buffer)+1);/*发给客户端*/
}
}