|
Hey TL.
So I have to write a chess server/client program for my university. I've already done the chess part, meaning the moves, check(mate)s, castling, etc etc. Now I need to do the rest, so I need to learn about sockets and stuff. Program has to be written in C and must compile on linux systems using gcc. I'm on windows, and I'm following Beej's guide to Network programming guide, because it's recommended by the guys @ uni. Okay, the guide's cool and awesome and stuff. Until I get cygwin and try to compile the first example and everything crashes and burns and I almost jump out of window (it doesn't compile).
+ Show Spoiler + /* ** showip.c -- show IP addresses for a host given on the command line */
#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; }
So uhh, I'm fucked unless you guys save me. I have grand two days left until the deadline, and I have NO clue what to do. Am I a dumbass and I just cannot compile properly (please says yes)? I just launch cygwin and "gcc showip.c -o showip" and it vomits a bunch of errors about incompatible pointer types.
What do I dooooo? Should I use a different guide? I've done some googling but all I found were quite techical and way over my knowledge level (pretty much useless). Right now I'm litteraly praying that I overlooked something incredibly obvious and I'm hoping someone can point it out. If not, maybe you guys can point me in the right direction or something, as otherwise I'm reall really screwed.
|
a have no idea about this, but it looks like not so much text, so just try to start from beginning again without checking ur existing code.
Sometimes you dont make the same mistake then and it works.
|
This isn't my code. It's the first example program of the guide.
|
If you can't read C compiler errors you're pretty much screwed anyway.
|
Gee, thanks for such an incredibly useful answer. I know nothing about socket programming. And when the very first example does not compile for me, I won't even pretend I know why and how should I go about fixing it.
Anyways, an "update" of sorts. it does compile just fine on linux. Seems like cygwin is playing tricks on me or something...
|
When you install cygwin, it asks you what libraries you want. You probably didn't get the correct socket libraries. Either that or you had a weird gcc.
Anyway, good luck writing your program. C sockets are a pain in ass.
|
You have to get socket libraries seperately? Man, that sounds dumb! On the other hand, I'm really hoping that you're right as this seems just like a dumbass overlook on my part.
/pray.
|
are you using POSIX socket libaries? if I remember correctly that is strictly unix stuff aka you can't do the same in Windows (unless you fetch the POSIX equivalent for Windows).
1.2. Platform and Compiler
The code contained within this document was compiled on a Linux PC using Gnu's gcc compiler. It should, however, build on just about any platform that uses gcc. Naturally, this doesn't apply if you're programming for Windows—see the section on Windows programming, below.
I doubt the author Beej actually tried to use cgywin as cgywin is only a subset of all POSIX libraries.
OP, you need to check all type errors then see if you can ssh into a unix machine (Most universities provide this) and compile your stuff from there.
Socket programming is very system specific, system calls vary greatly between different operating systems.
|
Canada9720 Posts
don't use cygwin as a dev environment man. either use an actual linux machine, or install a linux vm
|
Haduken: I did that and the program compiled just fine, as stated in my post above (or wherever).
CTStalker: I don't have the time for such stuff right now, I really don't. Instead of learning about sockets I'd have to learn about linux virtual machines? Maybe another time .
|
|
Canada9720 Posts
do you have access to a linux machine? maybe one at school? then you can use putty or something else to ssh to it
|
can you compile this example @ school computer? I have done this thing via virtual machine, especially compiling is much easier when using Unix based system.
|
CTStalker: I'm doing that right now - so I'm not screwed after all. It's somewhat annoying, but whatever. I'll try to solve those cygwin library/whatever problems later on if I feel like.
So yer, just to make clear - there was nothing wrong with the program, but instead it was a problem on my end with cygwin. Right now I'm compiling my stuff on uni's computers via putty, and it works just fine.
TL is awesome.
|
On October 31 2009 04:10 IMlemon wrote:Haduken: I did that and the program compiled just fine, as stated in my post above (or wherever). CTStalker: I don't have the time for such stuff right now, I really don't. Instead of learning about sockets I'd have to learn about linux virtual machines? Maybe another time .
Oops, Sorry I didn't read your posts.
OP don't blame yourself too hard, C/C++ compiler is a pain in the ass because every vendor/compiler implement their own version. It is one of the great weakness of this language.
I recommend you to stick with a particular framework such as Qt, avoid using low level system calls unless this is exactly what you want to do. Use wrappers when ever you can.
|
Just if anyone has the same problem with cygwin, here's what you need to do. Go to http://win6.jp/Cygwin/, download the newest package and extract to your cygwin folder.
Again, thanks for the help TL.
|
|
|
|