Jul 26, 2008

Server Socket

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Server Socket

kvarnerexpress
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???

 

 

 


Reply

dexter
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.

Reply

dexter
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.

 

 

 


Reply

dexter
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.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:


Searching Video's for server, socket
advertisement



Server Socket



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE