ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I came across a library a little while ago which provided some useful features for a project I am working on --- it would save a few days work, and a lot of bug testing, so you can see why I want to use it --- but it relies on serial communication, whereas I want to so everything via ethernet (because the serial port is already tied up). I would imagine adding ethernet support to these functions would be relatively simple, but I am a little stuck here.
I searched this forum the other day, and came up with libpcap, which sounded nice, however, as I get deeper into it, I get the feeling it is not quite what I need. While libpcap has some nice features for reading packets, what I want is simple receive and transmit of data between two systems: libpcap seems to have more than I need for receive, and does not seem to support transmit.
Obviously there would be something already out there (likely a heck of a lot simpler than I am thinking too), but I am a little stuck now as to what I need to do. If I could be directed to any HOWTOs or other guides which explain how to use ethernet in a C program, I would be really grateful.
as you have found, libpcap is pretty much just for sniffing packets on the wire. I am not sure how low-level you want to get, but I would start with "man socket" which will describe how to open a file-like descriptor to read or write data over the wire.
I have made use of this document to extend the library I had found, and everything works fine, except for one point I had not thought about before it happened. If enough clients connect to the server, further connections will be refused --- which makes sense --- but what about if all the currently connected clients have used close(fd)? How do I know when the server should close() the socket returned by accept(), and therefore be prepared to accept() future connections? Is there a simple way to know that the client has disconnected, or should there be some protocol whereby the client says "bye" before they call close()? I have thought about time outs, which would not be a bad idea to add, but there should surely be a cleaner option for when the client leaves like this.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.