我已經更改我下面什麼看到的,這是我
#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