我用C写郎IRC客户端。 并且在连接到serwer遇到了一些问题。 我碰到下面当我运行程序:
OUTPUT
Set Fully Qualified host Domain Name(human readable): ::automaticaly provided::
Set the port number of the server You want to connect to: ::automaticaly provided::
Destination server IP: 88.190.23.245
Socket descriptor: 3
Connection has been successfully established
Peer's IP is: 88.190.23.245
Peer's port is: 5190
:irc2.gbatemp.net NOTICE AUTH :*** Looking up your hostname...
:irc2.gbatemp.net NOTICE AUTH :*** Found your hostname (cached)
Type Your nick name: ::automaticaly provided::
Type Your user name: ::automaticaly provided::
(10-20 seconds break here and than what follows down here)
ERROR :Closing Link: thisIsMyNickNameXXXa[85.221.165.54] (Ping timeout)
temp.net NOTICE AUTH :*** Found your hostname (cached)
ERROR :Closing Link: thisIsMyNickNameXXXa[85.221.165.54] (Ping timeout)
temp.net NOTICE AUTH :*** Found your hostname (cached)
ERROR :Closing Link: thisIsMyNickNameXXXa[85.221.165.54] (Ping timeout)
temp.net NOTICE AUTH :*** Found your hostname (cached)
.......
.............
...................
================================================== ===========
::automaticaly provided::
-由一个程序现已通过手段,所以我不必须键入了很多次。
顺便说一句。 我连接到irc.gbatemp.net:5190(就我而言无需密码)
提供必要的数据10-20秒断裂发生后(i的输出部分指定)和比ERROR temp.net部分如下循环往复(ⅰ用点标记的话)
所以,问题的主要部分是如何以及何时我应该在respons发送PONG消息PING? 我做我的研究,但仍然无法做到这一点。 为什么我看不到在STDOUT PING消息?
我提供负责输出和PINGPONG部分波纹管(评论)的代码(主要的问题,现在是乒乓的错误状态Ping超时)propably而(1)循环是不完美的,但我想它的另一个话题s对象)代码波纹管:
int readReady=0;
int writeReady=0;
pid_t pID;
char buf[1024]; //I/O buffer (?)
pid_t sID;
char *NICK = "NICK thisIsMyNickNameXXXa\n\r";
char *USER = "USER tomaazrxtc 8 * :nameandsurname";
char ping[512];
char *change;
pID=fork();
if(pID < 0){
//failed to execute fork()
perror("Error while forking");
getchar();getchar();
exit(1);
}
if(pID > 0){
exit(0);
}
//child down here
//setting new session
sID = setsid();
if(sID < 0){
perror("Error while setting new session");
getchar();getchar();
exit(1);
}
//---------receiving NOTICE AUTH :*** part-------------------------------
if(recv(sockfd, buf, 1024,0)>0){
printf(buf);
}
else{
perror("Error while receiving data");
}
//---------providing and sending NICK and USERNAME-----------------------
printf("Type Your nick name: \n");
//scanf(nickname); pamietaj zeby zapewnic podawanie tylko nicku, a format handler zrobic osobno
send(sockfd, NICK, strlen(NICK), 0);
printf("Type Your user name: \n");
//scanf(username); pamietaj zeby zapewnic podawanie tylko nicku, a format handler zrobic osobno
send(sockfd, USER, strlen(USER), 0);
//--------Shoudnt I receive PING message just here ?????-----------------
recv(sockfd, buf, strlen(buf), 0);
printf(buf);
//--------PONG'ing function which I havent tested yet since i cant see PING message----
recv(sockfd, ping, 512,0);
if(strstr(ping, "PING")){
change = strstr(ping, "PING");
strncpy(change, "PONG", 4);
send(sockfd, ping, 512, 0);
}
//-------------------------------------------------------------------------
while(1){
//sleep(1);
if((readReady = readReadiness(sockfd))==0){ //nothing to recv
if((writeReady = writeReadiness(sockfd))>0){ //sending is possible
scanf(buf);
send(sockfd, buf, strlen(buf), 0);
continue;
}
else
continue; //if there s no data to read and cant send (is it even possible?)
}
else{ //if there s some data to recv() on the socket buffer
recv(sockfd, buf, strlen(buf), 0);
printf(buf);
continue;
}
}
//--------------------------------------------------------------------------
好。 我将离开别人的问题在未来提供答案。 这是繁琐。
我刚添加\ n \ r在USER变量的末尾(就像在NICK字符串)。 连接就像一个魅力!
最后 : ))