1
#pragma comment(lib,"Ws2_32.lib")
#include<sdkddkver.h>
#include<conio.h>
#include<stdio.h>
#include<iostream>
#include<WinSock2.h>
#include<windows.h>
#define SCK_VERSION2 0x0202

using namespace std;

int main()
{
    long SUCCESSFUL;
    WSAData WinSockData;
    WORD DLLVERSION;

    DLLVERSION=MAKEWORD(2,1);
    SUCCESSFUL=WSAStartup(DLLVERSION,&WinSockData);
    SOCKADDR_IN ADDRESS;
    int AddressSize=sizeof(ADDRESS);

    SOCKET sock_LISTEN;
    SOCKET sock_CONNECTION;
    sock_CONNECTION=socket(AF_INET,SOCK_STREAM,NULL);

    ADDRESS.sin_addr.s_addr=inet_addr("127.0.0.1");
    ADDRESS.sin_family=AF_INET;
    ADDRESS.sin_port=htons(444);

    sock_LISTEN=socket(AF_INET,SOCK_STREAM,NULL);

    bind(sock_LISTEN,(SOCKADDR *)&ADDRESS,sizeof(ADDRESS));

    listen(sock_LISTEN,SOMAXCONN);

    for(;;)
    {
        cout<<"\n\tSERVER:Waiting for incoming connection...";
        if(sock_CONNECTION=accept(sock_LISTEN,(SOCKADDR *)&ADDRESS,&AddressSize));
        {
            cout<<"\n\tA connection was found!"<<endl;
            SUCCESSFUL=send(sock_CONNECTION,"Welcome! You are now connected to the Server!",46,NULL);
        }
    }
 }

Whenever I try building this code for server I get following errors:

enter image description here

I'm totally new. I'm using code blocks. I've been searching for solution from last 4 days but I'm not understanding anything. Please help

unixia
  • 4,102
  • 1
  • 19
  • 23

1 Answers1

1

If you are using CodeBlocks, then you use MinGW that goes with it (I guess, according to low experience level).

This feature works at Visual Studio compilers:

#pragma comment(lib,"Ws2_32.lib") 

With MinGW such #pragma will not work.

Instead if it you have to open "Project" -> "Options", then choose your project at left tree (or "Debug"/"Release", if you want that change work just there), and then open tab "link settings".

Press "Add" and find library name like "libws2_32.a" in your MinGW/lib/ directory.

After try rebuild. Hope that will helps.

Arkady
  • 2,084
  • 3
  • 27
  • 48
  • 1
    The name you add should just be `ws2_32`. The `lib` and `.a` are taken care of. – chris Jun 24 '14 at 13:56
  • Not exactly this way. For just "ws2_32" he will also have to set in tab "Search directories" in subtab "Linker" path to "MinGW\lib", then "ws2_32" will works. – Arkady Jun 24 '14 at 14:04
  • I don't ever recall having to change the default search directories for libraries in there. – chris Jun 24 '14 at 14:06
  • I had to. To avoid this, as I remember, it needs to set "Settings -> Compiler -> search directories -> Linker". Maybe they are set automatically if CodeBlocks goes with MinGW, I don't know. I used it just with another MinGW compilation, so I had to define that. – Arkady Jun 24 '14 at 14:27
  • Hey @Arkady! Now my code compiles fine but I'm getting a lot of warnings like warning: passing NULL to non-pointer argument 3 of 'SOCKET socket(int, int, int)' What is causing this? The lectures I took help of showed exactly the same code and it did run fine there. Is it something to do with Code::Blocks? – unixia Jun 24 '14 at 16:39
  • 1
    @timidgeek that means that you put "NULL" where it waits "int" (as you can see from function declaration). If you use modern compiler, and you do, I guess, you have to change "NULL" to "0" everywhere, where it uses as zero (for example, in your socket creation function) and "NULL" to "nullptr" where it has to be pointer that points to nowere. – Arkady Jun 24 '14 at 17:56
  • @Arkady thanks! One more question, how do I make these (client and server) files? As in if I make them under the same project, it shows error because of two main functions. If I make them as different projects then only one runs at a time. What do I do? – unixia Jun 25 '14 at 12:30
  • It have to be 2 projects, and you have to run them from different instances of CodeBlocks, or run one of them in CodeBlocks debug mode, second - just as simple application. – Arkady Jun 25 '14 at 12:31
  • @Arkady if I run it as a simple application, debug mode gets disabled and vice-versa. I don't know how to create multiple instances. – unixia Jun 25 '14 at 17:59