Valhalla Legends Forums Archive | C/C++ Programming | Winsock problem

AuthorMessageTime
BreW
So, im working on my bot, and i get an ipban from an invalid cdkey. Ever since then, it hasnt been working right at all. It seems no matter what server i connect to, i get a winsock error of 6, which i think means invalid handle.
it seems i get the callback for FD_CLOSE as soon as i connect, and when i check the error, the error is that, 6.
[code]
bool Connect(char *Server, unsigned short Port) {
if (connected)
Disconnect();
WSADATA wsadata;
struct hostent *hstEnt;
int WSAStartupError = WSAStartup(0x0101, &wsadata);
if (WSAStartupError) {
AddChat(vbRed, strcat("[WS32] WSAStartup failure: ", CStr(WSAStartupError)));
return false;
}
if (wsadata.wVersion != 0x0101) {
AddChat(vbRed, strcat("[WS32] Unavaliable winsock version: ", CStr(wsadata.wVersion)));
WSACleanup();
return false;
}
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET) {
AddChat(vbRed, "[WS32] Could not create raw socket.");
}
memset(&sName, 0, sizeof(sName));
sName.sin_family = AF_INET;
sName.sin_port = htons(Port);
char *p = Server;
while (*p && (isdigit(*p) || (*p == '.')))
p++;
if(*p){
hstEnt = gethostbyname(Server);
if(hstEnt == 0)
return false;
AddChat(vbYellow, p);
memcpy(&sName.sin_addr, hstEnt->h_addr, hstEnt->h_length);
} else {
sName.sin_addr.s_addr = inet_addr(Server);
}
if (connect(s, (struct sockaddr *)&sName, sizeof(sName))) {
AddChat(vbRed, "[WS32] Winsock connect error");
AddChat(vbRed, CStr(WSAGetLastError()));
AddChat(vbRed, strcat("[WS32] WSAGetLastError(); == ", CStr(WSAGetLastError())));
return false;
}
WSAAsyncSelect(s, hWnd_main, WM_USER, FD_READ | FD_CLOSE);  
char buf1[64];
sprintf(buf1, "[BNET] Connected to %s!", bot.server);
AddChat(vbGreen, buf1);
//send(s, "\x1", 1, 0);
//if (strcmp(bot.client, "SSHR") == 0) {
// Send0x05();
// Send0x06();
// } else {
// Send0x50();
// }
return true;
}

[/code]

[code]
case WM_USER + 1:
AddChat(vbCyan, "WS DataArrival!!!");
if (LOWORD(lParam) == FD_CLOSE) {
connected = false;
AddChat(vbRed, "[BNET] Winsock Close");
int asdferror;
asdferror = WSAGetLastError();
if (asdferror) {
char sdfg[64];
sprintf(sdfg, "[BNET] Winsock Error %d.", asdferror);
AddChat(vbRed, sdfg);
WSASetLastError(0);
}
Disconnect();
return 0;
}
char szBuffer[2048];
int PacketLen;
PacketLen = recv(s, strBuffer, sizeof(strBuffer) - 1, 0);
unsigned short lngLen;
char data[2048], sdfg[2];
memcpy(sdfg, strBuffer + 2, 2);
lngLen = GetWORD(sdfg);
memcpy(data, strBuffer, lngLen);
ParsePacket((unsigned char)data[1], data);
break;
[/code]
(i also can't declare and initalize in the same line because its inside a switch statement)
August 8, 2007, 4:34 PM
laurion
have you tried just creating a sample project to connect to these servers and see if you still receive error 6?

if not, then you can be sure it is an error in your code, and you may proceed from there.
August 8, 2007, 6:11 PM
BreW
I did, for some reason this barebones connection using WSAAsyncSelect() that i just made (which is what i used with my other connection) appears to be connecting but it's not, i'm completely clueless. Here's a packetlog.
[code]

C > S
HEADER:
45 00 00 30 2F 94 40 00 80 06 C8 9B C0 A8 01 2F  E..0/.@......../
D5 F8 6A C8 10 BE 17 E0 A6 39 98 52 00 00 00 00  ..j......9.R....
70 02 FF FF 19 5D 00 00                          p....]..       

DATA:
02 04 05 B4 01 01 04 02                          ........       
--------------------------------------------------------------------------------------

C > S
HEADER:
45 00 00 28 2F 97 40 00 80 06 C8 A0 C0 A8 01 2F  E..(/.@......../
D5 F8 6A C8 10 BE 17 E0 A6 39 98 53 F0 92 CC 60  ..j......9.S...`
50 10 FF FF 89 1D 00 00                          P.......       

DATA:
<null>
----------------------------------------------------------------------------------------

S > C
HEADER:
45 00 00 30 AA 0A 00 00 73 06 9B 25 D5 F8 6A C8  E..0....s..%..j.
C0 A8 01 2F 17 E0 10 BE F0 92 CC 5F A6 39 98 53  .../......._.9.S
70 12 FF FF 5C 61 00 00                          p...\a..       

DATA:
02 04 05 AC 01 01 04 02                          ........ 
----------------------------------------------------------------------------------------
C > S
HEADER:
45 00 00 29 2F 98 40 00 80 06 C8 9E C0 A8 01 2F  E..)/.@......../
D5 F8 6A C8 10 BE 17 E0 A6 39 98 53 F0 92 CC 60  ..j......9.S...`
50 18 FF FF 88 14 00 00                          P.......       

DATA:
01                                               .               
-----------------------------------------------------------------------------------------
S > C
HEADER:
45 00 00 28 AB 7B 00 00 73 06 99 BC D5 F8 6A C8  E..(.{..s.....j.
C0 A8 01 2F 17 E0 10 BE F0 92 CC 60 A6 39 98 54  .../.......`.9.T
50 10 FF FE 89 1D 00 00                          P.......       

DATA:
<null>
------------------------------------------------------------------------------------------
C > S
HEADER:
45 00 00 62 2F 99 40 00 80 06 C8 64 C0 A8 01 2F  E..b/.@....d.../
D5 F8 6A C8 10 BE 17 E0 A6 39 98 54 F0 92 CC 60  ..j......9.T...`
50 18 FF FF 12 BD 00 00                          P.......       

DATA:
FF 50 3A 00 00 00 00 00 36 38 58 49 52 41 54 53  .P:.....68XIRATS
D1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00 00 00 00 00 00 00 00 55 53 41 00 55 6E 69 74  ........USA.Unit
65 64 20 53 74 61 74 65 73 00                    ed States.     
--------------------------------------------------------------------------------------
S > C
HEADER:
45 00 00 28 AB 7E 00 00 73 06 99 B9 D5 F8 6A C8  E..(.~..s.....j.
C0 A8 01 2F 17 E0 10 BE F0 92 CC 60 09 B1 A6 02  .../.......`....
50 04 00 00 18 03 00 00                          P.......       

DATA:
<null>
--------------------------------------------------------------------------------------
S > C
HEADER:
45 00 00 28 AB 8C 00 00 73 06 99 AB D5 F8 6A C8  E..(....s.....j.
C0 A8 01 2F 17 E0 10 BE F0 92 CC 60 F0 92 CC 60  .../.......`...`
50 04 00 00 0A C3 00 00                          P.......       

DATA:
<null>
----------------------------------------------------------------------------------------
S > C
HEADER:
45 00 00 28 AC A6 00 00 73 06 98 91 D5 F8 6A C8  E..(....s.....j.
C0 A8 01 2F 17 E0 10 BE F0 92 CC 60 F0 92 CC 60  .../.......`...`
50 04 00 00 0A C3 00 00                          P.......       

DATA:
<null>
----------------------------------------------------------------------------------------
[/code]

EDIT
Oh wow, it seems when I don't send data on WM_CREATE (i guess that has something to do with it?) i now receive data, but recv() returns a len of -1. Great, what now.
August 9, 2007, 1:28 AM
Yoni
recv returning -1 means an error occurred. Call WSAGetLastError() to get the error number.
Chances are the error number is WSAEWOULDBLOCK; this means you have to wait before recv()'ing, until you get the window message that you registered with WSAAsyncSelect() and the FD_READ event is specified in the message.
August 9, 2007, 6:06 PM
BreW
sdfg! none of that matters anymore. I managed to get it working on my original project again (that error 6 only happens when i get an ipban) and i found out why my bot is crashing, its because of the packetbuffer i made.
this
[code]
void InsertByte(unsigned char data) {
memcpy(buffer + length, (const void *)data, 1);
length++;
}
[/code]
seems to be crashing it
what do i need to change so it works properly?
August 9, 2007, 7:12 PM
Myndfyr
[quote author=brew link=topic=16932.msg171471#msg171471 date=1186686721]
sdfg! none of that matters anymore. I managed to get it working on my original project again (that error 6 only happens when i get an ipban) and i found out why my bot is crashing, its because of the packetbuffer i made.
this
[code]
void InsertByte(unsigned char data) {
memcpy(buffer + length, (const void *)data, 1);
length++;
}
[/code]
seems to be crashing it
what do i need to change so it works properly?
[/quote]
Well, you're...... not using pointers right?
[code]
void InsertByte(unsigned char data)
{
  int retVal = memcpy(buffer + length, (const void*)&data, 1);
// it might even be more appropriate to use:
// (const void*)((const unsigned char*)&data)
// instead.  Ask someone more versed in C/C++.  Doing the cast ONLY to void* may introduce endianness problems
// across platforms.
  if (retVal > 0)
  {
    length++;
  }
}
[/code]
Also, I would guess you're storing buffer as a void*?  You should be storing it as a PBYTE or PUCHAR (please see this thread for nomenclature notes) and casting it to void* after doing the pointer arithmetic on it.  Here's why:

In pointer arithmetic, adding 1 to a pointer adds the size of the pointer to it.  So given:
[code]
void* pVoid1 = (void*)0x0411cd0c;
void* pVoid2 = pVoid1 + 1;
// pVoid2 now equals 0x0411cd10 on a 32-bit word machine, or 0x0411cd14 on a 64-bit word machine
[/code]
If you're using void* + int, you're occasionally going to be out of the bounds of your allocated memory if you're writing any substantial amount of data.
August 9, 2007, 8:13 PM
Kp
[quote author=MyndFyre[vL] link=topic=16932.msg171472#msg171472 date=1186690387]
[code]
void InsertByte(unsigned char data)
{
  int retVal = memcpy(buffer + length, (const void*)&data, 1);
// it might even be more appropriate to use:
// (const void*)((const unsigned char*)&data)
// instead.  Ask someone more versed in C/C++.  Doing the cast ONLY to void* may introduce endianness problems
// across platforms.
  if (retVal > 0)
  {
    length++;
  }
}
[/code][/quote]

There is no endianness problem here.  The function only moves a single byte!  Besides that, memcpy is always just a straight copy of bytes, so it will copy the bytes in the same order without regard to host endianness.  Of course, host endianness will affect how the result is interpreted when you read it through a pointer to a non-byte type.

[quote author=MyndFyre[vL] link=topic=16932.msg171472#msg171472 date=1186690387]
In pointer arithmetic, adding 1 to a pointer adds the size of the pointer to it.  So given:
[code]
void* pVoid1 = (void*)0x0411cd0c;
void* pVoid2 = pVoid1 + 1;
// pVoid2 now equals 0x0411cd10 on a 32-bit word machine, or 0x0411cd14 on a 64-bit word machine
[/code]
If you're using void* + int, you're occasionally going to be out of the bounds of your allocated memory if you're writing any substantial amount of data.
[/quote]

That does not seem very useful (or correct).  Adding 1 to a pointer adds the size of the referenced object to the resulting address.  As far as I know, arithmetic on a void pointer is not defined by the standard.  For convenience, gcc allows you to perform arithmetic on void pointers and treats the referenced data as having a size of 1.  You could fix your example by switching to a void **, so that you are adding the size of a void *.  That would also make your example well-defined on compilers other than gcc.
August 11, 2007, 3:31 AM
BreW
I was wondering when someone was going to point out that I'm moving a single byte, thanks Kp. (I just forgot to pass the address of the variable before casting it to a pointer heh..)
August 11, 2007, 4:04 AM

Search