编译没错误,运行时Segmentation fault(高分悬赏)

末世剩人 2013-06-05 11:30:38
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <stdlib.h>


main(argc,argv)
int argc;

char **argv;

{

int sockfd;
FILE *fp1;

struct sockaddr_in server;

struct hostent *hp, *gethostbyname();

char msg[1];

sockfd=socket(AF_INET,SOCK_STREAM,0);
fp1=fopen("file.txt","r");

if (sockfd<0)

printf("Opening stream socket");

if ((hp=gethostbyname(argv[1]))==NULL){

fprintf(stderr,"%s:unknown host\n",argv[1]);

exit(2);

}

server.sin_family=AF_INET;
bcopy((char *)hp->h_addr,(char *)&server.sin_addr.s_addr,hp->h_length);
server.sin_port=htons(atoi(argv[2]));
if (connect(sockfd,(struct sockaddr *)&server,sizeof(server))<0)
printf("connecting stream socket");
while((fread(msg,1,1,fp1))>0){
//printf("Enter send message: %s",msg);
//scanf("%s",msg);
if(!strlen(msg)) break;
if(send(sockfd,msg,strlen(msg),0)<0)
printf("sending message");
bzero(msg,sizeof(msg));
}
printf("EOF...disconnect\n");
close(sockfd);
exit(0);

}


看了半天没看出哪有问题!大侠帮我看看哪错了,谢谢了。
...全文
295 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
末世剩人 2013-06-07
  • 打赏
  • 举报
回复
引用 11 楼 liangqingsong5300 的回复:
我怎么越看越像是没有绑定地址造成的呢。如果我没有记错客服端也需要绑定端口吧。
怎么绑定呢。
liangqingsong5300 2013-06-07
  • 打赏
  • 举报
回复
我怎么越看越像是没有绑定地址造成的呢。如果我没有记错客服端也需要绑定端口吧。
ma100 2013-06-06
  • 打赏
  • 举报
回复
gcc -g a.cpp gdb ./a.out r bt
末世剩人 2013-06-06
  • 打赏
  • 举报
回复
我直接./program 运行
JoeBlackzqq 2013-06-06
  • 打赏
  • 举报
回复
程序是没有错误,但是运行时要输入两个参数。 不知道你是如何运行的?最好帖出过程来
Ericz 2013-06-06
  • 打赏
  • 举报
回复
你定义了一个只有一个元素的数组, char msg[1]; 然后用这个数组当字符串使用: if(!strlen(msg)) break; msg只有一个空间,然而strlen是以字符串末尾的'\0'标示字符串结束, 这句话出现了问题。运行结果依赖于你系统当前的状态
AnYidan 2013-06-06
  • 打赏
  • 举报
回复
char msg[1]; Segmentation fault 一般是非法访问内存,基本是指针没有初始化或数组越界
soaliap 2013-06-06
  • 打赏
  • 举报
回复
貌似没有问题吧,楼主运行 ./program www.gnu.org 试试,应该是你参数传的不对
赵4老师 2013-06-06
  • 打赏
  • 举报
回复
进程意外退出会在当前目录下产生‘code’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
wqkjj 2013-06-06
  • 打赏
  • 举报
回复
引用 4 楼 czd327917086 的回复:
我直接./program 运行
程序需要两个参数:主机名称或者IP地址 服务器端口号 不带参数运行的话,应该在这句 if ((hp=gethostbyname(argv[1]))==NULL){ ...... } core了 另外,char msg[1];,会导致下面两句很玄 if(!strlen(msg)) break; if(send(sockfd,msg,strlen(msg),0)<0) printf("sending message"); 最后,最笨也是最有效的办法是printf(....)
末世剩人 2013-06-05
  • 打赏
  • 举报
回复
[root@localhost ~]# gdb ./a.out
GNU gdb (GDB) CentOS (7.0.1-45.el5.centos)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/a.out...(no debugging symbols found)...done.
(gdb) bt
No stack.
ma100 2013-06-05
  • 打赏
  • 举报
回复
gcc a.cpp gdb ./a.out bt

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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