Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <winsock2.h>
#include <iostream>

using namespace std;

// -----------------------------------------------------------------------------------
// StartServerListening() - will return us a socket that is bound to the
// port we specify, and is listening for connections if all operations succeeded,
// and a -1 if startup failed.
int StartServerListening(unsigned short port);

// -----------------------------------------------------------------------------------
// EndServer() - will take the socket we specify and shutdown the
// network utilities we started with StartServerListening()
// Note: In order to function properly, the socket passed in MUST be the
// socket created by the StartServerListening() function
void EndServer(int socket);


int main()
{
    cout << "Server: Part 4a\n";
    cout << "Server: Celestialkey's Tutorial\n\n";

    int serverSocket; // Used to listen

    serverSocket = StartServerListening(7700);

    
    if (serverSocket == -1)
    {
        cout << "Oh god... Everything went wrong!! Run!\n";
        return 1;
    }

    int clientSocket; // Socket to accept
    clientSocket = accept(serverSocket, 0, 0);


    if (clientSocket == SOCKET_ERROR)
    {
        cout << "Server: Heh, Taught him a lesson on acceptence...\n";
    }


    int nBytes;
    

    #define MAX_MESSAGE_SIZE 4096 // We need a big enough buffer for our maximum message size.

    char buffer[MAX_MESSAGE_SIZE];

    unsigned long messageSize; // We need a variable for our size.


    nBytes = recv(clientSocket, (char*)&messageSize, sizeof(messageSize), 0);

    if (nBytes == SOCKET_ERROR)
    {
        cout << "Server: I failed you master! Nothing was recieved!\n";
    }

    messageSize = ntohl(messageSize);    // Remember to fix our byte ordering since this is a number

    nBytes = recv(clientSocket, buffer, messageSize, 0);

    if (nBytes == SOCKET_ERROR)
    {
        cout << "Server: Recv Failed!\n";
    }

    
    buffer[messageSize] = ''; // Place the terminator in the string 'messageSize'

    cout << "Server: Someone said : " << buffer << endl;


    closesocket(clientSocket);

    EndServer(serverSocket);

    cout << "Server: Press any key to turn me off baby ...\n";
    getchar();
    
    return 0;
}


// -----------------------------------------------------------------------------------
// StartServerListening() - a function to startup winsock, and open a socket for listening

int StartServerListening(unsigned short port)
{
    int error;

    WSAData wsaData;

    if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
        cout << "Server: WinSock is being a bitch, smack it a few times eh?\n";
        return -1;
    }

    int mySocket = socket(AF_INET, SOCK_STREAM, 0);

    if (mySocket == SOCKET_ERROR)
    {
        cout << "Server: Something went terribly wrong, the socket wont open!\n";
        return -1;
    }

    struct sockaddr_in server;

    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = INADDR_ANY;

    if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
    {
        cout << "Server: It wont let me bind correctly.\n";
        closesocket(mySocket);
        return -1;
    }

    if (listen(mySocket, 5) == SOCKET_ERROR)
    {
        cout << "Server: Damnit.. im deaf... Listening Failed!\n";
        closesocket(mySocket);
        return -1;

    }

    cout << "Server: Im up and running boss!\n";

    return mySocket;
}


// -----------------------------------------------------------------------------------
// EndServer() - a function to shutdown a socket and clean up winsock

void EndServer(int socket)
{
    closesocket(socket);

    WSACleanup();

    cout << "Server: Remember the Alamo!\n";
}


Will give me error error C2137: empty character constant
If i change '' to "" the error will be: error C2440: '=' : cannot convert from 'const char [1]' to 'char'
If i change "" or '' to '\0' i get: 1>main.obj : error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__ntohl@4 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__recv@16 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__accept@12 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__listen@8 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__bind@12 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function "void __cdecl EndServer(int)" (?EndServer@@YAXH@Z)
1>c:\users\james.stream\documents\visual studio 2010\Projects\TheServer\Debug\TheServer.exe : fatal error LNK1120: 10 unresolved externals



Cheers

This is not my code. Im using it to learn about chat boxes.
Posted
Comments
Jochen Arndt 11-Mar-14 7:50am    
Regarding the unresolved external errors:

Your program is not linked against the winsock library.

When using MFC, you should include afxsock.h (e.g. in stdafx.h).

When not using MFC, add this line to one of your source files:
#pragma comment(lib, "wsock32.lib")

Assuming you mean this line:
C++
buffer[messageSize] = ''; // Place the terminator in the string 'messageSize'

then it should be:
C++
buffer[messageSize] = '\0'; // Place the terminator in the string 'messageSize'
But check that is a zero, not an uppercase "O"
If that doesn't work (and it should) then try
C++
buffer[messageSize] = (char) 0; // Place the terminator in the string 'messageSize'
Which should have the same effect.
 
Share this answer
 
I get the following if i do

buffer[messageSize] = (char) 0;
or
buffer[messageSize] = '\0'

VB
1>main.obj : error LNK2019: unresolved external symbol __imp__accept@12 referenced in function "void __cdecl AcceptThread(int *)" (?AcceptThread@@YAXPAH@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__ntohl@4 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__WSAGetLastError@0 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__recv@16 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__select@20 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__listen@8 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__bind@12 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "int __cdecl StartServerListening(unsigned short)" (?StartServerListening@@YAHG@Z)
1>main.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function "void __cdecl EndServer(int)" (?EndServer@@YAXH@Z)



Do/Should i have a SDK installed?
is this my problem...?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900