|
-
September 15th, 2017, 08:22 PM
#1
IRC Socket - Stop receiving after 2 minutes
Hi guys!
Someone can tell me why after about 2 minutes I stop receiving data? I see all the connection reply from the server, the bot join the channel, I can talk to him in private or in channel but if I wait about 2 minutes then.. nothing. The bot stay connected but that's it. I can't see the messages, nothing.
I have tried many things for the past 4 hours.. including a lot of google search.. I can't figure out what is the problem.
Thanks
Code:
int status, sockfd, conn;
ssize_t receivedBytes;
char buffer[MAXDATASIZE];
int main()
{
struct addrinfo hints;
struct addrinfo *servinfo; //will point to the results
//make sure the struct above is clear
memset(&hints, 0, sizeof hints);
//setup hints
hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
//Setup the structs if error print why
status = getaddrinfo(server.c_str(),port.c_str(),&hints,&servinfo);
//si getaddrinfo retourne pas 0 il y a un erreur
if (status != 0)
{
cout << "getaddrinfo:" << gai_strerror(status) << endl;
}
//setup the socket
sockfd = socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol);
//si le socket retourne -1 il y a un erreur
if (sockfd == -1)
{
perror("client: socket error");
}
//Connection to the socket
conn = connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
// si la connection retourne -1 il y a une erreur
if (conn == -1)
{
close (sockfd);
perror("client: error connecting to the socket");
}
if (servinfo == NULL) {
cout << "Failed to connect" << endl;
}
//cout << "#### DEBUG 01 ####" << endl;
addpredefinedaliases(); //ajoute les alias par default
//creer un loop infini (1) qui va lire les réponses du serveur tout la duré d'éxécution du prog.
//s'assure que nous somme connecté. voir env. L238
int count = 0;
while (1){
if(count != 1){
//after 3 message du server send data to server (as per IRC protacol)
sendData("USER "+username);
sendData("NICK "+nick);
count++;
}
//grace au loop infini, reste * l'écoute des réponses du serveur
//les réponses sont stocké dans buffer
receivedBytes = recv(sockfd, buffer, MAXDATASIZE-1,0);
//break if connection closed
if (receivedBytes == 0)
{
cout << "----------------------CONNECTION CLOSED BY REMOTE---------------------------"<< endl;
cout << timeNow() << endl;
break;
}else if(receivedBytes == -1){
cout << "----------------------ERROR---------------------------"<< endl;
cout << timeNow() << endl;
break;
}else{
cout << receivedBytes << " :" << buffer; //affiche le buffer
}
//répond au ping si envoyé. Protocol IRC
if (recvPing(buffer, "PING"))
{
sendPong(buffer);
}
string str_buf = buffer;
//join le channel lorsque connecté
if ((str_buf.find("376") != string::npos) && settings.autoJoin_onConnect){
sendData("JOIN "+channel+" "+channelpwd);
}
//traite les messages recu du server
//msgHandler(buffer);
//clean buffer et le remet * 0 * chaque nouveau message du server. Sinon lorsque buffer = MAXDATASIZE ça renvoie plus rien car plein.
bzero(buffer, sizeof(buffer));
}
//libere la mémoire de la structure pointeur *servinfo
freeaddrinfo(servinfo);
return 0;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|