实现点对点聊天功能。其中,signal函数实现对僵尸进程的处理。
server.c程序:
-
#include <sys/socket.h>
-
#include <stdio.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <fcntl.h>
-
#include <sys/shm.h>
-
#include<signal.h>
-
#define MYPORT 8887
-
#define QUEUE 20
-
#define BUFFER_SIZE 1024
-
void handler(int sig) //信号的处理,避免出现僵尸进程。
-
{
-
printf("recv a sig=%d\n",sig);
-
exit(0);
-
}
-
-
int main()
-
{
-
///定义sockfd
-
int server_sockfd = socket(AF_INET,SOCK_STREAM, 0);
-
-
///定义sockaddr_in
-
struct sockaddr_in server_sockaddr;
-
server_sockaddr.sin_family = AF_INET;
-
server_sockaddr.sin_port = htons(MYPORT);
-
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
-
///bind,成功返回0,出错返回-1
-
if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)
-
{
-
perror("bind");
-
exit(1);
-
}
-
-
///listen,成功返回0,出错返回-1
-
if(listen(server_sockfd,QUEUE) == -1)
-
{
-
perror("listen");
-
exit(1);
-
}
-
-
///客户端套接字
-
-
struct sockaddr_in client_addr;
-
socklen_t length = sizeof(client_addr);
-
-
///成功返回非负描述字,出错返回-1
-
int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length);
-
if(conn<0)
-
{
-
perror("connect");
-
exit(1);
-
}
-
pid_t pid; //fork实现多进程
-
pid =fork();
-
if(pid ==-1)
-
{
-
perror("fork");
-
exit(1);
-
}
-
if(pid ==0) //子进程
-
{
-
signal(SIGUSR1,handler);
-
char sendbuf[BUFFER_SIZE] ={0};
-
while (fgets(sendbuf,sizeof(sendbuf),stdin) !=NULL)
-
{
-
write(conn,sendbuf,strlen(sendbuf));
-
memset(sendbuf,0,sizeof(sendbuf));
-
}
-
printf("child cose\n");
-
exit(0);
-
}
-
else //父进程
-
{
-
char buffer[BUFFER_SIZE];
-
-
-
while(1)
-
{
-
memset(buffer,0,sizeof(buffer));
-
int len = recv(conn, buffer, sizeof(buffer),0);
-
if(len==-1) {
-
perror("fork");
-
exit(1);
-
}
-
else if(len ==0){
-
printf("client close\n");
-
break;
-
}
-
fputs(buffer, stdout);
-
-
}
-
printf("parent close\n");
-
kill(pid,SIGUSR1);
-
exit(0);
-
}
-
close(conn);
-
close(server_sockfd);
-
return 0;
-
}
client.c代码:
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <stdio.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <fcntl.h>
-
#include <sys/shm.h>
-
#include<signal.h>
-
#define MYPORT 8887
-
#define BUFFER_SIZE 1024
-
void handler(int sig) //信号处理功能
-
{
-
printf("recv a sig=%d",sig);
-
exit(0);
-
}
-
int main()
-
{
-
///定义sockfd
-
int sock_cli = socket(AF_INET,SOCK_STREAM, 0);
-
-
///定义sockaddr_in
-
struct sockaddr_in servaddr;
-
memset(&servaddr, 0, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_port = htons(MYPORT); ///服务器端口
-
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); ///服务器ip
-
-
///连接服务器,成功返回0,错误返回-1
-
if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
-
{
-
perror("connect");
-
exit(1);
-
}
-
pid_t pid;
-
pid =fork();
-
if(pid ==-1)
-
{
-
perror("fork");
-
exit(1);
-
}
-
if(pid ==0) //子进程
-
{
-
char recvbuf[BUFFER_SIZE];
-
while(1)
-
{
-
memset(recvbuf,0,sizeof(recvbuf));
-
int len =read(sock_cli,recvbuf,sizeof(recvbuf));
-
if (len ==-1){
-
perror("read");
-
exit(1);
-
}
-
else if(len ==0)
-
{
-
printf("client close\n");
-
break;
-
}
-
fputs(recvbuf,stdout);
-
}
-
close(socket);
-
kill(getppid(),SIGUSR1);
-
-
}
-
else //父进程
-
{
-
signal(SIGUSR1,handler);
-
char sendbuf[BUFFER_SIZE] ={0};
-
while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
-
{
-
send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送
-
memset(sendbuf, 0, sizeof(sendbuf));
-
-
}
-
close(socket);
-
}
-
-
-
-
return 0;
-
}
阅读(1320) | 评论(0) | 转发(0) |