win sockets

Im using a simple irc bot code and moding it so that i can learn more about c++ and have fun with it. But on lines 43-48 theres a problem. It wont message the channel with pong. On line 48 it prints the command sent to the server to the console and the command is correct (PRIVMSG #locked : Pong!). The quit works in the else if statement but always leaves out the reason. When i place the code outside of the if statement it messages the channel fine (even in the while loop and above it)

A question on memset, in this example is it used to simply split the text up so we dont go over chars limit?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
	#include "stdafx.h" 
	char *server = "69.162.163.86";
	int port = 6667;
	int numb;
	char *nick = "IrcBot";
	char *chan = "#locked";
	char buff[4096];
	char tmp[4096];
	
   int main()
   {
   srand(time(0));
   numb = rand()%99+1;
       
   WSADATA WSAData;
   WSAStartup(MAKEWORD(2,0), &WSAData);
   SOCKADDR_IN sin;
   SOCKET sock;  
   sock = socket(AF_INET, SOCK_STREAM, 0);
   sin.sin_addr.s_addr			= inet_addr(server);
   sin.sin_family				= AF_INET;
   sin.sin_port				= htons(port);
   connect(sock, (SOCKADDR *)&sin, sizeof(sin));
	
   memset(tmp,0,255);
   sprintf(tmp,"USER LockBot \"\" \"127.0.0.1\" :%s%i%c",nick,numb,10);
   send(sock,tmp,strlen(tmp),0);
	
   memset(tmp,0,255);
   sprintf(tmp,"NICK %s%i%c",nick,numb,10);
   send(sock,tmp,strlen(tmp),0);

   Sleep(100);

   memset(tmp,0,255);
   sprintf(tmp,"JOIN %s%c",chan,10);
   send(sock,tmp,strlen(tmp),0);

   while(1){
       memset(buff,0,255);
       recv( sock,buff,255,0);
       cout << buff <<endl;
      if ( strstr(buff,"PING") != 0 )  
      {
       send(sock,"PONG :\r\n",6,0);
       sprintf(tmp,"PRIVMSG %s : Pong!%c",chan,10);
       send(sock,tmp,strlen(tmp),0); 
       cout << tmp;
       
      }
	  else if ( strstr(buff,"!bot quit") !=0){
	  sprintf(tmp,"QUIT :Leaving... %c",10);
	  send(sock,tmp,strlen(tmp),0);    
	  cout << tmp ;
	  }
   };
}
Last edited on
When i remove send(sock,"PONG :\r\n",6,0); from line 45, it seems to work. Anyone have any idea as to why that happens?
It's impossible to tell what's going on without seeing the server side. But I'll make a few observations.

You treatment of buffers is incorrect. You declare a couple of 4K buffers, tmp and buff, but assume they're 255 bytes long. Don't use magic numbers in your program (like 255) and use the correct size instead; that is, sizeof(tmp) and sizeof(buff) respectively.

TCP is stream oriented protocol. This means, there are no "end of record" markers, you have to make up your own scheme for determining the end of a message. In your code, it would make sense to use \n or \r\n as you've sort of started to already.

The recv() calls don't necessarily correspond to each send() call. Only by using your own end of message marker can the two sides properly correspond. For example, HTTP uses \r\n\r\n as an end of message marker.

What do you think get's sent with:
 
send(sock,"PONG :\r\n",6,0);
As you can see, magic numbers are a problem.
Topic archived. No new replies allowed.