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 was wondering if anybody can help me out with this. I'm not sure what I am doing wrong. I'm just trying to communicate from 1 unix machine to another using a socket. Here is my code.
Your treating your IP address (e.g. 192.x.x.x) as an int value via the sscanf(). Sure, the sscanf() will succeed; it will scan the first int value it sees, and return that as a result. Thus the IP address that is yielded is 192, not the value you expected.
Hence this statement is incorrect (applies to both the client and server):
Code:
addr.sin_addr.s_addr = htonl(ip);
Although not considered the ideal way to set the IP address, the following is the easiest:
Code:
addr.sin_addr = inet_addr(argv[1]);
Note how I skip around the usage of sscanf(), and just pass the raw input to inet_addr(). If you are concerned about this, then employ sscanf() to ensure that you have a valid IP address.
Ideally you should learn about using getaddrinfo(). I've used it, but I must admit I'm not an expert on the subject. Perhaps you can learn more about this function by reading Beej's Guide to Network Programming.
P.S. Your server is opening AF_INET socket, not an AF_UNIX one. Thus there's no need to unlink() anything at the end of your main(). A socket file is only created when dealing with AF_UNIX.
P.S. #2 In time (ie with experience) you will learn that it is unwise to assume that an entire TCP packet will be received all at once. After all, you are streaming data. For small packets you may not have an issue, but as the packets grow larger, they could very well arrive "in pieces".
Last edited by dwhitney67; 11-11-2012 at 05:22 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.