Thread: winsock help needed

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

    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?

    #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() );
        // Connect to a server.
        sockaddr_in clientService;
        clientService.sin_family = AF_INET;
        clientService.sin_addr.s_addr = inet_addr( "" );
        clientService.sin_port = htons( 27015 );
        if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
            printf( "Failed to connect.\n" );
        // 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");
            if (bytesRecv < 0)
            printf( "Bytes Recv: %ld\n", bytesRecv );
    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
    >>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
    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
    >>Library: Use Kernel32.lib.
    Err, wasn't it WS2_32.lib?

    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
    use wsock32.lib
    and winsock.h

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    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
    Since you're using vc you can easily fix this problem by having this at the top of the file:

    #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, 08: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, 12:11 PM
  4. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM