我在SCOUNIX上用C写了一段SOCKET程序,在SCOUNIX上运行正常,但是在SOLARIS上则出错,请问高手,这是为什么?

whatname 2004-09-13 01:14:36
我在SCOUNIX上用C写了一段SOCKET程序,在SCOUNIX上运行正常,但是在SOLARIS上则出错,请问高手,这是为什么?
...全文
176 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yho 2004-12-30
  • 打赏
  • 举报
回复
printf("connect from %s %d\n",inet_ntoa(cliaddr.sin_addr.s_addr),ntohs(cliaddr.sin_port));

This statement has bug on inet_ntoa(). If you change cliaddr.sin_addr.s_addr to cliaddr.sin_addr, you will get what you want.
neo954 2004-09-14
  • 打赏
  • 举报
回复
1 你还有什么要补充的信息么?
2 真糟糕,希望你能搞定……
starstargao 2004-09-14
  • 打赏
  • 举报
回复
可能会有点不一样,因为在不同的版本的unix下, 操作系统实现TCP/IP和实现可能有一点小差别,所以你还是要重新检查一下你的程序的和编译一下
whatname 2004-09-14
  • 打赏
  • 举报
回复
程序内容如下:
SOCKET SERVER

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <limits.h>

#define SERVER_PORT 9999
main(int argc, char *argv[])
{
int listenfd,clifd;
long pid;
struct sockaddr_in myaddr,cliaddr;
int ret;
int len;
char buf[60];
char tmpstr[10];
int i;
memset(buf, '\0', sizeof(buf));
memset(tmpstr, '\0', sizeof(tmpstr));
listenfd=socket(AF_INET,SOCK_STREAM,0);
if (listenfd<0)
{
perror("socket error");
exit(-1);
}
myaddr.sin_family=AF_INET;
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
myaddr.sin_port=htons(atoi(argv[1]));
ret=bind(listenfd,(struct sockaddr *)&myaddr,sizeof(myaddr));
if (ret<0)
{
perror("bind error");
exit(-1);
}
listen(listenfd,100);
len=sizeof(struct sockaddr);
while(1)
{
clifd=accept(listenfd,(struct sockaddr*)&cliaddr,&len);
/*注意accept的第三个参数也是地址*/
if(clifd==-1)
{
/* perror("accept error"); */
continue;
}
printf("connect from %s %d\n",inet_ntoa(cliaddr.sin_addr.s_addr),ntohs(cliaddr
.sin_port));
switch(pid=fork())
{
case 0: /*子进程*/
close(listenfd);
/*子进程进行其他的操作*/
/* send(clifd, "cmd:connected", 26, 0); */
if (recv(clifd, buf, 60, 0) !=-1)
{
buf[9]='\0';
strcpy(tmpstr,"111111");
for (i=0;i<6;i++) tmpstr[i]=buf[i];
if (strcmp("statup",tmpstr)==0)
{
system("sh /usr/servercheck/statup.sh ");

printf("ok");
};

};
close(clifd);
exit(0);
break;
case -1:
/*子进程错误*/
perror("fork error");
break;
default:/*父进程*/
close(clifd);
/*printf("parent\n");*/
break;
}
}
}


每当有连上来的,
这边就报
Segmentation Fault - core dumped

进程详细描述如下:

Base time stamp: 1095153219.1467 [ Tue Sep 14 17:13:39 CST 2004 ]
14173: accept(3, 0xFFBEFD98, 0xFFBEFD90, 1) (sleeping...)
14173: 3.3059 accept(3, 0xFFBEFD98, 0xFFBEFD90, 1) = 4
14173: 3.3069 Incurred fault #6, FLTBOUNDS %pc = 0xFF1CB0B8
14173: siginfo: SIGSEGV SEGV_MAPERR addr=0x87FC29FA
14173: 3.3072 Received signal #11, SIGSEGV [default]
14173: siginfo: SIGSEGV SEGV_MAPERR addr=0x87FC29FA
14173: *** process killed ***

这段代码在SCOUNIX上运行正常,但是在SOLARIS上面就出错了!!!

请大家帮忙! 谢谢!



NC 2004-09-13
  • 打赏
  • 举报
回复
把出错信息贴出来,或者你重新编译一下程序再试试
billdavid 2004-09-13
  • 打赏
  • 举报
回复
Run-time error or compile error? Have you recompile your program?

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧