![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2004
Location: Ontario, Canada.
Posts: 38
Rep Power: 0
![]() |
Winsock Create//Connect Problem.
Alright ive done some winsock work before but im getting an error and I cannot fix it. Im using Visual C++ 6. i got the winsock library added to settings, and i am using winsock header properly here is some code.
#include "winsock.h"
SOCKET xSocks[30];
SOCKADDR_IN BAS;
int CallConnect(const char *server, int threadm, int port) {
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
WSAStartup(version, &wsaData);
LPHOSTENT lpHostEntry;
xSocks[threadm] = socket(AF_INET,SOCK_STREAM,0); //
unsigned int buf;
buf = xSocks[thread];
lpHostEntry = gethostbyname(server);
BAS.sin_family = AF_INET;
BAS.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
BAS.sin_port = htons(port);
buf = connect(xSocks[threadm], (LPSOCKADDR)&BAS, sizeof(struct sockaddr));
if (buf == SOCKET_ERROR) {
return 0;
}
else {
return 1;
}
}Alright it compiles fine and all but doesnt work. and creates a error fault when running. Any help is appreciated. Thanks. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 884
Rep Power: 4
![]() |
Put some trace writes in so you can narrow down the error. You should also check your return values. For example, gethostbyname will return NULL if there is an error. Your code dereferences the return value without checking, so that may cause a crash.
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
what is this file, #include "winsock.h", shouldn't that be #include <winsock2.h>?
|
|
|
|
|
|
#4 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
That should be #include <winsock.h> and he is using winsock version 1.1 so why try to include a winsock2 header?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
whoops, I didn't check that, I just saw that he included a different header, and stopped there. If checking the return values of your function calls, like the_dark suggested, doesn't work, you should put some cout statements after each function call to determine which function is creating the error, and go from there. Also, like Polyphemus stated, please provide us with more information.
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
I Windows there is a function called WSAGetLastError() which could help you in some circumstances.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|