日期:2014-05-16  浏览次数:20752 次

[求助]这段程序内存哪儿出问题了哈
错误在HttpSocket.cpp中的Socket函数中,ppe=gethostbyname( "tcp "),就是内存错误,但是我不晓得为什么错了....

bool   CHttpSocket::Socket()
{
if(m_bConnected)   return   false;

struct   protoent   *ppe=NULL;
ppe=getprotobyname( "tcp ");

m_s=socket(PF_INET,SOCK_STREAM,ppe-> p_proto);
if(m_s==-1)
{
printf( "socket()   failed!\n ");
return   false;
}

return   true;
}

gdb调试的提示是这样的:
****************************************************
CHttpSocket::Socket   (this=0xbff9a660)   at   HttpSocket.cpp:31
31   if(m_bConnected)   return   false;
(gdb)   n
33   struct   protoent   *ppe=NULL;
(gdb)   n
34   ppe=getprotobyname( "tcp ");
(gdb)   n
***   glibc   detected   ***   free():   invalid   next   size   (normal):   0x09e20cd8   ***

Program   received   signal   SIGABRT,   Aborted.
0x001327a2   in   _dl_sysinfo_int80   ()   from   /lib/ld-linux.so.2
(gdb)   bt
#0   0x001327a2   in   _dl_sysinfo_int80   ()   from   /lib/ld-linux.so.2
#1   0x001727f5   in   raise   ()   from   /lib/tls/libc.so.6
#2   0x00174199   in   abort   ()   from   /lib/tls/libc.so.6
#3   0x001a64ea   in   __libc_message   ()   from   /lib/tls/libc.so.6
#4   0x001acc6f   in   _int_free   ()   from   /lib/tls/libc.so.6
#5   0x001acfea   in   free   ()   from   /lib/tls/libc.so.6
#6   0x0019d516   in   fclose@@GLIBC_2.1   ()   from   /lib/tls/libc.so.6
#7   0x00825305   in   _nss_files_getprotobyname_r   ()   from   /lib/libnss_files.so.2
#8   0x00229332   in   getprotobyname_r@@GLIBC_2.1.2   ()   from   /lib/tls/libc.so.6
#9   0x002291f1   in   getprotobyname   ()   from   /lib/tls/libc.so.6
#10   0x0804961e   in   CHttpSocket::Socket   (this=0xbff9a660)   at   HttpSocket.cpp:34
#11   0x08048ed4   in   Run   (strUrl=0xbffcdb25   "http://hi.baidu.com ")   at   http.cpp:54
#12   0x08048d60   in   main   (args=2,   argv=0xbff9b074)   at   http.cpp:16


代码在这儿     [url=http://bbs.nettf.net/forums/index.php?act=Attach&type=post&id=8533]http.rar[/url]


------解决方案--------------------
pp类型错误.应该为
struct hostent
------解决方案--------------------
ppe
------解决方案--------------------
If no error occurs, getprotobyname returns a pointer to the protoent. Otherwise, it returns a null pointer
so,the program will be dump at m_s=socket(PF_INET,SOCK_STREAM,ppe-> p_proto);
------解决方案--------------------
ppe=getprotobyname( "tcp ");
没有检查ppe的值
------解决方案--------------------
这段代码是没有问题的,一定是其它的部分引起的问题,下面的程序是可以运行的。
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include <stdio.h>

#include <iostream>
using namespace std;

class CHttpSocket
{
public: