Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Eli_1 on February 07, 2004, 12:37 PM

Title: Socket not connecting
Post by: Eli_1 on February 07, 2004, 12:37 PM
Trying to do a simple bot now that will just connect and join a channel.
The server it's trying to connect to was up when I did this testing.


#include <winsock.h>
#include <iostream.h>
#include <memory.h>
#include <conio.h>
#include <string.h>

char connect_string[256];
SOCKET socket_handle;
int main() {
   struct sockaddr_in sa;
   struct hostent *hp;
   cout << "struct\n";
   //hp=gethostbyname(hostname);
   //if(hp==NULL){return INVALID_SOCKET;}                               
   // Clrs the memory in struct
   
   ZeroMemory(&sa,sizeof(sa));
   cout << "bzero\n";

   //Set mem address
   //memcpy(&sa.sin_addr,hp->h_addr,hp->h_length);

   sa.sin_family=AF_INET;
   sa.sin_port=htons(6112);
   sa.sin_addr.s_addr=inet_addr("66.132.130.34");
   cout << "socket information set\n";

   socket_handle=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
   cout << "socket->" << socket_handle << "\n";
   int i;
   i=connect(socket_handle,(sockaddr*)&sa,sizeof(sa));
   cout << "Connect?->" << i << "\n";
   if (i==0) {
      cout << "Connected!\n";
      strcpy(connect_string,""); /*(char)3*/
      strcpy(connect_string,"Eli_1\n");
      strcpy(connect_string,"************\n");
      strcpy(connect_string,"/join hideout\n");
      send(socket_handle,connect_string,strlen(connect_string), 0);
      cout << "Logging in!\n";
   } else { cout << "Unable to connect socket!\n\n"; }
   return 0;
}


Here's the output I get when I run the program:

struct
bzero
socket information set
socket->46329463
Connect?->-1
Unable to connect socket!

any ideas?
Title: Re:Socket not connecting
Post by: Maddox on February 07, 2004, 12:41 PM
You need to call WSAStartup() first.

For any other errors call WSAGetLastError() and look up the error code you get.
Title: Re:Socket not connecting
Post by: Eli_1 on February 07, 2004, 12:49 PM
thanks madd, the socket connects now, but it isn't logging in.

#include <winsock.h>
#include <iostream.h>
#include <memory.h>
#include <conio.h>
#include <string.h>

char connect_string[256];
SOCKET socket_handle;
bool connected;

int main() {
   connected=false;

   WSAData info;
   WSAStartup(MAKEWORD(1,1),&info);
   struct sockaddr_in sa;
   struct hostent *hp;
   cout << "struct\n";
   //hp=gethostbyname(hostname);
   //if(hp==NULL){return INVALID_SOCKET;}                               
   // Clrs the memory in struct
   
   ZeroMemory(&sa,sizeof(sa));
   cout << "bzero\n";

   //Set mem address
   //memcpy(&sa.sin_addr,hp->h_addr,hp->h_length);

   sa.sin_family=AF_INET;
   sa.sin_port=htons(6112);
   sa.sin_addr.s_addr=inet_addr("66.132.130.34");
   cout << "socket information set\n";
   socket_handle=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
   cout << "socket->" << socket_handle << "\n";
   int i;
   i=connect(socket_handle,(sockaddr*)&sa,sizeof(sa));
   cout << "Connect?->" << i << "\n";
   if (i==0) {
      cout << "Connected!\n";
      strcpy(connect_string,"");
      strcpy(connect_string,"Bot(1)\n");
      strcpy(connect_string,"test\n");
      strcpy(connect_string,"/join hideout\n");
      send(socket_handle,connect_string,strlen(connect_string), 0);
      cout << "Logging in!\n";
      connected=true;   
   } else {
      cout << "Unable to connect socket!\n\n";
      cout << "error->" << WSAGetLastError() << "\n";
      return 0;
   }
   while (connected==true) { continue; }
}

is there something wrong with my connect_string?
Title: Re:Socket not connecting
Post by: Maddox on February 07, 2004, 02:08 PM
This should work.


char username[] = "Bot(1)";
char password[] = "test";
char homechannel[] = "hideout";
sprintf(connect_string,"\x03\x04%s\r\n%s\r\n/join %s\r\n", username, password, homechannel);
Title: Re:Socket not connecting
Post by: Eli_1 on February 07, 2004, 02:17 PM
Quote from: Maddox on February 07, 2004, 02:08 PM

char username[] = "Bot(1)";
char password[] = "test";
char homechannel[] = "hideout";
sprintf(connect_string,"\x03\x04%s\r\n%s\r\n/join %s\r\n", username, password, homechannel);

what is sprintf(connect_string,"\x03\x04%s\r\n%s\r\n/join %s\r\n", username, password, homechannel)?
Title: Re:Socket not connecting
Post by: Maddox on February 07, 2004, 02:25 PM
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_sprintf_swprintf.asp
Title: Re:Socket not connecting
Post by: Eli_1 on February 07, 2004, 02:33 PM
I read it but I still don't understand what \r and %s are
Title: Re:Socket not connecting
Post by: Maddox on February 07, 2004, 02:57 PM
Quote from: Eli_1 on February 07, 2004, 02:33 PM
I read it but I still don't understand what \r and %s are

\r is a carriage return, and %s represents a string.
Title: Re:Socket not connecting
Post by: Grok on February 07, 2004, 08:20 PM
Need more Maddox's around here.  +1 for helping, then following through.
Title: Re:Socket not connecting
Post by: Kp on February 07, 2004, 08:58 PM
Quote from: Eli_1 on February 07, 2004, 12:49 PM

     strcpy(connect_string,"");
     strcpy(connect_string,"Bot(1)\n");
     strcpy(connect_string,"test\n");
     strcpy(connect_string,"/join hideout\n");
is there something wrong with my connect_string?

The reason this didn't work is that you set connect_string to the control 3, then replaced that with your username, then that with your password, then that with your /join command.  The first three operations are each overridden by the following.  Thus, you only actually sent the /join (without having logged in).  As an alternative to Maddox's solution, you could change the last three strcpys to strcat (though his solution is IMO more elegant).
Title: Re:Socket not connecting
Post by: Eli_1 on February 07, 2004, 11:14 PM
thanks all of you, especially maddox, you've been very helpful.
Title: Re:Socket not connecting
Post by: Marine on June 06, 2004, 04:18 AM
does this still work?
Title: Re:Socket not connecting
Post by: Eli_1 on June 06, 2004, 12:12 PM
Quote from: Marine on June 06, 2004, 04:18 AM
does this still work?
The origional code posted above never did work. But if you make the corrections named in some of the replys, you'll know how to make it work. And since this is your first post, try not to bring up dead topics like this.

AIM me some time (x 666 treme) and I'll try to help you out.