64位linux atoi,inet_ntoa(remote_addr.sin_addr) 在64位linux下core dump

int socket_server()

{

int sockfd,client_fd; /*sock_fd:监听socket;client_fd:数据传输socket */

struct sockaddr_in my_addr; /*本机地址信息*/

struct sockaddr_in remote_addr; /*客户端地址信息*/

int sin_size;

charrecv_buf[MAX_BUF_SIZE]={0};

intrecv_size=0;

charsend_buf[MAX_BUF_SIZE]={0};

intsend_size=0;

intpid;

intstatus;

struct timeval tv;

struct in_addr clientaddr ;

charstr[32]={0};

charbuf_log[MAX_BUF_SIZE]={0};

enumstat_actionaction;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

sprintf(buf_log,"socke errno=%d,desc=%s\r\n",errno,strerror(errno));

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

return 1;

}

my_addr.sin_family=AF_INET;

my_addr.sin_port=htons(SERVPORT);

my_addr.sin_addr.s_addr = INADDR_ANY;

bzero(&(my_addr.sin_zero),8);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1) {

sprintf(buf_log,"bind errno=%d,desc=%s\r\n",errno,strerror(errno));

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

return 1;

}

if (listen(sockfd, BACKLOG) == -1) {

sprintf(buf_log,"bind errno=%d,desc=%s\r\n",errno,strerror(errno));

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

return 1;

}

tv.tv_sec= SOCKET_TIMEOUT_SECOND;

tv.tv_usec=0;

while(1) {

memset(&remote_addr, 0, sizeof(struct sockaddr));

sin_size = sizeof(remote_addr);

if ((client_fd = accept(sockfd, (struct sockaddr *)(&remote_addr), &sin_size)) == -1) {

sprintf(buf_log,"accept errno=%d,desc=%s\r\n",errno,strerror(errno));

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

continue;

}

//sprintf(buf_log,"received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));

if ( NULL== (char*) (inet_ntop(AF_INET,&remote_addr.sin_addr.s_addr,str,31)) )

{

printf("inet_ntop error,errno=%d,desc=%s\r\n",errno,strerror(errno) );

}

sprintf(buf_log,"received a connection from %s\n", str);

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

if (setsockopt(client_fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1)

{

sprintf(buf_log,"warning!setsockopterrno=%d,desc=%s\r\n",errno,strerror(errno));

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

}

if ( (pid=fork()) == 0) { /*子进程代码段*/

while(1)

{

memset(recv_buf,0,MAX_BUF_SIZE);

recv_size=recv(client_fd,recv_buf,MAX_BUF_SIZE -1 ,0);

if(recv_size ==-1)

{

sprintf(buf_log,"recv errno=%d,desc=%s\r\n",errno,strerror(errno));

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

close(client_fd);

exit(1);

}

else if( 0== recv_size)

{

sprintf(buf_log,"connection reset by peer!\r\n");

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

exit(1);

}

else//if (1 == recv_size)

{

action=atoi(recv_buf);

sprintf(buf_log,"recv_buf=%s,recv_size=%d,action=%d\r\n",recv_buf,recv_size,action);

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

memset(send_buf,0,MAX_BUF_SIZE);

if (exec_command(2,action,send_buf) ==-1 )

{

continue;

}

send_buf[ strlen(send_buf)] = 0;

sprintf(buf_log,"begin send...,send_buf=%s\r\n",send_buf);

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

if (send(client_fd, send_buf,strlen(send_buf), 0) == -1) {

sprintf(buf_log,"send errno=%d,desc=%s\r\n",errno,strerror(errno));

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

close(client_fd);

exit(1);

}

}

}

}

else if(pid <0)

{

sprintf(buf_log,"socket_server fork errno=%s\n",strerror(errno));

buf_log[strlen(buf_log)]=0;

writelog(g_logfile,buf_log);

}

else

{

//parent;

close(client_fd);

waitpid(pid,&status,0);

}

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值