为什么我收到错误的文件描述符错误?

时间:2011-09-13 17:57:46

标签: c++ sockets client

我正在尝试编写一个类似客户端的短程序,如telnet。 我收到的用户输入如下:www.google.com 80(端口号)和/index.html 但是,我收到了一些错误。当我写一些调试信息时,它说我有一个错误的文件描述符和文件描述符100上的读取失败messagesize = 0.

struct hostent * pHostInfo;
struct sockaddr_in Address;
long nHostAddress;
char pBuffer[BUFFER_SIZE];
unsigned nReadAmount;
int nHostPort = atoi(port);

vector<char *> headerLines;
char buffer[MAX_MSG_SZ];
char contentType[MAX_MSG_SZ];

int hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (hSocket == SOCKET_ERROR)
{
  cout << "\nCould Not Make a Socket!\n" << endl;
  return 1;
}
pHostInfo = gethostbyname(strHostName);
memcpy(&nHostAddress,pHostInfo -> h_addr, pHostInfo -> h_length);

Address.sin_addr.s_addr = nHostAddress;
Address.sin_port=htons(nHostPort);
Address.sin_family = AF_INET;

if (connect(hSocket, (struct sockaddr *)&Address, sizeof(Address)) == SOCKET_ERROR)
{
  cout << "Could not connect to the host!" << endl;
  exit(1);
}
char get[] = "GET ";
char http[] = " HTTP/1.0\r\nHost: ";
char end[] = "\r\n\r\n";
strcat(get, URI);
strcat(get, http);
strcat(get, strHostName);
strcat(get, end);
strcpy(pBuffer, get);
int len = strlen(pBuffer);
send(hSocket, pBuffer, len, 0);
nReadAmount = recv(hSocket,pBuffer, BUFFER_SIZE, 0);
//Parsing of data here, then close socket
if (close(hSocket) == SOCKET_ERROR)
{
  cout << "Could not close the socket!" << endl;
  exit(1);
} 

谢谢!

3 个答案:

答案 0 :(得分:6)

您的代码中有buffer overrun。您试图将一堆字符数组一起分配到一个缓冲区中,而不只是5个字节长:

char get[] = "GET ";  // 'get' is 5 bytes long
char http[] = " HTTP/1.0\r\nHost: ";
char end[] = "\r\n\r\n";
strcat(get, URI);  // BOOM!
strcat(get, http);
strcat(get, strHostName);
strcat(get, end);

这可能会覆盖您的本地变量hSocket,从而导致“错误的文件描述符错误”。

由于您使用的是C ++(代码中有vector),因此只需使用std::string而不是C字符数组,这样您就不必担心内存管理了。

答案 1 :(得分:0)

“文件描述符错误”的另一个原因可能是偶然地尝试将write()写入只读描述符。我之所以停留一段时间,是因为我一直在寻找第二个close()或缓冲区溢出。

答案 2 :(得分:0)

尝试写入您没有写许可权的位置时,还会遇到“文件描述符错误”的另一个原因。例如,在Windows环境中尝试使用wget下载内容时:
Cannot write to 'ftp.org.com/parent/child/index.html' (Bad file descriptor).