• 0

Help with Socket programming in C (Linux)


Question

Hi guys!

I really need your help on this one... i can't figure it out, and since I'm new to socket programming... :(

I have to create a small webserver for a class I'm taking. So far so good - the webserver is working properly and delivering files to the clients. With only one problem...

If the client closes the connect while the server is sending the files, the server just stops executing. No error message is displayed, no segmentation fault, nothing... It just goes back to the command prompt when it should listen for other connections.

If the client never stops the connection while it is receiving the files, everything works fine.

Examples of it closing the connection would be pressing CTRL-C while wget is receiving a file, or hitting Refresh on the browser while it is loading a page/image.

My guess (and I dunno if it is an accurate one...) is that before sending anything to the client it must see if the socket is open.

But this wouldn't be 100% secure - what if the client closes the connection after the server sees if the connection is open, but before it actually sends anything through it?

The method I'm using to send stuff to the client is opening a socket and a file descriptor of that socket. I then send stuff using fputc and checking for EOF as a return value (to see if something went wrong).

It may not be the best method, but it was just to get started - I haven't optimized anything yet.

So.. any ideas or suggestions?...

Thanks!

3 answers to this question

Recommended Posts

  • 0

I'm not a UNIX programmer, but I would suggest that you first of all send your files with send(), the return value of it is helpful and designed for sockets, always go for the socket-specific functions, it's very easy, just read a tut or man. Also, in your loop, you could check the status of the socket, I don't know of a function for that in UNIX, but I'm pretty damn sure there is one, just google around a little bit.

Good luck.

  • 0

Whenever you try to write to a client that has closed the connection, you get a SIGPIPE signal. You can ignore it to prevent your proxy from crashing by putting calling this command:

signal(SIGPIPE, SIG_IGN);

That should ignore the SIGPIPE signal. Also, when writing to a client that has already been closed, it'll give you an error of EPIPE. If you run into this errno, just close the socket. I'm actually working on a sockets program myself (web proxy), what class are you doing this for?

  • 0

Thanks for the reply!

That was in fact the problem :-)

I did not get any error or system message, the program just quit, so I was completely clueless... I didn't even remember that a signal would be sent in this case... And since the default treatment for all signals is killing the program.. lol

This is for the Operating Systems class, i'm on the second year of Computer Engineering. Last year the project of this class was a proxy too... And what class are you writing the proxy for?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.