|
-
Mar 18th, 2002, 03:46 PM
#1
Thread Starter
Hyperactive Member
winsock problem
why doesn't this work:
PHP Code:
//skeleton server
#include <winsock.h>
#include <iostream.h>
#define PORTNUM 50000 //port number to be used
void DoSomething (SOCKET); //use this to do anything with socket
SOCKET Establish (unsigned short PortNum)
{
char *own_name ;
SOCKET ret_socket; //socket to be returned
sockaddr_in sa; //internet socket address
hostent *hp; //host info
memset (&sa, 0, sizeof (sockaddr_in)); //clear the address (mem space)
gethostname (own_name, sizeof (own_name)); //get local host name - our own
hp = gethostbyname (own_name); //retrieve address info
if (hp == NULL) //hopefully we do exist, and we wont get that error
return INVALID_SOCKET;
sa.sin_family = hp->h_addrtype; //our host address
sa.sin_port = htons (PortNum); //our port number
ret_socket = socket (AF_INET, SOCK_STREAM, 0); //create the socket
if (ret_socket == INVALID_SOCKET) //failed to create socket
return INVALID_SOCKET;
//now, we bind the socket to the internet address
if (bind (ret_socket, (sockaddr *) &sa, sizeof (sockaddr_in)) == SOCKET_ERROR) {
closesocket (ret_socket); //failed to do so, so get rid of the socket
return INVALID_SOCKET;
}
listen (ret_socket, 3); //make the socket listen
return ret_socket;
} //Establish
int main (int argc, char *argv[])
{
SOCKET my_socket; //main socket
if ((my_socket = Establish (PORTNUM)) == INVALID_SOCKET) { //plug in the phone
cout << "Establish: Failed to install socket" << endl;
return 0;
}
for ( ; ; ) { //loop for phone calls
SOCKET new_socket = accept (my_socket, NULL, NULL);
if (my_socket == INVALID_SOCKET) {
cout << "Error waiting for connection" << endl;
return 0;
}
DoSomething (new_socket);
closesocket (new_socket);
}
return 0;
}
void DoSomething (SOCKET sock)
{
//do thing with socket
}
Amon Ra
The Power of Learning.
-
Mar 18th, 2002, 04:17 PM
#2
Thread Starter
Hyperactive Member
i just found out i forgot to initialize Winsock with WSAStartup (..), so i dont get the Init. failed error..but after a bit of running, the program crashes..
Amon Ra
The Power of Learning.
-
Mar 18th, 2002, 04:39 PM
#3
Thread Starter
Hyperactive Member
i changed the
to
PHP Code:
char own_name[256];
that's the way it was in the tutorial (w/ the 256)
why does that work and not the one before??
Amon Ra
The Power of Learning.
-
Mar 18th, 2002, 05:00 PM
#4
Did you forget the pointer 1x1 ?
char* ptr;
is a wild pointer, it points to a random location (most likely 0xCCCCCCCC), so you can't use it.
char buf[512];
on the other hand is an array of 512 chars, it already has memory allocated.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 18th, 2002, 05:10 PM
#5
Thread Starter
Hyperactive Member
ohh ok..that explains why i would get similar errors in other programs..thanks
why do you call it pointer 1x1?
Amon Ra
The Power of Learning.
-
Mar 19th, 2002, 10:00 AM
#6
I don't know if that exists in English. The 1x1 ("Ein mal Eins" = "One times One") means "the absolute basics", because it's the first thing you learn in school when multiplying. To know that a pointer declared like char* p; points at random memory is part of the absolute basics of pointers.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 19th, 2002, 11:09 AM
#7
Thread Starter
Hyperactive Member
ooops.. ok..i wont forget again
Amon Ra
The Power of Learning.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|