求大神,什么条件下会执行 cerr << "unknown error: " << errno << "

QQ594373024 2013-11-28 07:42:47
代码有点多就不都复制上来,我只想知道什么条件会执行
cerr << "unknown error: " << errno << " " << connfd << endl;
exit(-1); // Who knows? Bad foo. Maybe continue for roubustness?
我是用cmd执行exe的

main.h
#ifndef MAIN_H
#define MAIN_H

#include "Listing.h"
#include "Group.h"

#define BACKLOG 11
#define REQ_MAX 8192

extern Listing advertisements;
extern Group users;

extern string active_user;

extern int advertisement_counter;

extern bool create_verified;
extern bool login_failed;

#endif


main.cpp



#include <winsock2.h>
#include <ws2tcpip.h>
#include <signal.h>
#include <cstdio>

#include <string>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <sstream>

#include "Listing.h"
#include "Group.h"
#include "processrequest.h"
#include "main.h"

using namespace std;

Listing advertisements;
Group users;

int advertisement_counter = 0;

string active_user = "";
bool create_verified = true;
bool login_failed = false;


/*
* Not this is explicitly not concurrent -- the auction data structures
* are not necessarily concurrency safe
*/

int main (int argc, char *argv[]) {



/*
* argv[1] should be the port number
*/
if (2 != argc) {
cout << "Usage: " << argv[0] << " [port]" << endl << endl;
return -1;
}

int port = atoi(argv[1]);


WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 0;
}



/*
* Create and configure the socket
*/
int connfd = -1;
int listenfd = -1;

struct sockaddr_in cliaddr;
struct sockaddr_in servaddr;
int clilen = sizeof (cliaddr);

listenfd = socket (AF_INET, SOCK_STREAM, 0);

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
servaddr.sin_port = htons(port);


/*
* Bind to the socket
*/
bind (listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));


/*
* Configure the listen queue
*/
listen (listenfd, BACKLOG);

/*
* Main work loop:
* Listen for connection, accept it, process request, print html page
*/


while (true) {

// Wait for the knock at the door
if (0 > (connfd = accept (listenfd,
(struct sockaddr *) &cliaddr, &clilen)) ) {

if (errno == EINTR)
continue;
else {
//问题在这里
cerr << "unknown error: " << errno << " " << connfd << endl;
exit(-1); // Who knows? Bad foo. Maybe continue for roubustness?

}


}

// Process the request
char req_buffer[REQ_MAX];
int count;



for (count=0; count<REQ_MAX; count++) {
recv (connfd, req_buffer+count, 1, 0);

if (4 == req_buffer[count]) { // 4 is EOT, a.ka. CTRL-D
req_buffer[count] = '\0';
break;
}
}


// Move request to stream for C++ style processing
ostringstream oss;
oss.str("");
oss << req_buffer;
memset (req_buffer, 0, REQ_MAX);

cerr << oss.str() << endl;

istringstream iss (oss.str());

processrequest (iss, connfd, port);

// Done with this request
closesocket (connfd);
connfd = -1;
}

}


感觉我这样没说清楚,,我也不知道该怎么说,代码那么多
...全文
71 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,678

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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