C++ Networking

Hi everyone!

I've been attempting to do some network programming in c++ but I'm having trouble getting started. So you know where I'm coming from, I'm fairly familiar with network programming with python where the only set up for network programming is putting 'include sockets' at the top of the file. It seems to be a little more complicated in c++.

The set up I have right now is MinGW installed on windows 7 64-bit. I also have Cygwin installed, but I'm not very sure how to use it.

The file I'm trying to compile is:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <winsock.h>
#include <winsock2.h>

int main()
  {
  
  struct sockaddr_in sa;
  
  inet_pton(AF_INET, "192.0.2.1", &(sa.sin_addr));
  
  return 0;
  
  }

using these parameters
 
g++ -c nettest.cpp  -lws2_32.a -lwsock32.a


and I get this error
 
nettest.cpp:11:49: error: 'inet_pton' was not declared in this scope


Clearly something isn't being setup correctly but I don't know what. I tried googling the error but I can't seem to find anyone with a similar problem.

Any help would be greatly appreciated.
 
Last edited on
That could be it but I can't seem to include it. It says it can't find inet.h.
 
Last edited on
I think you're right. I found the inet.h file in the cygwin directory but it can't seem to include the files it needs correctly.

I can include inet.h fine. But inet.h needs to include in.h which is in the netinet directory. I've tried moving the inet.h file to many different directories but it still can't find the in.h file.

I also tried editing the inet.h file to make it include the in.h file using absolute referencing but then I get this error which is similar except for the last part:
1
2
3
4
In file included from C:\cygwin\usr\include\arpa\inet.h:14:0,
                 from nettest.cpp:3:
C:/cygwin/usr/include/netinet/in.h:14:23: fatal error: cygwin/in.h: No such file
 or directory

The difference there is that it says "cygwin/in.h" but that's not where the file is. It's in C:/cygwin/usr/include/netinet/ and that's where I said to include to.


Edit: I realized that the error is coming from the in.h file. I looked in the in.h file and found "#ifndef _CYGWIN_IN_H" to be the source of the error but I have no idea how to fix that.
Last edited on
It seems to be a little more complicated in c++.

It is, at least until boost.asio becomes officially a part of C++. Until then, the most portable way to do networking in C++ is to #include <boost/asio.hpp> -- see http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/tutorial.html
Don't include both winsock.h and winsock2.h, just use winsock2.h. And don't link to wsock32.lib, use ws2_32.lib instead.

On windows, you need to include WS2tcpip.h for inet_pton.

The header files used for networking data structures and helper functions are not standardised and vary depending on operating system/variant.
Last edited on
@kbw I still get the same error( inet_pton undeclared). Like you said, it isn't standardized and I definitely don't know enough to figure what it is at the moment. I wanted to just use winsock but for now I might try some other library. Thanks anyway.

@Cubbi Thanks, I'll look into that.
inet_pton requires windows vista, according to MSDN documentation:
http://msdn.microsoft.com/en-us/library/windows/desktop/cc805844%28v=vs.85%29.aspx

Set first WINVER to at least 0x0600 and get the latest SDK to compile.
Topic archived. No new replies allowed.