我已经更改我下面什么看到的,这是我
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string>
#include <vector>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <errno.h>
using namespace std;
string buffer;
vector<string> ex;
int s;
void recvline (int s, string* buf) {
char in, t;
while (1) {
recv (s, &in, 1, 0);
*buf += in;
if (in == 10) {
t = 1; }
if (t && in == 13) {
break; }
}
}
void push (int s, string msg) {
string o = msg + "\r\n";
cout << "SENT:", o;
send (s, o.c_str(), o.size(), 0);
}
int main (int argc, char *argv[]) {
if (argc < 3) {
cout << "Insufficient Arguments" << endl;
exit (7); }
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s < 0)
exit (1);
struct hostent h = *gethostbyname (argv[1]);
struct sockaddr_in c;
c.sin_family = AF_INET;
c.sin_port = htons(atoi(argv[2]));
c.sin_addr.s_addr = inet_addr (h.h_addr_list[0]);
if (connect (s, (struct sockaddr*)&c, sizeof c) != 0) {
cout << "Unable to connect to network" << endl;
cout << strerror(errno) << endl;
exit (2);
}
push (s, "USER LOLwat Lw lol.wat :LOLwat");
push (s, "NICK LOLwat");
while (true) {
recvline (s, &buffer);
cout << buffer;
if (buffer.substr(0,4).c_str() == "PING")
push (s, "PONG " + buffer.substr(6,-2));
}
}
这是结果:
[[email protected] Desktop]$ g++ ?.cpp -o 4096 -
[[email protected] Desktop]$ ./4096 irc.scrapirc.com 6667 - Unable to connect to network - Network is unreachable
您应该检查'errno'的值来查看实际的错误是什么。 – 2010-06-23 04:26:31
另外,注意你的大括号 - 有一些地方缩进建议两行将一起执行,但他们不(例如,在无法连接到网络错误之后)。我建议使用自动缩进编辑器或其他东西。 – bdonlan 2010-06-23 04:27:54
*总是*使用花括号。 C不是Python。 – 2010-06-23 04:31:03