winsock help needed

This is a discussion on winsock help needed within the Networking/Device Communication forums, part of the General Programming Boards category; Ok, so I'm trying to learn sockets and I went to MS's site and just copied some client/server code. I ...

  1. #1
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429

    winsock help needed

    Ok, so I'm trying to learn sockets and I went to MS's site and just copied some client/server code. I tried to compile it and I got no errors. However, when I went to link it, I got all kinds of external symbol errors, and they all refer to functions which I thought were in winsock2.h and I've included that file. I'm using MSVC++ 6 SP5. Can anyone tell me what I'm doing wrong?

    Code:
    #include <stdio.h>
    #include "winsock2.h"
    
    int main() {
    
        // Initialize Winsock.
        WSADATA wsaData;
        int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
        if ( iResult != NO_ERROR )
            printf("Error at WSAStartup()\n");
    
        // Create a socket.
        SOCKET m_socket;
        m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    
        if ( m_socket == INVALID_SOCKET ) {
            printf( "Error at socket(): %ld\n", WSAGetLastError() );
            WSACleanup();
            return(0);
        }
    
        // Connect to a server.
        sockaddr_in clientService;
    
        clientService.sin_family = AF_INET;
        clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
        clientService.sin_port = htons( 27015 );
    
        if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
            printf( "Failed to connect.\n" );
            WSACleanup();
            return(0);
        }
    
        // Send and receive data.
        int bytesSent;
        int bytesRecv = SOCKET_ERROR;
        char sendbuf[32] = "Client: Sending data.";
        char recvbuf[32] = "";
    
        bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
        printf( "Bytes Sent: %ld\n", bytesSent );
    
        while( bytesRecv == SOCKET_ERROR ) {
            bytesRecv = recv( m_socket, recvbuf, 32, 0 );
            if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
                printf( "Connection Closed.\n");
                break;
            }
            if (bytesRecv < 0)
                return(0);
            printf( "Bytes Recv: %ld\n", bytesRecv );
        }
    
        return(0);
    }
    Sidenote: They had it posted with void main()!!! I had to change that.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I got all kinds of external symbol errors,
    Best post them.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    1. Look up the missing function at msdn.
    2. Find the line, down the bottom:
    Library: Use Kernel32.lib.
    3. Add the specified library to your link.
    4. If you still get link errors, or you don't have the specified library, panic.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Library: Use Kernel32.lib.
    Err, wasn't it WS2_32.lib?

    **edit
    Oh I get it, an example
    #define Hunter2 SLOW
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    use wsock32.lib
    and winsock.h

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>winsock.h
    Only if you're planning on marketing to people with a Windows version below 95. 98+ includes Winsock2 already, and there's a patch/update for 95. Use winsock2.h (as I see you already did). And similarly for wsock32.lib, I imagine that's the old Winsock library; use ws2_32.lib.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Since you're using vc you can easily fix this problem by having this at the top of the file:

    Code:
    #pragma comment(lib, "ws2_32.lib") //no semi-colon
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 07:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 11:11 AM
  4. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21