|
|
|
|
![]() ![]() |
Apr 13 2005, 10:58 AM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 407 Joined: 13-December 04 Member No.: 2,696 |
I'm trying to write a simple C server program using sockets, that will interract with a Java client - I've never used BSD sockets before, so I'm a bit lost. I've written the program how, from what I've seen, I'm supposed to, but it won't compile. I've attached the code below:
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <netinet/in.h> int main() { int sock = 0; struct sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(9001); sock = socket(PF_INET, SOCK_STREAM, 0); if (sock == -1) printf("Error creating socket"); else { if (bind(sock, (struct sockaddr *)& address, sizeof(address))) printf("error binding socket"); else { printf("socket bound successfully"); listen(sock,5); struct sockaddr_in address2; int addrlen = sizeof(struct sockaddr_in); int newSocket = accept(sock, (struct sockaddr *)&address2, &addrlen); if (newSocket < 0) printf("Error accepting incoming connection\n"); else { char* msg = "Test message"; printf("About to send the message"); send(sock,msg,strlen(msg),0); printf("I think i just sent the message"); } } } close(sock); return 0; } but when I attempt to compile I'm getting the errors below: $ gcc servtest.c Undefined first referenced symbol in file bind /var/tmp//cc3taE90.o send /var/tmp//cc3taE90.o accept /var/tmp//cc3taE90.o listen /var/tmp//cc3taE90.o socket /var/tmp//cc3taE90.o ld: fatal: Symbol referencing errors. No output written to a.out collect2: ld returned 1 exit status Any ideas anybody??? |
|
|
|
Apr 15 2005, 12:33 AM
Post
#2
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 142 Joined: 24-December 04 From: Queensland, Australia Member No.: 2,902 |
I had to move some of the variables out of your inner if/else statements to declare them at the beginning to get it to compile. It seems odd. The code seems correct (I've done server building before, so you've got all the headers, and all the function calls should work).
Maybe instead of nesting the if/else just call function, test for error, exit(1) upon errors, otherwise continue. And you could always try using perror() to display error messages. I've always found it to be more useful than printing what I think has gone wrong. Anyway, tell us how you go. |
|
|
|
Apr 16 2005, 06:21 AM
Post
#3
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 142 Joined: 24-December 04 From: Queensland, Australia Member No.: 2,902 |
This is the part where I had problems compiling:
CODE struct sockaddr_in address2; int addrlen = sizeof(struct sockaddr_in); int newSocket = accept(sock, (struct sockaddr *)&address2, &addrlen); For some reason, it didn't like the variables being declared at that point in the code. Instead, I had CODE int main() { ... // initial variable declarations at beginning int addrlen = sizeof(struct sockaddr_in); struct sockaddr_in address2; int newSocket; ... // some code... accept(sock, (struct sockaddr *)&address2, &addrlen); ... // some more code return(0); } Another thing you should always do (at least, it's recommended, anyway), is to zero out the rest of the sockaddr_in struct. CODE struct sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(9001); memset(&(address.sin_zero), '\0', 8); Last of all, if your server is shutdown, and you try to start it again, you'll often have problems binding again. And lastly, remember to use the [CODE] tags for your code. |
|
|
|
Apr 19 2005, 12:51 PM
Post
#4
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 142 Joined: 24-December 04 From: Queensland, Australia Member No.: 2,902 |
Another thing. I must've been on drugs when I said don't nest the ifs. If it's formatted right, it does look much nicer. I've just been modifying my server to be an object, and I've broken socket() and check, setsockopt() and check, bind() and check, etc.. into small functions.
Only small functions, but it get rid of all the nasty fail checking in the main calling function (actually, they're all placed into another startup function, too). The point was, nest away if you feel like it, it doesn't really matter either way. One last thing... have you had any results? There's been so many questions, but no actual response to see if anything we've said actually helped. I'm sure I'm not the only one who wants to get a bit of feedback. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 12th October 2008 - 10:08 PM |