Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 2nd, 2005, 1:58 PM   #1
HackeZ
Programmer
 
Join Date: Dec 2004
Location: Ontario, Canada.
Posts: 38
Rep Power: 0 HackeZ is on a distinguished road
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.
HackeZ is offline   Reply With Quote
Old Dec 2nd, 2005, 7:25 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 884
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Dec 3rd, 2005, 1:27 AM   #3
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
what is this file, #include "winsock.h", shouldn't that be #include <winsock2.h>?
nindoja is offline   Reply With Quote
Old Dec 3rd, 2005, 5:47 AM   #4
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by HackeZ
Alright it compiles fine and all but doesnt work. and creates a error fault when running. Any help is appreciated. Thanks.
what does the error say?
Polyphemus_ is offline   Reply With Quote
Old Dec 3rd, 2005, 11:38 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Dec 3rd, 2005, 3:14 PM   #6
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
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.
nindoja is offline   Reply With Quote
Old Dec 3rd, 2005, 6:26 PM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC