当前位置: 技术问答>linux和unix
请各位linux高手帮忙, Segmentation fault 错误
来源: 互联网 发布时间:2016-06-18
本文导语: #define MSG_FILE "/tmp/dbg.msg" #define BUFFER 512 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct msgtype { long mtype; char buffer...
#define MSG_FILE "/tmp/dbg.msg"
#define BUFFER 512
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct msgtype {
long mtype;
char buffer[BUFFER];
};
int main(int argc, char *argv[])
{
struct msgtype msg;
key_t key;
int msgid,ln,size;
struct sockaddr_in sa;
unsigned long sd;
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd==0xffffffff){
//printf("create socket error %dn",errno);
exit(1);
}
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(0);
if(bind(sd, (struct sockaddr*)&sa,sizeof(struct sockaddr))){
//printf("bind socket error: %d n",errno);
exit(1);
}
#if 0
if(argc>1)
sa.sin_addr.s_addr = inet_addr(argv[1]);
else
#endif
sa.sin_addr.s_addr = htonl(0xc0a80208);
sa.sin_port = htons(58000);
sa.sin_family = AF_INET;
if((key=ftok(MSG_FILE,'a'))==-1)
{
//printf("Creat Key Error:%dn",errno);
exit(1);
}
if((msgid=msgget(key,0666|IPC_CREAT|IPC_EXCL))==-1)
{
//printf("Creat Message Error:%dn",errno);
exit(1);
}
while(1)
{
ln = msgrcv(msgid,&msg,511,1,0);
size = sizeof(struct sockaddr);
sendto(sd,(const char*)msg.buffer, ln,0,(const struct sockaddr*)&sa,size);
}
exit(0);
}
找不到啥原因。请高手指正,万分感激
#define BUFFER 512
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct msgtype {
long mtype;
char buffer[BUFFER];
};
int main(int argc, char *argv[])
{
struct msgtype msg;
key_t key;
int msgid,ln,size;
struct sockaddr_in sa;
unsigned long sd;
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd==0xffffffff){
//printf("create socket error %dn",errno);
exit(1);
}
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(0);
if(bind(sd, (struct sockaddr*)&sa,sizeof(struct sockaddr))){
//printf("bind socket error: %d n",errno);
exit(1);
}
#if 0
if(argc>1)
sa.sin_addr.s_addr = inet_addr(argv[1]);
else
#endif
sa.sin_addr.s_addr = htonl(0xc0a80208);
sa.sin_port = htons(58000);
sa.sin_family = AF_INET;
if((key=ftok(MSG_FILE,'a'))==-1)
{
//printf("Creat Key Error:%dn",errno);
exit(1);
}
if((msgid=msgget(key,0666|IPC_CREAT|IPC_EXCL))==-1)
{
//printf("Creat Message Error:%dn",errno);
exit(1);
}
while(1)
{
ln = msgrcv(msgid,&msg,511,1,0);
size = sizeof(struct sockaddr);
sendto(sd,(const char*)msg.buffer, ln,0,(const struct sockaddr*)&sa,size);
}
exit(0);
}
找不到啥原因。请高手指正,万分感激
|
这种问题通常是你的编译工具链问题,或者是链接的libc不对。不要在你的代码上找了,不信你自己写个hello,world都会死掉。
|
是库 的 区别吧?
|
Segmentation fault 基本上就是指针越界访问这个错了。
仔仔细细的找了一遍,没发现错误。
难道我眼花了?
sa.sin_addr.s_addr = htonl(0xc0a80208);
这步感觉不对,楼主再check一下?
仔仔细细的找了一遍,没发现错误。
难道我眼花了?
sa.sin_addr.s_addr = htonl(0xc0a80208);
这步感觉不对,楼主再check一下?
|
单步调试了吗?
执行到哪一行报错了?
执行到哪一行报错了?