Valhalla Legends Forums Archive | C/C++ Programming | w32 Winsock2 Problems

AuthorMessageTime
TheNewOne
[code]
#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);
}

}
[/code]

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.
August 2, 2005, 11:55 PM
LoRd
[code]BnetSocket[currentsock] = socket(AF_INET,SOCK_DGRAM,0);[/code]

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

[code]BnetSocket[currentsock] = socket(AF_INET,SOCK_STREAM,0);[/code]
August 3, 2005, 12:20 AM
Myndfyr
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.
August 3, 2005, 12:53 AM
Quarantine
Is it possible to include the winsock 2 header yet use the version 1.1?
August 3, 2005, 1:35 AM
Kp
[quote author=MyndFyre link=topic=12421.msg122891#msg122891 date=1123030432]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.[/quote]

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**? :)
August 3, 2005, 2:09 AM
TheNewOne
Fixed: Thanks kp for the insight on the sockaddr error I made it now connects fine.
August 3, 2005, 4:40 AM

Search