日期:2014-05-16 浏览次数:20687 次
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
typedef void (*sighandler_t)(int);
void sig_pipe(int signo)
{
printf("捕获到SIGPIPE信号!\n");
}
int main()
{
struct sockaddr_in client_addr;
bzero(&client_addr,sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = htons(INADDR_ANY);
client_addr.sin_port = htons(0);
int client_socket = socket(AF_INET,SOCK_STREAM,0);
if( client_socket < 0)
{
printf("Create Socket Failed!\n");
return(1);
}
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
if(inet_aton("189.20.210.137",&server_addr.sin_addr) == 0)
{
printf("Server IP Address Error!\n");
return(1);
}
server_addr.sin_port = htons(6666);
socklen_t server_addr_length = sizeof(server_addr);
if(connect(client_socket,(struct sockaddr*)&server_addr, server_addr_length) < 0)
{
printf("Can Not Connect To 189.20.210.137!\n");
return(1);
}
char buf[1024000];
bzero(buf,sizeof(buf));
int length = 0;
if (signal(SIGPIPE, sig_pipe) == SIG_ERR)
{
printf("注册信号函数失败!\n");
return(1);
}
while (1)
{
do
{
length = recv(client_socket, buf, sizeof(buf), 0);
}
while (length < 0 && errno == EINTR);
if (length < 0)
{
if (errno == EAGAIN)
{
printf("EAGAIN\n");
}
}
else if (length == 0)
{
printf("对端主动关闭连接!\n");
//break;
continue;
}
else
printf("from ser: \n%s\n", buf);
//回显给服务器
//send(client_socket, buf, sizeof(buf), 0);
//模拟SIGPIPE信号
//sleep(2);
//send(client_socket, buf, sizeof(buf), 0);
}
close(client_socket);
return 0;
}