多进程服务器
#include
#define PORT 9999
#define IP "192.168.125.113"
void handler(int signo)
{
if(signo == SIGCHLD)
{
while(waitpid(-1, NULL, WNOHANG) > 0 );
}
}
int deal_cli_msg(int newfd, struct sockaddr_in cin)
{
char rbuf[128] = "";
while(1)
{
bzero(rbuf, sizeof(rbuf));
int res = recv(newfd, rbuf, sizeof(rbuf), 0);
if(res == 0)
{
printf("客户端下线\n");
break;
}
printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), rbuf);
strcat(rbuf, "*_*");
send(newfd, rbuf, sizeof(rbuf), 0);
}
close(newfd);
return 0;
}
int main(int argc, const char *argv[])
{
if(signal(SIGCHLD, handler) == SIG_ERR)
{
perror("signal error");
return -1;
}
int sfd = -1;
if((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error");
return -1;
}
printf("sfd = %d\n", sfd);
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success _%d_%s_%s\n", __LINE__, __FILE__, __func__);
if(listen(sfd, 128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success _%d_%s_%s\n", __LINE__, __FILE__, __func__);
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
int newfd = -1;
pid_t pid = -1;
while(1)
{
if( (newfd=accept(sfd, (struct sockaddr*)&cin, &socklen)) == -1)
{
perror("accept error");
return -1;
}
printf("[%s:%d] 连接成功, newfd = %d\n", inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port), newfd);
pid = fork();
if(pid > 0)
{
close(newfd);
}else if(pid == 0)
{
close(sfd);
deal_cli_msg(newfd, cin);
exit(EXIT_SUCCESS);
}else
{
perror("fork error");
return -1;
}
}
close(sfd);
return 0;
}
多线程服务器
#include
#define PORT 9999
#define IP "192.168.125.113"
struct pthread_ds
{
int newfd;
struct sockaddr_in cin;
};
void *deal_cli_msg(void *arg)
{
int newfd = ((struct pthread_ds*)arg)->newfd;
struct sockaddr_in cin = ((struct pthread_ds*)arg)->cin;
char rbuf[128] = "";
while(1)
{
bzero(rbuf, sizeof(rbuf));
int res = recv(newfd, rbuf, sizeof(rbuf), 0);
if(res == 0)
{
printf("客户端下线\n");
break;
}
printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), rbuf);
strcat(rbuf, "*_*");
send(newfd, rbuf, sizeof(rbuf), 0);
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int sfd = -1;
if((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error");
return -1;
}
printf("sfd = %d\n", sfd);
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success _%d_%s_%s\n", __LINE__, __FILE__, __func__);
if(listen(sfd, 128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success _%d_%s_%s\n", __LINE__, __FILE__, __func__);
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
int newfd = -1;
pthread_t tid = -1;
while(1)
{
if( (newfd=accept(sfd, (struct sockaddr*)&cin, &socklen)) == -1)
{
perror("accept error");
return -1;
}
printf("[%s:%d] 连接成功, newfd = %d\n", inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port), newfd);
struct pthread_ds info = {newfd, cin};
if(pthread_create(&tid, NULL, deal_cli_msg, &info) != 0)
{
printf("tid create error\n");
return -1;
}
pthread_detach(tid);
}
close(sfd);
return 0;
}