WinSock programming

This is a discussion on WinSock programming within the Windows Programming forums, part of the Platform Specific Boards category; After fighting heavily with BCC5.5 i turned to Dev-C++. My progs were compiling well with code (this is an example ...

  1. #1
    Registered User oktech's Avatar
    Join Date
    Dec 2001
    Posts
    3

    WinSock programming

    After fighting heavily with BCC5.5 i turned to Dev-C++.

    My progs were compiling well with code (this is an example from Jonnie's site):

    #include <winsock.h> // winsock.h always needs to be included
    #include <stdio.h>


    int main(int argc, char** argv) {
    WORD version = MAKEWORD(1,1);
    WSADATA wsaData;
    int nRet;


    //
    // First, we start up Winsock
    //
    WSAStartup(version, &wsaData);


    //
    // Next, create the socket itself
    //
    SOCKET listeningSocket;

    listeningSocket = socket(AF_INET, // Go over TCP/IP
    SOCK_STREAM, // Socket type
    IPPROTO_TCP); // Protocol
    if (listeningSocket == INVALID_SOCKET) {
    printf("Error at socket()");
    return 0;
    }


    //
    // Use SOCKADDR_IN to fill in address information
    //
    SOCKADDR_IN saServer;

    saServer.sin_family = AF_INET;
    saServer.sin_addr.s_addr = INADDR_ANY; // Since this is a server, any address will do
    saServer.sin_port = htons(8888); // Convert int 8888 to a value for the port field


    //
    // Bind the socket to our local server address
    //
    nRet = bind(listeningSocket, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
    if (nRet == SOCKET_ERROR) {
    printf("Error at bind()");
    return 0;
    }


    //
    // Make the socket listen
    //
    nRet = listen(listeningSocket, 10); // 10 is the number of clients that can be queued
    if (nRet == SOCKET_ERROR) {
    printf("Error at listen()");
    return 0;
    }


    //
    // Wait for a client
    //
    SOCKET theClient;

    theClient = accept(listeningSocket,
    NULL, // Wait for a specific address to connect; here there is none
    NULL);
    if (theClient == INVALID_SOCKET) {
    printf("Error at accept()");
    return 0;
    }


    // Send/receive from the client, and finally:

    closesocket(theClient);
    closesocket(listeningSocket);


    //
    // Shutdown Winsock
    //
    WSACleanup();
    }


    now it does compile BUT:

    C:\DOCUME~1\ADMINI~1.OKT\LOCALS~1\Temp\ccS4aaaa.o( .text+0x6c):test4.cpp: undefined reference to `WSAStartup@8'
    C:\DOCUME~1\ADMINI~1.OKT\LOCALS~1\Temp\ccS4aaaa.o( .text+0x7d):test4.cpp: undefined reference to `socket@12'
    C:\DOCUME~1\ADMINI~1.OKT\LOCALS~1\Temp\ccS4aaaa.o( .text+0xcc):test4.cpp: undefined reference to `htons@4'
    C:\DOCUME~1\ADMINI~1.OKT\LOCALS~1\Temp\ccS4aaaa.o( .text+0xf0):test4.cpp: undefined reference to `bind@12'
    C:\DOCUME~1\ADMINI~1.OKT\LOCALS~1\Temp\ccS4aaaa.o( .text+0x12d):test4.cpp: undefined reference to `listen@8'
    C:\DOCUME~1\ADMINI~1.OKT\LOCALS~1\Temp\ccS4aaaa.o( .text+0x16f):test4.cpp: undefined reference to `accept@12'
    C:\DOCUME~1\ADMINI~1.OKT\LOCALS~1\Temp\ccS4aaaa.o( .text+0x1ab):test4.cpp: undefined reference to `closesocket@4'
    C:\DOCUME~1\ADMINI~1.OKT\LOCALS~1\Temp\ccS4aaaa.o( .text+0x1bd):test4.cpp: undefined reference to `closesocket@4'
    C:\DOCUME~1\ADMINI~1.OKT\LOCALS~1\Temp\ccS4aaaa.o( .text+0x1c5):test4.cpp: undefined reference to `WSACleanup@0'

    Happens!!!!!

    Help me pls

    Why cant Win compilers be like *NIX compilers, my stuff is seamless on *NIX, why not on Win????

    oktech

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I tried to compile the code with MSVC++ and got the same errors. The author of the code may have forgotten to include a header, library or a section of code to it, but I seriously doubt that he would have published the code without checking it first. Go back to where you found it, make sure its all right and if you still have problems, contact the author.

    -Chris

  3. #3
    Registered User oktech's Avatar
    Join Date
    Dec 2001
    Posts
    3

    IM CONFUSED

    I checked the code, it is an exact copy :S

    All i can say is that i took the code knowing that it worked because someone else used it!

    Any ideas people?

    Craig

  4. #4
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb I know what it is...

    I use Dev-C++, too. Click the "Project Options" button torwards the upper-right. in the first edit box, type -lwsock32. Then, click OK and it should work.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    What about MSVC++? I tried with winsock, but I couldn't even get started with all of the errors it gave me for the smallest things. Do I need to edit my resorces or something?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I think that you might have to add the .lib to the project or something just like he did with Dev-C++

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    The error messages you are getting would seem to indicate the functions are not declared, which would lead me to suggest you are using the wrong header file. The naming of headers is somewhat compiler specific. In VC++ for example, the header you would use is winsock2.h and the library you link to is ws2_32.lib. Try these.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Link with the Winsock library (as the tutorial states at the end of the third paragraph), and you should be fine.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

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, 05:00 PM
  3. Winsock - Where do i start?
    By Brain Cell in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-14-2005, 01:39 PM
  4. 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
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 01:45 AM

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