0

I have a problem with the following code.Please help me out

#include<stdio.h>
#include<iostream>
#include<conio.h>
#include<winsock.h>
#include<winsock2.h>
#include<fstream>
#include<windows.h>
#define WIN32_MEAN_AND_LEAN
#include <sys/types.h>
#include <streambuf>
#include<string.h>

using namespace std;


int main()
{
    int length;
    char *buffer;
    const int iReqWinsockVer = 2;
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(iReqWinsockVer,0),&wsaData)==0)
    {
        if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
            {
            SOCKET hSocket;
            hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            S:if (hSocket==INVALID_SOCKET)
            {
                static int i=1;
                if(i!=8)
                {
                    i++;
                    goto S;
                }
                cout<<"SOCKET CREATION FAILED";
            }
            else
            {
                sockaddr_in sockAddr;
                sockAddr.sin_family = AF_INET;
                sockAddr.sin_port = htons(115);
                sockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
                B:if (bind(hSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr))!=0)
                {
                     goto B;
                }
                else
                {
                    L:if (listen(hSocket, SOMAXCONN)!=0)
                    {
                                        static int j=0;
                                        if(j!=8)
                                        {
                                                j++;
                                                goto L;
                                        }
                                        cout<<"Could not listen to the incoming connection";
                    }
                    else
                    {
                        sockaddr_in remoteAddr;
                        int iRemoteAddrLen;
                        SOCKET hRemoteSocket;
                        iRemoteAddrLen = sizeof(remoteAddr);
                        hRemoteSocket = accept(hSocket, (sockaddr*)&remoteAddr, &iRemoteAddrLen);
                        if (hRemoteSocket==INVALID_SOCKET)
                        {
                                                          cout<<"Connection acception failed";
                        }
                        else
                        {
                            ofstream out;
                            out.open("test.txt",ios::binary);
                            recv(hRemoteSocket,buffer,length, 0);
                            out.write(buffer,length);
                        }
                    }
                }
            }
            }
            else
            {
                cout<<"Version Incompatibility";
            }
            if (WSACleanup()!=0)
            {
                            cout<<"Cleanup Failed";
            }
    }
    else
    {
    }
}

On execution I get the following errors.
D:\Temp\test_server.o:test_server.cpp|| undefined reference to WSAStartup@8'| D:\Temp\test_server.o:test_server.cpp|| undefined reference tosocket@12'|
D:\Temp\test_server.o:test_server.cpp|| undefined reference to htons@4'| D:\Temp\test_server.o:test_server.cpp|| undefined reference tobind@12'|
D:\Temp\test_server.o:test_server.cpp|| undefined reference to listen@8'| D:\Temp\test_server.o:test_server.cpp|| undefined reference toaccept@12'|
D:\Temp\test_server.o:test_server.cpp|| undefined reference to recv@16'| D:\Temp\test_server.o:test_server.cpp|| undefined reference toWSACleanup@0'|
||=== Build finished: 8 errors, 0 warnings ===|

How to rectify the following errors.I use Code::Blocks, Windows7 platform.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by triumphost
0

Link to libwsock32.a and any necessary libraries.

In Visual studio you can link by doing #pragma comment(lib, "libwsock32.a");

Edited by triumphost

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.