1)
服务器端:
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define DATA_LEN 40 //data number
int main(int argc, char **argv)
{
int server_sockfd = -1;
int client_sockfd = -1;
socklen_t client_len = 0;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
char time_stamp[DATA_LEN] = {0};
static unsigned int idx = 1;
// 创建流套接字
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
// 设置服务器接收的连接地址和监听的端口
server_addr.sin_family = AF_INET; // 指定网络套接字
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // 接受所有IP地址的连接
server_addr.sin_port = htons(9521); // 绑定到 9736 端口
// 绑定(命名)套接字
bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
// 创建套接字队列,监听套接字
listen(server_sockfd, 5);
// 忽略子进程停止或退出信号
signal(SIGCHLD, SIG_IGN);
while (1)
{
//char ch = '\0';
client_len = sizeof(client_addr);
//printf("Server p...\n");
// 接收连接,创建新的套接字
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_addr, &client_len);
//printf("server csid[%x]...\n", client_sockfd);
idx++;
if (fork() == 0)
{
// 子进程中,读取客户端发过来的信息,处理信息,再发送给客户端
//printf("C p...\n");
read(client_sockfd, time_stamp, 32);
//sleep(3);
sprintf(&time_stamp[28],"-%04d ", idx);
write(client_sockfd, time_stamp, 32);
close(client_sockfd);
printf("server: %s \n", time_stamp);
exit(0);
}
else
{
// 父进程中,关闭套接字
//printf("server Close...csid[%x]\n", client_sockfd);
close(client_sockfd);
}
}
}
2)
客户端:
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/wait.h>
#define LOOP_NUM 10 //create process number
#define SEND_DATA_LEN 40 //data number
int GetTime(char * psTime) {
//char time_stamp[32] = {0}; // 2015-02-07 20:36:56 00000000xxxx
time_t nSeconds;
struct tm * pTM;
clock_t stamp = 0;
time(&nSeconds);
pTM = localtime(&nSeconds);
stamp = clock();
/* 系统时间,格式: HHMMSS */
sprintf(psTime, "%04d-%02d-%02d %02d:%02d:%02d %08ld",
pTM->tm_year+1900,pTM->tm_mon,pTM->tm_mday,pTM->tm_hour, pTM->tm_min, pTM->tm_sec, stamp);
return 0;
}
int mysendmsg(char* dat, unsigned int len)
{
int sockfd = -1;
struct sockaddr_in address;
int result;
char time_stamp[SEND_DATA_LEN] = {0}; // 2015-02-07 20:36:56 00000000xxxx
// 创建流套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// 设置要连接的服务器的信息
address.sin_family = AF_INET; // 使用网络套接字
address.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器地址
address.sin_port = htons(9521); // 服务器所监听的端口
len = sizeof(address);
// 连接到服务器
result = connect(sockfd, (struct sockaddr *)&address, (socklen_t)len);
if (result == -1)
{
perror("ops: client\n");
exit(1);
}
memset(time_stamp,0,SEND_DATA_LEN);
GetTime(time_stamp);
// 发送请求给服务器
write(sockfd, time_stamp, SEND_DATA_LEN);
printf("client: %s \n", &time_stamp[0]);
// 从服务器获取数据
memset(time_stamp, 0, SEND_DATA_LEN);
read(sockfd, time_stamp, SEND_DATA_LEN);
close(sockfd);
return 0;
}
int main_loop(void)
{
int idx=0;
pid_t childpid=0;
//char cmd[16]={0,};
//printf("LOOP start:--> \n");
while (1)
{
if(idx>=LOOP_NUM)
{
break;
}
idx++;
//printf(" create pid=%d ppid=%d idx=%d \n", getpid(), getppid(), idx);
childpid = fork();
if (childpid == 0)
{
// 子进程中
//printf(" C runing 1 pid=%d ppid=%d idx=%d \n", getpid(), getppid(), idx);
mysendmsg(NULL, 0);
//printf(" C runing 2 pid=%d ppid=%d idx=%d \n", getpid(), getppid(), idx);
//
exit(0);
break;
}
else if (childpid > 0)
{
// 父进程中
}
else
{
//错误
printf("fork error pid=%d idx=%d \n", getpid(), idx);
wait(NULL);
}
}
//父进程中回收资源
if(childpid > 0)
{
//childpid is child process pid in the parent process
//printf("FIRST PROCESS:parent create process finished! waiting for children pid=%d childpid=%d \n", getpid(), childpid);
//sprintf(cmd,"ps -ef | grep %d",getpid());
//system(cmd);
//printf("\n\n %s \n\n", cmd);
wait(NULL);
//printf("FIRST PROCESS: all exit & wait over pid=%d \n", getpid());
}
else
{
//printf(" <--pid=%d ppid=%d childpid=%d idx=%d will exit \n", getpid(), getppid(), childpid, idx);
}
//exit(0);
return 0;
}
int main(int argc, char **argv)
{
main_loop();
return 0;
}
3)