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.
it made up of two process that one of them is writing to a local socket and the other reading the socket and prints the contents.
but it does not work.
Could you please specify what exactly "does not work"?
With a quick glance at your code, I'm curious as to why you chose to insert the accept() call within the common area of the server/client code? This call should only be made by the server. Also, the sleep() call probably serves no purpose, and should be removed.
Consider something like:
Code:
...
if (fork() == 0)
{
/* server */
int clientfd = accept(...); /* this will block, unless using non-blocking socket */
if (clientfd > 0)
{
/* handle client */
...
}
}
else
{
/* client runs here */
...
int result = connect(...);
...
}
From what I can tell, the issue you are having is not so much with socket programming, but with using fork(). Perhaps you should attempt to develop the client and server functionality in separate apps. Once you have that working, then consider going with the multi-process approach.
P.S. write() returns the number of bytes written. If you ask this function to write N-bytes, where N > 0, and a result of 0 is returned, I would say the call "failed", although a -1 is really the determining result for an error. In your code, you seem to think otherwise.
Last edited by dwhitney67; 12-13-2011 at 06:04 AM.
I added an include for string.h in the code above (because it is needed), compiled it (using gcc -Wall -pedantic), and then ran it.
As expected, the Unix (Local) socket was created in the current directory with the name of "recipe". When I launched a separate client (one of my own) to connect to that socket, my client received the following messages (w/o a newline): Server Response: spam spam ,spam,spam,
My client didn't bother to check for additional messages, but I'm sure if it did, it would have received the hard-coded message associated with the variable "line2".
As for the server, it only outputted the following uninformative message: 1
Anyhow, from what I could tell, the server application "works". You still have an issue in writerecipe(); as I mentioned earlier (and it seems that you ignored this advice), write() returns the number of bytes written. If you merely check for equality to zero, and then print a message indicating "ok", then you are misleading yourself. How about something like this:
Code:
size_t line1_len = strlen(line1);
int bytes_written = 0;
while (bytes_written != line1_len)
{
int result = write(fd, line1 + bytes_written, line1_len - bytes_written);
if (result < 0)
{
if (errno == EWOULDBLOCK || errno == EINTR)
continue;
perror("write failed");
break;
}
bytes_written += result;
}
You are not required to write a null byte.
Last edited by dwhitney67; 12-13-2011 at 06:22 AM.
thanks for reply I changed what you said about write and I found something that when I start the program for the first time that socket file recipe does not exist it is working !!! but with strange message 1 that you said (this is my first question why) but the second question is when I start it for the second time or more which recipe file exist it does not work and accept system call failed and nok message start printing again.Any way I found that if the recipe file exist before it does not work but if I remove it and let the program to create it again it works fine except it prints 1 message that I don't know where it comes from?
my code is:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.