Results 1 to 7 of 7

Thread: winsock problem

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    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 (&sa0sizeof (sockaddr_in));    //clear the address (mem space)
        
    gethostname (own_namesizeof (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_INETSOCK_STREAM0);    //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 *) &sasizeof (sockaddr_in)) == SOCKET_ERROR)    {
            
    closesocket (ret_socket);    //failed to do so, so get rid of the socket
                
    return INVALID_SOCKET;
        }

        
    listen (ret_socket3);    //make the socket listen
            
    return ret_socket;
    }    
    //Establish

    int main (int argcchar *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_socketNULLNULL);
            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.

  2. #2

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    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.

  3. #3

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    i changed the
    PHP Code:
    char *own_name 
    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.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    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.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    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
  •  



Click Here to Expand Forum to Full Width