输出
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秒发生中断(我在OUTPUT部分指定),而不是ERROR temp.net部分无限追随(我用点标记)
问题的主要部分是我应该如何以及何时发送PONG消息以响应PING?我做了我的研究,但仍然无法做到。为什么我不能在STDOUT中看到PING消息?
我提供负责输出的代码和PINGPONG部分(评论) (现在的主要问题是PING PONG为错误状态PING TIMEOUT),而(1)循环还不完美,但我认为它是另一个主题的主题)代码如下:
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;
}
}
//--------------------------------------------------------------------------
确定。我将来会向其他人提出问题并提供答案。这很简单。
我刚刚在USER变量的末尾添加了\ n \ r \ n(就像在NICK字符串中一样)。 像魅力一样连接!!
最后:))
答案 0 :(得分:3)
所以,我发现的一些问题是:
NICK
未正确终止。它应该是\r\n
。USER
根本没有终止,它应该以{{1}}结尾。\r\n
。 512
不适用于字符串,它适用于原始数据。所以,你也会在这里传递大量的垃圾。您需要根据收到的内容传递长度。send()
不安全。碰巧包含格式说明符的任何传入字符串都会导致printf(buf);
尝试解释它们(这称为“格式字符串漏洞”)。应该用printf()
替换它们,以便以安全的方式实现相同的行为。printf("%s", buf);
的返回值来了解您收到的数据量。recv()
来确定缓冲区的大小。此函数计算字符串的长度,而不是缓冲区的大小。您应该使用strlen()
运算符。sizeof
应该做什么,但几乎可以肯定不是你想要的。也许您在寻找scanf(buf);
?fgets()
ping请求发生在recv()
之后。你怎么知道它们将在何时以及将会发生多久?好像你应该一直使用相同的缓冲区。