本文主要是介绍模拟捕获SIGPIPE信号 实现TCP服务器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
server端代码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
void handler (int sig) //信号处理函数
{
printf("sig = %d\n", sig);
}
int main (int argc, char *argv[])
{
int pid, bytes;
int sockfd, newfd;
socklen_t len;
struct sockaddr_in my_addr, their_addr;
char buffer[128];
if (argc != 3){
printf("Please input %s <IP-address> <Port>\n",argv[0]);
}
signal(SIGPIPE, handler); // 安装SIGPIPE 信号
if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0))){ //创建socket文件描述符
perror("Create socket failed");
exit(EXIT_FAILURE);
}
memset(&my_addr, 0, sizeof(struct sockaddr_in*));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(atoi(argv[2]));
if (0 == (inet_aton(argv[1], (struct in_addr*) &my_addr.sin_addr.s_addr))){
perror("argv[1]");
exit(errno);
}
if(-1 == (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)))){ // 绑定IP信息
perror("bind failesd");
exit(EXIT_FAILURE);
}
if (-1 == listen(sockfd, 5)){ //监听网络
perror("listen failed");
exit(EXIT_FAILURE);
}
len = sizeof(struct sockaddr_in);
while(1){
printf("Wait for new connect\n");
if(-1 == (newfd = (accept(sockfd, (struct sockaddr *) &their_addr, &len)))){
perror("Accept failed");
exit(EXIT_FAILURE);
}
else{
printf("server: got connection from %s, port %d\n",inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port));
while (1){
memset(&buffer, 0, 128);
printf("pls send message to send:");
fgets(buffer, 128, stdin);
if ( !strncasecmp(buffer, "quit", 4)){
printf("I will quit!\n");
break;
}
bytes = write(newfd, buffer, strlen(buffer));
if (bytes < 0){
printf("Send failure, errno code id %d, errno message is '%s'\n", errno, strerror(errno));
close(newfd);
break;
}
else
printf("Message :%s\t send successful, %dbyte send !\n", buffer, len);
}
}
}
close(sockfd);
return 0;
}
client端代码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
int main (int argc, char *argv[])
{
int sockfd, len;
struct sockaddr_in dest;
char buf[128]; //用于存放接收数据
if (argc != 3){ //输入数据参数小于3个
printf("Error format, it must be:\n %s IP_Address Port\n",argv[0]);
exit(errno);
}
if (-1 == (sockfd = (socket(AF_INET, SOCK_STREAM, 0)))){ // 创建socket
perror("Socket failed");
exit(errno);
}
printf("Client socket Created\n");
dest.sin_family = AF_INET;
dest.sin_port = htons(atoi(argv[2]));
if(0 == inet_aton(argv[1], (struct in_addr*) &dest.sin_addr.s_addr)){
perror(argv[1]);
exit(errno);
}
if(-1 == (connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)))){
perror("Connect failed");
exit(errno);
}
memset(&buf, '\0', 128);
while(1){
len = recv(sockfd, buf, 128, 0);
printf("Recvive data: buf = %s\n",buf);
}
close(sockfd);
return 0; }
这篇关于模拟捕获SIGPIPE信号 实现TCP服务器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!