• Welcome to Valhalla Legends Archive.
 

Socket not connecting

Started by Eli_1, February 07, 2004, 12:37 PM

Previous topic - Next topic

Eli_1

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?

Maddox

#1
You need to call WSAStartup() first.

For any other errors call WSAGetLastError() and look up the error code you get.
asdf.

Eli_1

#2
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?

Maddox

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);
asdf.

Eli_1

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)?


Eli_1

I read it but I still don't understand what \r and %s are

Maddox

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.
asdf.

Grok

Need more Maddox's around here.  +1 for helping, then following through.

Kp

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).
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Eli_1

thanks all of you, especially maddox, you've been very helpful.

Marine


Eli_1

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.