网络编程:单客户端单进程统一accpet TCP并发服务器(简单时间服务器)
写在前面的话
由主进程统一的处理客户端连接,当客户端连接到来时才fork()子进程来处理客户端请求。参考教材:Linux网络编程。
题目(功能)
- 服务端:接收客户端时间请求,返回本地时间
- 客户端:发送时间请求,接收客户端返回的时间并打印
服务端代码
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#define BUFFLEN 1024
#define PORT 45678
#define BACKLOG 5
static void handle_request(int s_c)
{
char buff[BUFFLEN];
while (1)
{
memset(buff, 0, BUFFLEN);
int n = recv(s_c, buff, BUFFLEN, 0);
if (n > 0 && !strncmp(buff, "TIME", 4))
{
memset(buff, 0, BUFFLEN);
time_t now = time(NULL);
sprintf(buff, "%24s\r\n", ctime(&now));
send(s_c, buff, strlen(buff), 0);
}
close(s_c);
}
}
static int handle_connect(int s_s)
{
struct sockaddr_in from;
socklen_t len = sizeof(from);
int s_c = accept(s_s, (struct sockaddr *)&from, &len);
if (s_c > 0)
{
if (fork() > 0)
{
close(s_c);
}
else
{
handle_request(s_c);
return 0;
}
}
}
int main(int argc, char *argv[])
{
struct sockaddr_in local;
int s_s = socket(AF_INET, SOCK_STREAM, 0);
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons(PORT);
bind(s_s, (struct sockaddr *)&local, sizeof(local));
listen(s_s, BACKLOG);
handle_connect(s_s);
close(s_s);
return 0;
}
客户端代码
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#define BUFFLEN 1024
#define SERVER_PORT 45677
int main(int argc, char *argv[])
{
int s;
struct sockaddr_in server;
char buff[BUFFLEN];
int n = 0;
s = socket(AF_INET, SOCK_STREAM, 0);
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(SERVER_PORT);
connect(s, (struct sockaddr *)&server, sizeof(server));
memset(buff, 0, BUFFLEN);
strcpy(buff, "TIME");
send(s, buff, strlen(buff), 0);
memset(buff, 0, BUFFLEN);
n = recv(s, buff, BUFFLEN, 0);
if (n > 0)
{
printf("TIME:%s", buff);
}
close(s);
return 0;
}
|