Thread: Winsock Problem

  1. #1
    Banned
    Join Date
    May 2004
    Posts
    55

    Arrow Winsock Problem

    When I compile this program

    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    #define NETWORK_ERROR -1
    #define NETWORK_OK     0
    
    void ReportError(int, const char *);
    
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
    	WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    
    	sockVersion = MAKEWORD(1, 1);			// We'd like Winsock version 1.1
    
    
    	// We begin by initializing Winsock
    	WSAStartup(sockVersion, &wsaData);
    
    
    	// Next, create the listening socket
    	SOCKET listeningSocket;
    
    	listeningSocket = socket(AF_INET,		// Go over TCP/IP
    			         SOCK_STREAM,   	// This is a stream-oriented socket
    				 IPPROTO_TCP);		// Use TCP rather than UDP
    
    	if (listeningSocket == INVALID_SOCKET) {
    		nret = WSAGetLastError();		// Get a more detailed error
    		ReportError(nret, "socket()");		// Report the error with our custom function
    
    		WSACleanup();				// Shutdown Winsock
    		return NETWORK_ERROR;			// Return an error value
    	}
    
    
    	// Use a SOCKADDR_IN struct to fill in address information
    	SOCKADDR_IN serverInfo;
    
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_addr.s_addr = INADDR_ANY;	// Since this socket is listening for connections,
    							// any local address will do
    	serverInfo.sin_port = htons(8888);		// Convert integer 8888 to network-byte order
    							// and insert into the port field
    
    
    	// Bind the socket to our local server address
    	nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
    
    	if (nret == SOCKET_ERROR) {
    		nret = WSAGetLastError();
    		ReportError(nret, "bind()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	// Make the socket listen
    	nret = listen(listeningSocket, 10);		// Up to 10 connections may wait at any
    							// one time to be accept()'ed
    
    	if (nret == SOCKET_ERROR) {
    		nret = WSAGetLastError();
    		ReportError(nret, "listen()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	// Wait for a client
    	SOCKET theClient;
    
    	theClient = accept(listeningSocket,
    			   NULL,			// Address of a sockaddr structure (see explanation below)
    			   NULL);			// Address of a variable containing size of sockaddr struct
    
    	if (theClient == INVALID_SOCKET) {
    		nret = WSAGetLastError();
    		ReportError(nret, "accept()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	// Send and receive from the client, and finally,
    	closesocket(theClient);
    	closesocket(listeningSocket);
    
    
    	// Shutdown Winsock
    	WSACleanup();
    	return NETWORK_OK;
    }
    
    
    void ReportError(int errorCode, const char *whichFunc) {
       char errorMsg[92];					// Declare a buffer to hold
    							// the generated error message
       
       ZeroMemory(errorMsg, 92);				// Automatically NULL-terminate the string
    
       // The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
       sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
    
       MessageBox(NULL, errorMsg, "socketIndication", MB_OK);
    }
    I get these errors:
    [Linker error] undefined reference to `WSACleanup@0' (5 of them)
    [Linker error] undefined reference to `socket@12'
    [Linker error] undefined reference to `WSAStartup@8'
    [Linker error] undefined reference to `WSAGetLastError@0' (5 of them)
    [Linker error] undefined reference to `htons@4'
    [Linker error] undefined reference to `bind@12'
    [Linker error] undefined reference to `listen@8'
    [Linker error] undefined reference to `accept@12' (2 of them)
    [Linker error] undefined reference to `closesocket@4' (2 of them)

    They says that I shall link ws2_32.lib but were is it? and how I do it with dev-c++?

    Grateful for answers
    Last edited by Noxir; 07-18-2004 at 07:20 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Roughly speaking
    Project settings -> Linker -> additional libraries

    If the library is not in one of the standard places, then you may need additional library directories as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    May 2004
    Posts
    55
    Where is the library for winsock then?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's what you said isn't it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> and how I do it with dev-c++?

    I believe it's libwsock32.a for that compiler (in the \lib directory - of course).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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. 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
  3. Winsock packet problem
    By Rare177 in forum Windows Programming
    Replies: 7
    Last Post: 10-05-2004, 04:53 PM
  4. WinSock Problem
    By loobian in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2002, 11:25 AM
  5. Small Winsock problem...
    By SyntaxBubble in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2002, 10:09 AM