Results 1 to 8 of 8

Thread: problem with winsock tutorials

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746

    problem with winsock tutorials

    this is code i downloaded of the internet and tried:

    #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();
    }


    but i get all these errors when i try it.

    --------------------Configuration: system - Win32 Debug--------------------
    Compiling...
    source.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\system\source.cpp(85) : warning C4715: 'main' : not all control paths return a value
    Linking...
    source.obj : error LNK2001: unresolved external symbol _WSACleanup@0
    source.obj : error LNK2001: unresolved external symbol _closesocket@4
    source.obj : error LNK2001: unresolved external symbol _accept@12
    source.obj : error LNK2001: unresolved external symbol _listen@8
    source.obj : error LNK2001: unresolved external symbol _bind@12
    source.obj : error LNK2001: unresolved external symbol _htons@4
    source.obj : error LNK2001: unresolved external symbol _socket@12
    source.obj : error LNK2001: unresolved external symbol _WSAStartup@8
    Debug/system.exe : fatal error LNK1120: 8 unresolved externals
    Error executing link.exe.

    system.exe - 9 error(s), 1 warning(s)

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    You need to include the winsock library file in project. I think it is called "WSOCK32.lib" or "WS2_32.lib".
    Baaaaaaaaah

  3. #3
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    You need to include the winsock library file in your project. I think it is called "WSOCK32.lib" or "WS2_32.lib".
    Baaaaaaaaah

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    how do i do that i went to the add components thing under the project menu but it isnt showing any .lib files.

  5. #5
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    GO to Project->Settings, and then in the linker option, type in the name "WSOCK32.lib" yourself.


    See the attached image for details.
    Baaaaaaaaah

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    THANK. thats COOL!

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    ok now that i can listen and connect on ports how can i send and recieve data?

  8. #8
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Originally posted by flamewavetech
    ok now that i can listen and connect on ports how can i send and recieve data?
    You need a tutorial to learn how to send and recieve data,etc.
    The best tutorial I have ever found on the Net about Winsock programming in C++ is here:
    http://www.inf.pucrs.br/~derose/file...ktutorial.html

    It has a section that describes how to send and recieve data using two different types on winsock connections.
    Baaaaaaaaah

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