Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Server Socket
kvarnerexpress
post 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???
Go to the top of the page
 
+Quote Post
dexter
post 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.
Go to the top of the page
 
+Quote Post
dexter
post 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.
Go to the top of the page
 
+Quote Post
dexter
post 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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Private Server Ro .(162)
  2. How To Make A Counter Strike 1.6 Dedicated Server(18)
  3. Recommended Server-side Programming Language(7)
  4. Private Servers(16)
  5. The Other Hdd Does Not Appear On My Server(2)
  6. Dohwow(2)
  7. Help With Compiling My Server(0)
  8. Runescape Private Server(76)
  9. Who Thinks Trap 17 Is Th Best Server?(9)
  10. Private Server(1)
  11. Connection Error 800a0e7a On Win Server 2003 X64(1)
  12. Wow Private Server(3)
  13. how to make a pc server?(30)
  14. Tutorial Build Your Own Cs Server 1.6 Steam(16)
  15. A Web Server On Lan(9)
  1. Tutorial On How To Compile Your Own Mangos World Of Warcraft Private Server.(3)
  2. How To Make Your Own Counter Strike Source Dedicated Server!(38)
  3. How to setup DNS server in Linux Slackware(5)
  4. Print Server Help(0)
  5. Rsps Hosting?(0)
  6. I Have Install Windows 2003 Server(5)
  7. How To Install Sql Server Express In Package Deployment With Different Instance Name(0)
  8. Dedicated Server Package(1)
  9. The Only Reason I Choose Ms Sql Server Rather Than Mysql(0)
  10. Runescape 2 Private Server Guide: Part 2(22)
  11. Runescape 2 Private Server: Code/guide 1(11)
  12. [help]create P Server(2)
  13. The New And Very Good Free Host Server(13)
  14. Gmail: 502 Server Error(22)
  15. How Do I Make My Own Private Online Server Please Help Me Out.thank You For Your Time.(26)


 



- Lo-Fi Version Time is now: 12th October 2008 - 10:08 PM