Connecting to IRC server and joing a #channel
I am trying to write a small plugin for a game server. It should allow the game server to effectively join an irc server, and then a channel. It's joining the server, and responding to ping after setting it's own nick, but after I receive all the junk messages the irc server wants to send, it seems to freeze up on JOIN, or rather, when it tries to receive the response back from the irc server, and it doesn't join the channel.
Can anyone offer some help? I am a real IRC newbie tbh...
Code:
DWORD WINAPI MainThread( LPVOID lpParam ){
char buf1[4096];
char nick[] = "rebot";
char text1[4096];
int n;
WORD wsver=MAKEWORD(2, 0);
int nret=WSAStartup(wsver, &wsaData);
if(nret != 0){
printf("Startup failed, error code: %d\n",WSAGetLastError());
WSACleanup();
return false;
}
printf("Init success\n");
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET){
printf("Socket init failed");
return false;
}
printf("Socket initialized\n");
sockaddr_in sin;
sin.sin_port=htons(6668);
sin.sin_addr.s_addr=inet_addr("85.25.143.169");
sin.sin_family=AF_INET;
if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
printf("Startup failed, error code: %d\n",WSAGetLastError());
WSACleanup();
return false;
}
printf("Connection successful!\n\n");
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
sprintf(text1, "NICK rebot2\r\nUSER rebot2 0 0 :rebot2\r\n");
send(kSock, text1, strlen(text1), 0);
printf(">>Client: %s\n",text1);
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
if (strstr(buf1,"PING")){
printf("I got a ping, cool!\n");
char* myStringPtr = buf1;
myStringPtr+=6;
char * pch;
pch = strtok (myStringPtr," =\n");
std::vector<std::string> str_Vector;
while (pch != NULL){
std::string strData = pch;
str_Vector.push_back(strData);
pch = strtok (NULL, " =\n");
}
sprintf(text1,"PONG :%s\r\n",str_Vector.at(0).c_str());
send(kSock, text1, sizeof(text1), 0);
printf(">>Client: %s\n",text1);
}
}
else {
printf(">>Server: No Data\n");
}
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
sprintf(text1,"JOIN #lobby\r\n");
send(kSock, text1, sizeof(text1), 0);
printf(">>Client: %s\n",text1);
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
/*
sprintf(text1,"PRIVMSG #lobby :Test Message\r\n");
send(kSock,text1,sizeof(text1),0);
printf(">>Client: %s\n",text1);
n = recv(kSock, buf1, 4096, 0);
if ( n > 0 ) {
printf(">>Server: %s\n",buf1);
}
else {
printf(">>Server: No Data\n");
}
*/
/*
// ping and pong, will obviously be moved...
while (1) {
recv( kSock,buf,255,0);
if (strstr(buf,"PING")) {
printf("Server sent PING\n");
send(kSock,"PONG :\r\n",128,0);
printf("Replying with PONG\n");
}
else{
printf(">>Server: %s\n",buf);
}
}
*/
return 1;
}
http://www.game-maps.net/staff/reborn/rebot2.JPG