kvarnerexpress 0 Report post Posted April 13, 2005 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.cUndefined first referencedsymbol in filebind /var/tmp//cc3taE90.osend /var/tmp//cc3taE90.oaccept /var/tmp//cc3taE90.olisten /var/tmp//cc3taE90.osocket /var/tmp//cc3taE90.old: fatal: Symbol referencing errors. No output written to a.outcollect2: ld returned 1 exit statusAny ideas anybody??? Share this post Link to post Share on other sites
dexter 0 Report post Posted April 15, 2005 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. Share this post Link to post Share on other sites
dexter 0 Report post Posted April 16, 2005 This is the part where I had problems compiling: 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 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. 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 tags for your code. Share this post Link to post Share on other sites
dexter 0 Report post Posted April 19, 2005 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. Share this post Link to post Share on other sites