Network

In last week I asked for a network book and others suggest me
http://beej.us/guide/bgnet/
I downloaded the book and read it but in the first code i have some errors
i use visual C++ 2008
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: showip hostname\n");
return 1;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", argv[1]);
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res); // free the linked list
return 0;
}

I had an error that computer doesn't find sys/socket.h and netdb.h and arpa/inet.h
closed account (z05DSL3A)
Did you read the section 1.5. Note for Windows Programmers in the book?

Edit:
Have a look at Network Programming Using the Winsock API to get you going on the windows side, you should be able to return to beej.
http://tombell.org.uk/papers/winsock-api.pdf
Last edited on
Please explain more
closed account (z05DSL3A)
Beej's Guide to Network Programming is aimed at the Linux platform, section 1.5 includes the text "This is what you'll have to do (unless you install Cygwin!): first, ignore pretty much all of the system header files I mention in here. All you need to include is: #include <winsock.h>", so it should come as no surprise that the headers in the code that you posted are not on windows.

The link I posted should be enough to get you familiar with setting up winsock then you can go back to Beej's guide to learn more.
How i can't understand from the link to how add #include<winsock.h> to the first program
closed account (S6k9GNh0)
Okay, Windows crappy API is retarded. For a while, Windows had the same defines as the BSD Sockets API which is what is truly explained in the tutorial. Then one day Windows programming staff said, "Okay guys we don't suck enough. Let's make it difficult for cross-platform programmers to have convenience with our shitty API." They saw the shitty API was bad. Windows created extensions and functions that aren't part of the BSD Sockets API and are required for the sockets to work correctly. In order to truly code for cross-platform sockets, a library that handles WinSock's differences is now required for Microsoft Windows compatibility or you can make defines and directives to basically force it to be compatible. Not only do you need to include the winsock2.h header and link to the winsock library, you need to compensate for Microsoft's crappy API.
Last edited on
Ok
But i downloaded cygwin how do i use it
Help
if u like to learn the basics and understand how all this works, dont use some external libs...
What do you mean how do i use it cygwin
Last edited on
Mr Grey Wolf please answere what shoul i do with cygwin
closed account (z05DSL3A)
I don't use cygwin, so I can't help there.
so who can help me with cygwin
no one use cygwin
so who can help me with cygwin


Google
Topic archived. No new replies allowed.