• Welcome to Valhalla Legends Archive.
 

w32 Winsock2 Problems

Started by TheNewOne, August 02, 2005, 06:55 PM

Previous topic - Next topic

TheNewOne


#include "winsock2.h"
SOCKET BnetSocket[1012]; long currentsock;

void bconnect(const int port, const char *server) {
//Includes liek everythign else needs these days
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
WSAStartup(version, &wsaData);
LPHOSTENT lpHostEntry;
int storebuf;
currentsock = currentsock + 1;
BnetSocket[currentsock] = socket(AF_INET,SOCK_DGRAM,0);     //
unsigned int buf;
buf = BnetSocket[currentsock];

if(BnetSocket[currentsock] == INVALID_SOCKET) {
MessageBox(0,"Error On Socket Create", "Error", MB_OK);
return;
};
   

lpHostEntry = gethostbyname(server);
SOCKADDR_IN BAS; BAS.sin_family = AF_INET;
BAS.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
BAS.sin_port = htons(port);

storebuf = connect(BnetSocket[currentsock], (LPSOCKADDR)&server, sizeof(struct sockaddr));
if (storebuf == SOCKET_ERROR) {
MessageBox(0,"Error On Connect", "Error", MB_OK);
return;
}
else {
MessageBox(0, "Connected!", "Worked!", MB_OK);
}

}


Alright at first it had some problems creating the socket properly, I fixed that.  Now it doesnt seem to connect to anything properly always sends me a connect error. Any Enlightenment on my problem well appreciated.

Eric

BnetSocket[currentsock] = socket(AF_INET,SOCK_DGRAM,0);

You're creating a UDP socket for a TCP/IP connection.

BnetSocket[currentsock] = socket(AF_INET,SOCK_STREAM,0);

MyndFyre

Two questions/comments:

1.) Why are you allocating a 1012-item array of SOCKETs when you only ever use 1 in code?

2.) I believe gethostbyname(const char*) doesn't resolve strings of dotted-quad IP addresses (so if you put in "192.168.0.1", it wouldn't resolve).  You might want to consider using an alternate, or checking if the result is valid.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Warrior

Is it possible to include the winsock 2 header yet use the version 1.1?
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Kp

Quote from: MyndFyre on August 02, 2005, 07:53 PM2.) I believe gethostbyname(const char*) doesn't resolve strings of dotted-quad IP addresses (so if you put in "192.168.0.1", it wouldn't resolve).  You might want to consider using an alternate, or checking if the result is valid.

Although it does the OP no good, it's worth noting that this limitation is not present in most implementations of gethostbyname.  Microsoft's is the only one which I know to have this flaw.

TheNewOne: perhaps you should pass a socket address to connect(2) [hence why it takes a SOCKADDR*], instead of passing it a char**? :)
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

TheNewOne

Fixed: Thanks kp for the insight on the sockaddr error I made it now connects fine.