Valhalla Legends Forums Archive | C/C++ Programming | Socket not connecting

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

[code]
#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;
}
[/code]

Here's the output I get when I run the program:
[code]
struct
bzero
socket information set
socket->46329463
Connect?->-1
Unable to connect socket!
[/code]
any ideas?
February 7, 2004, 6:37 PM
Maddox
You need to call WSAStartup() first.

For any other errors call WSAGetLastError() and look up the error code you get.
February 7, 2004, 6:41 PM
Eli_1
thanks madd, the socket connects now, but it isn't logging in.
[code]
#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; }
}
[/code]
is there something wrong with my connect_string?
February 7, 2004, 6:49 PM
Maddox
This should work.

[code]
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);
[/code]
February 7, 2004, 8:08 PM
Eli_1
[quote author=Maddox link=board=30;threadid=5153;start=0#msg42976 date=1076184537]
[code]
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);
[/code]
[/quote]
what is sprintf(connect_string,"\x03\x04%s\r\n%s\r\n/join %s\r\n", username, password, homechannel)?
February 7, 2004, 8:17 PM
Maddox
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_sprintf_swprintf.asp
February 7, 2004, 8:25 PM
Eli_1
I read it but I still don't understand what \r and %s are
February 7, 2004, 8:33 PM
Maddox
[quote author=Eli_1 link=board=30;threadid=5153;start=0#msg42982 date=1076186007]
I read it but I still don't understand what \r and %s are
[/quote]

\r is a carriage return, and %s represents a string.
February 7, 2004, 8:57 PM
Grok
Need more Maddox's around here. +1 for helping, then following through.
February 8, 2004, 2:20 AM
Kp
[quote author=Eli_1 link=board=30;threadid=5153;start=0#msg42967 date=1076179770]
[code]
strcpy(connect_string,"");
strcpy(connect_string,"Bot(1)\n");
strcpy(connect_string,"test\n");
strcpy(connect_string,"/join hideout\n");
[/code]is there something wrong with my connect_string?[/quote]

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).
February 8, 2004, 2:58 AM
Eli_1
thanks all of you, especially maddox, you've been very helpful.
February 8, 2004, 5:14 AM
Marine
does this still work?
June 6, 2004, 9:18 AM
Eli_1
[quote author=Marine link=board=30;threadid=5153;start=0#msg63822 date=1086513531]
does this still work?
[/quote]
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.
June 6, 2004, 5:12 PM

Search