Linux传统Fork多进程并发网络编程代码 ,只是简单实现了一个多进程并发的模型,如果需要真实应用,则需要修改process_client方法即可。这种方式并发编程负载能力不怎么样,但是有一个好处就是, 当子进程处理异常时,哪怕是指针异常导致的段错误,崩溃不会影响主进程。
服务端代码:
-
//Linux传统Fork多进程并发网络编程代码
-
/* sokcet_fork.c*/
-
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <unistd.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <signal.h>
-
#define PORT 9999
-
#define MAXSOCKFD 10
-
int process_client( int connfd );
-
void handle_sigcld(int signo);
-
int main( int argc, char *argv[] )
-
{
-
int sockfd, connfd;
-
struct sockaddr_in servaddr, cliaddr;
-
socklen_t cliaddr_len;
-
fd_set readfds;
-
char buffer[256];
-
char msg[] = "Welcome to server!";
-
if ( (sockfd=socket(AF_INET,SOCK_STREAM,0))<0 )
-
{
-
perror("socket");
-
return 1;
-
}
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_port = htons(PORT);
-
servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
-
int reuse = 1;
-
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
-
&reuse, sizeof(int)) < 0){
-
perror("setsockopt error");
-
return 1;
-
}
-
if ( bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr) ) != 0 )
-
{
-
perror("connetc");
-
return 1;
-
}
-
if ( listen(sockfd,5) != 0 )
-
{
-
perror("listen");
-
return 1;
-
}
-
//允许中断退出
-
#if 0
-
signal(SIGCLD, SIG_IGN); /* now I don't have to wait()! */
-
#else
-
signal(SIGCLD, handle_sigcld); /* 处理中断信息*/
-
#endif
-
while (1)
-
{
-
cliaddr_len = sizeof( struct sockaddr);
-
connfd = accept(sockfd, (struct sockaddr*)&cliaddr,&cliaddr_len );
-
if (connfd < 0 ){
-
perror("accept");
-
continue;
-
}
-
printf("connetc from %s,sockeid:%d\r\n", inet_ntoa(cliaddr.sin_addr), connfd );
-
//if ( connfd == 0)continue;
-
int fid = fork();
-
if ( fid < 0 )
-
break;
-
else if ( fid == 0 ){
-
printf("child process \r\n");
-
//处理客户端请求数据
-
process_client(connfd);
-
//退出子进程
-
return 0;
-
//exit(0);
-
}
-
else {
-
// 父进程继续处理连接请求
-
continue;
-
}
-
}
-
return 0;
-
}
-
int process_client( int connfd )
-
{
-
char buffer[1024];
-
bzero(buffer,sizeof(buffer));
-
if ( read(connfd,buffer,4)<=0 ){
-
return -1;
-
}
-
int len = atoi(buffer);
-
printf("packet length:%04d\n", len );
-
bzero(buffer,sizeof(buffer));
-
if ( read(connfd,buffer,len)<=0 )
-
{
-
printf("connetc closed.\r\n");
-
}
-
else
-
{
-
printf("recv from client:[%s]\r\n",buffer);
-
}
-
/*将小写转换成大写*/
-
int i = 0;
-
while ( i<len ){
-
char c = *(buffer+i);
-
if ( c>='a' && c<='z' )
-
*(buffer+i) -= ('a'-'A');
-
i++;
-
}
-
//模拟5秒等待时间
-
sleep(5);
-
write(connfd, buffer, strlen(buffer) );
-
printf("return client:[%s]\r\n",buffer);
-
return 0;
-
}
-
void handle_sigcld(int signo)
-
{
-
int pid,status;
-
pid = waitpid( -1, &status, 0);//-1表示等待任何子进程
-
//printf("child process %d exit with %d\n",pid,status);
-
}
客户端代码 a.c :
-
#include <fcntl.h>
-
#include <unistd.h>
-
#include <sys/stat.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <stdio.h>
-
#include <strings.h>
-
-
#define PORT 9999
-
#define SERVER_IP "127.0.0.1"
-
//#define SERVER_IP "192.168.1.3"
-
-
int main()
-
{
-
int s;
-
-
struct sockaddr_in addr;
-
-
char buffer[256];
-
if ( (s=socket(AF_INET,SOCK_STREAM,0)) < 0 )
-
{
-
perror("socket");
-
exit(1);
-
}
-
-
-
char server_info[1024] = {0x00};
-
sprintf( server_info, "port=%d,ip=%s", PORT, SERVER_IP);
-
printf( "%s\n", server_info );
-
-
bzero( &addr, sizeof(addr) );
-
addr.sin_family = AF_INET;
-
addr.sin_port = htons(PORT);
-
addr.sin_addr.s_addr = inet_addr(SERVER_IP);
-
-
int nret = connect(s, (const struct sockaddr *)&addr,sizeof(addr));
-
if ( nret < 0)
-
{
-
//printf("test string here\n");
-
perror("connect &*******");
-
exit(1);
-
}
-
printf("nret:%d\n", nret);
-
-
memset(buffer, 0x00, sizeof(buffer) );
-
char *psend = "test string";
-
snprintf(buffer, sizeof(buffer), "%04d%s", strlen(psend), psend );
-
if ( send(s,buffer,strlen(buffer),0) < 0 )
-
{
-
perror("send");
-
return 1;
-
}
-
-
memset(buffer,0x00, sizeof(buffer) );
-
recv( s, buffer,sizeof(buffer),0);
-
printf("recv:%s\r\n",buffer);
-
-
-
return 0;
-
}
阅读(5932) | 评论(2) | 转发(4) |