14 Replies - 2233 Views - Last Post: 22 November 2012 - 06:43 PM Rate Topic: -----

#1 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

C++ - Socket Server

Posted 18 November 2012 - 02:49 PM

Hello!
Fran here with a little problem.

Recently, I've been working on a chat project.
This project consists of two people chatting with eachother (Self-Explanitory).

I found this server code online, just as a startup sort of file to test my socket server.
#include <iostream>
#include "Included/winsock2.h"

int main() {
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);
    
    WSAStartup(Version, &wsa);
    
    SOCKET Listen = socket(AF_INET, SOCK_STREAM, NULL);
    SOCKET Connect = socket(AF_INET, SOCK_STREAM, NULL);
    
    SOCKADDR_IN Server;
    
    Server.sin_addr.s_addr = inet_addr("127.0.0.1");
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);
    
    bind(Listen, (SOCKADDR*)&Server, sizeof(server));
    
    listen(Listen, 1);
    
    int size = sizeof(Server);
    
    std::cout<<"Listening...";
    
    for(;;)/> {
            if (Connect = accept(Listen, (SOCKADDR*)&Server, &size)) {
                        std::cout<<"\nConnection was reached";
                        break;
            }
    }
    WSACleanup();
    std::cin.get();
    return 0;
}

I used this straight from a socket server tutorial (Via Youtube).
His worked perfectly fine! But mine, does not...
What am I doing wrong with this???
I'm getting this error (Won't even open):
In function 'console_main':
[Linker error] undefined reference to 'SDL_main'
Id returned 1 exit status
[Build Error] [Server-TEST.exe] Error 1


Not sure what i'm doing wrong,
linws2_32.a is linked, as well.

I'm using Dev-C++ for this compilation.
(Also, Please don't tell me I should get a new compiler! I know Dev-C++ isn't a great source for compiling, but it's easiest for me).

Thanks guys,
Fran (Anonymous Idon User)

Typo!:
libws2_32.a is included.

Thanks again,
Fran (Anonymous Idon User)

Is This A Good Question/Topic? 0
  • +

Replies To: C++ - Socket Server

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: C++ - Socket Server

Posted 18 November 2012 - 02:56 PM

Well, maybe adding -lSDL to your compiler/linker options could solve it.
added: Are you sure this is the code that gives you this error?


By the way, Dev-C++ is not a compiler - it's an IDE. The original is quite old now, and since it's not updated it uses preinstalled ooold version of MinGW. That's the main reason people nag other people about it.

Use whatever IDE you want, we don't really care. However, using outdated compiler may make your learning experience more confusing and more limiting.

This post has been edited by Xupicor: 18 November 2012 - 02:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Socket Server

Posted 18 November 2012 - 08:19 PM

View PostXupicor, on 18 November 2012 - 02:56 PM, said:

Well, maybe adding -lSDL to your compiler/linker options could solve it.
added: Are you sure this is the code that gives you this error?


By the way, Dev-C++ is not a compiler - it's an IDE. The original is quite old now, and since it's not updated it uses preinstalled ooold version of MinGW. That's the main reason people nag other people about it.

Use whatever IDE you want, we don't really care. However, using outdated compiler may make your learning experience more confusing and more limiting.


Yes, This is the code that gives me an error.
Not sure if it has anything to do with it,
But the extension is '.h'

Also, What kind of limitations might this include?
I tried to use Microsoft Visual C++,
But it's too confusing for me to even start on.
I'm sure my code would still work fine, But I have no idea where to even start with all of it's
completely different methods of use.

Is their anything that seems wrong in my code?
The guys who made the code used Code::Blocks,
But I'm not sure how reliable that is.

Thanks again,
Fran
Was This Post Helpful? 0
  • +
  • -

#4 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: C++ - Socket Server

Posted 19 November 2012 - 03:16 AM

More confusing in that old, non-standard code might work for you, but not for most other people using modern compilers, and more limiting in that standard, modern code may not work for you, while it works for most other people using more modern tools.

On topic - what is in "Included/winsock2.h"? Does it by any chance include SDL headers? And if so, old versions maybe? Because I don't think SDL needs SDL_main for some time now. You're into the debugging world now, use your Ctrl+F and search console_main, post the code, same with SDL_main. Try to find some version information.
Maybe some people using SDL more frequently can tell you something then.


Oh, about tools, if I may. Visual Studio isn't all that confusing, actually it's pretty nice. Code::Blocks will get you a fresh MinGW probably, so it might be a good idea to install it (but you'll probably need to uninstall Dev-C++ to avoid two separate MinGW installations).
One IDE that I'd recommend is NetBeans, but then you'd need to install MinGW yourself (try Nuwen's distro, see my signature).

Oh, and it would actually be beneficial to at least try compiling your programs using command line. You know, to type g++ -pedantic -Wall main.cpp -o main.exe. It's not scary or hard, and you'd get to know tools like make. You should at least vaguely know what happens when your IDE compiles a program. It's not magic. ;)
See g++ --version and post it here.

This post has been edited by Xupicor: 19 November 2012 - 03:22 AM

Was This Post Helpful? 1
  • +
  • -

#5 LarcaCode  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 21
  • Joined: 30-July 12

Re: C++ - Socket Server

Posted 19 November 2012 - 03:48 AM

View PostBlaze13, on 18 November 2012 - 02:49 PM, said:

...
I found this server code online, just as a startup sort of file to test my socket server.
...


If you are completely new to network programming its just fine to learn the "basics" - how it works and so on.
For more comfortable and faster programming I would advice to use a library ( like Boost ), that takes care of this.

This post has been edited by LarcaCode: 19 November 2012 - 03:49 AM

Was This Post Helpful? 0
  • +
  • -

#6 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Socket Server

Posted 19 November 2012 - 07:12 AM

View PostXupicor, on 19 November 2012 - 03:16 AM, said:

More confusing in that old, non-standard code might work for you, but not for most other people using modern compilers, and more limiting in that standard, modern code may not work for you, while it works for most other people using more modern tools.

On topic - what is in "Included/winsock2.h"? Does it by any chance include SDL headers? And if so, old versions maybe? Because I don't think SDL needs SDL_main for some time now. You're into the debugging world now, use your Ctrl+F and search console_main, post the code, same with SDL_main. Try to find some version information.
Maybe some people using SDL more frequently can tell you something then.


Oh, about tools, if I may. Visual Studio isn't all that confusing, actually it's pretty nice. Code::Blocks will get you a fresh MinGW probably, so it might be a good idea to install it (but you'll probably need to uninstall Dev-C++ to avoid two separate MinGW installations).
One IDE that I'd recommend is NetBeans, but then you'd need to install MinGW yourself (try Nuwen's distro, see my signature).

Oh, and it would actually be beneficial to at least try compiling your programs using command line. You know, to type g++ -pedantic -Wall main.cpp -o main.exe. It's not scary or hard, and you'd get to know tools like make. You should at least vaguely know what happens when your IDE compiles a program. It's not magic. ;)/>
See g++ --version and post it here.


winsock2 is for the sockets, I don't have anything added for SDL, and I'm not sure why it's needed =o
But I'll definitely debug it.

I could try Visual Studios, it does have everything I need, as well.

I'm not currently home, so I'll try it when I get back, and reply my results!
Thanks

View PostLarcaCode, on 19 November 2012 - 03:48 AM, said:

View PostBlaze13, on 18 November 2012 - 02:49 PM, said:

...
I found this server code online, just as a startup sort of file to test my socket server.
...


If you are completely new to network programming its just fine to learn the "basics" - how it works and so on.
For more comfortable and faster programming I would advice to use a library ( like Boost ), that takes care of this.


No worries, I'm not new to programming,
Just to socket programming in specific.

Other than that, I've been coding for a few years now.
Was This Post Helpful? 0
  • +
  • -

#7 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Socket Server

Posted 19 November 2012 - 04:28 PM

I downloaded Visual Studios C++ Compiler,
But i'm having difficulty finding the application :o

What's the actual name for the compiler or whatever called???
To edit the codes. (IDE I think it's called?)
Was This Post Helpful? 0
  • +
  • -

#8 LarcaCode  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 21
  • Joined: 30-July 12

Re: C++ - Socket Server

Posted 20 November 2012 - 04:22 AM

This should help you.
Search for "Visual C++ 2010 Express"
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,225
  • Joined: 20-March 10

Re: C++ - Socket Server

Posted 20 November 2012 - 05:23 AM

Hmm,

For someone who has been programming a couple of years your skills
are somewhat lacking.

Your code..
These should be your headers
#include <iostream>
#include <winsock2.h>



nothing else

line 27 and 28


for(; ; ) /> {
if (Connect = accept(Listen, (SOCKADDR*)&Server, &size)) {




line 19

bind(Listen, (SOCKADDR*)&Server, sizeof(server));


lines 10 and 11


SOCKET Listen = socket(AF_INET, SOCK_STREAM, NULL);
SOCKET Connect = socket(AF_INET, SOCK_STREAM, NULL);


What makes you think NULL is acceptable here ?
your compiler should be at least warning you..
a more appropriate value would be IPPROTO_IGMP

This is the trouble with DEV C++ its a badly broken IDE and no longer maintained try

Visual Studio Express with the Windows 7 sdk and the platform toolset set to Windows7 Sdk

or

Code::Blocks with MinGW an excellent IDE for Beginners its not broken and is updated regularly

or

Pelles C if you dont want to use C++ and prefer to program in C.

Netbeans like Visual Studio can be difficult for beginners

Eclipse with the MinGW installation again can be difficult for beginners.

My recommendation try Code::Blocks.


Your corrected code link against ws2_32 dont as you say include it...

#include <iostream>
#include <winsock2.h>

int main() {
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &wsa);

    SOCKET Listen = socket(AF_INET, SOCK_STREAM, IPPROTO_IGMP);
    SOCKET Connect = socket(AF_INET, SOCK_STREAM, IPPROTO_IGMP);

    SOCKADDR_IN Server;

    Server.sin_addr.s_addr = inet_addr("127.0.0.1");
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);

    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));

    listen(Listen, 1);

    int size = sizeof(Server);

    std::cout<<"Listening...";

    for(;; ) {
            if (Connect == accept(Listen, (SOCKADDR*)&Server, &size)) {
                        std::cout<<"\nConnection was reached";
                        break;
            }
    }
    WSACleanup();
    std::cin.get();
    return 0;
}




As for the SDL thing you probably have SDL set up in your project include search directories with DEV C++.

Best Wishes

Snoopy.

This post has been edited by snoopy11: 20 November 2012 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#10 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: C++ - Socket Server

Posted 20 November 2012 - 05:41 AM

@up
/> thing is not his fault but DICs. It's a known bug. Well, it was reported. ;) From what can be seen it's the BBCode parser fault - it adds extra /> somewhere, and it's broken in that it doesn't skip [code][/code] tags contents. (That's why lower-case b when followed by closing bracket
b[no space here])
is changed to upper-case B, see:
B)/>
even in code tags. Which is a bummer.

This post has been edited by Xupicor: 20 November 2012 - 05:43 AM

Was This Post Helpful? 0
  • +
  • -

#11 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,225
  • Joined: 20-March 10

Re: C++ - Socket Server

Posted 20 November 2012 - 05:47 AM

View PostXupicor, on 20 November 2012 - 12:41 PM, said:

@up
/> thing is not his fault but DICs. It's a known bug. Well, it was reported. ;)/> From what can be seen it's the BBCode parser fault - it adds extra /> somewhere, and it's broken in that it doesn't skip [code][/code] tags contents. (That's why lower-case b when followed by closing bracket
b[no space here])
is changed to upper-case B, see:
B)/>/>
even in code tags. Which is a bummer.


Yeah true,
doesn't excuse the rest of it though...

I was just being a little too thorough perhaps...

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#12 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Socket Server

Posted 20 November 2012 - 07:08 AM

View Postsnoopy11, on 20 November 2012 - 05:23 AM, said:

Hmm,

For someone who has been programming a couple of years your skills
are somewhat lacking.

Your code..
These should be your headers
#include <iostream>
#include <winsock2.h>



nothing else

line 27 and 28


for(; ; ) /> {
if (Connect = accept(Listen, (SOCKADDR*)&Server, &size)) {




line 19

bind(Listen, (SOCKADDR*)&Server, sizeof(server));


lines 10 and 11


SOCKET Listen = socket(AF_INET, SOCK_STREAM, NULL);
SOCKET Connect = socket(AF_INET, SOCK_STREAM, NULL);


What makes you think NULL is acceptable here ?
your compiler should be at least warning you..
a more appropriate value would be IPPROTO_IGMP

This is the trouble with DEV C++ its a badly broken IDE and no longer maintained try

Visual Studio Express with the Windows 7 sdk and the platform toolset set to Windows7 Sdk

or

Code::Blocks with MinGW an excellent IDE for Beginners its not broken and is updated regularly

or

Pelles C if you dont want to use C++ and prefer to program in C.

Netbeans like Visual Studio can be difficult for beginners

Eclipse with the MinGW installation again can be difficult for beginners.

My recommendation try Code::Blocks.


Your corrected code link against ws2_32 dont as you say include it...

#include <iostream>
#include <winsock2.h>

int main() {
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &wsa);

    SOCKET Listen = socket(AF_INET, SOCK_STREAM, IPPROTO_IGMP);
    SOCKET Connect = socket(AF_INET, SOCK_STREAM, IPPROTO_IGMP);

    SOCKADDR_IN Server;

    Server.sin_addr.s_addr = inet_addr("127.0.0.1");
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);

    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));

    listen(Listen, 1);

    int size = sizeof(Server);

    std::cout<<"Listening...";

    for(;; ) {
            if (Connect == accept(Listen, (SOCKADDR*)&Server, &size)) {
                        std::cout<<"\nConnection was reached";
                        break;
            }
    }
    WSACleanup();
    std::cin.get();
    return 0;
}




As for the SDL thing you probably have SDL set up in your project include search directories with DEV C++.

Best Wishes

Snoopy.


I've been programming in C++ for years, yes. But this is my first time with anything Socket related.
Again, I got this code online. I didn't make it myself.

Thanks for helping out, as well. But I haven't tried the code yet, just wanted to make sure I said thanks.
I'll tell you my results when I get home!
Was This Post Helpful? 0
  • +
  • -

#13 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Socket Server

Posted 20 November 2012 - 04:36 PM

I changed up my code, I'm getting the same errors, snoopy.
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,225
  • Joined: 20-March 10

Re: C++ - Socket Server

Posted 21 November 2012 - 04:20 AM

Well,

As I said if you are getting the SDL thing

You must have it in your search directories for you compiler and debugger.

remove SDL from the search directories and clean your project.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#15 Blaze13  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 11-November 12

Re: C++ - Socket Server

Posted 22 November 2012 - 06:43 PM

View Postsnoopy11, on 21 November 2012 - 04:20 AM, said:

Well,

As I said if you are getting the SDL thing

You must have it in your search directories for you compiler and debugger.

remove SDL from the search directories and clean your project.

Snoopy.


I took the same code and put it in Visual Studioes C++ and it worked.
Won't be going back to Dev-C++ =o lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1