LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2006, 09:39 AM   #1
harvest
LQ Newbie
 
Registered: Sep 2006
Posts: 8

Rep: Reputation: 0
How to make it work?


/*
* serv.c - server creating AF_UNIX socket used by cli.c
* compile with gcc -g -Wall serv.c -o serv
*/

#include <sys/types.h>
#ifdef __linux__
#include <linux/socket.h>
#else
#include <sys/socket.h>
#endif
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/file.h>
#include <errno.h>
#include <unistd.h>

#define USOCK "./usock"
#define BACKLOG 5

static sig_atomic_t sigreceived = 0;
static int s = 0, c = 0;

static void add_flags(int fd, int flags)
{
int val;

if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
perror("add_flags(): fcntl(F_GETFL)");
exit(1);
}
val |= flags;
if ((val = fcntl(fd, F_SETFL, val)) < 0) {
perror("add_flags(): fcntl(F_SETFL)");
exit(1);
}
}

static void mysig(int sig)
{
char buf[200];
int ret;

fprintf(stderr, "Signal %d received #%d\n", sig, ++sigreceived);
if (!c) {
if((c = accept(s, NULL, NULL)) < 0) {
perror("accept()");
exit(1);
}
fprintf(stderr, "accept() OK\n");
memset(buf, 0, 200);
/* ret = recv(c, buf, 200, 0); */
ret = recv(c, buf, 200, MSG_WAITALL);
if (ret < 0) {
perror("read()");
return;
}
fprintf(stderr, "received %d bytes <%s>\n", ret, buf);
return;
} else { /* this branch is never taken on Linux */
memset(buf, 0, 200);
ret = read(c, buf, 200);
if (ret < 0) {
perror("read()");
return;
}
fprintf(stderr, "received %d bytes <%s>\n", ret, buf);
}
return ;
}

int main(int argc, char *argv[])
{
int len;
struct sockaddr_un sa;
struct sigaction act;
pid_t pid = getpid();

act.sa_handler = mysig;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
if (sigaction(SIGPOLL, &act, NULL) < 0) {
perror("sigaction(SIGPOLL)");
exit(1);
}

(void)unlink(USOCK);
s = socket(PF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
perror("socket(PF_UNIX, SOCK_STREAM)");
exit(1);
}

if (fcntl(s, F_SETOWN, pid) < 0) {
perror("fcntl(s, F_SETOWN)");
exit(1);
}

add_flags(s, FASYNC);

len = sizeof(sa);
sa.sun_family = AF_UNIX;
strcpy(sa.sun_path, USOCK);
if (bind(s, (struct sockaddr *)&sa, len) < 0) {
perror("bind()");
exit(1);
}
fprintf(stderr, "bind() OK\n");
if (listen(s, BACKLOG) < 0) {
perror("listen()");
exit(1);
}
fprintf(stderr, "listen() OK\n");

while (1)
pause();

/* not reached */
return 0;
}

/*
* cli.c - client connecting to AF_UNIX socket created by serv.c
* compile with gcc -g -Wall cli.c -o cli
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define USOCK "./usock"

int main(int argc, char *argv[])
{
int fd, len;
struct sockaddr_un sa;
const char *buf1 = "Hello from client";
const int len1 = strlen(buf1);
const char *buf2 = "Goodbye from client";
const int len2 = strlen(buf2);
const char *buf3 = "Cheerio from client";
const int len3 = strlen(buf2);

fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
perror("cli: socket(PF_UNIX,SOCK_STREAM)");
exit(1);
}
sa.sun_family = AF_UNIX;
strcpy(sa.sun_path, USOCK);
len = sizeof(sa);
if (connect(fd, (struct sockaddr *)&sa, len) < 0) {
perror("cli: connect()");
exit(1);
}
sleep(1);
if (send(fd, buf1, len1, 0) != len1) {
perror("cli: send(buf1)");
exit(1);
}
sleep(1);
if (send(fd, buf2, len2, 0) != len2) {
perror("cli: send(buf2)");
exit(1);
}
sleep(1);
if (send(fd, buf3, len3, 0) != len3) {
perror("cli: send(buf3)");
exit(1);
}
return 0;
}
 
Old 09-22-2006, 10:35 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if you expect help, you could at least bother to ask a real question and put a code dump in some sort of context...
 
Old 09-22-2006, 11:58 AM   #3
harvest
LQ Newbie
 
Registered: Sep 2006
Posts: 8

Original Poster
Rep: Reputation: 0
server part works only one time...
 
Old 09-22-2006, 12:30 PM   #4
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,191

Rep: Reputation: 49
lol
give a detailed essay on whats wrong here
 
Old 09-22-2006, 12:33 PM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by harvest
server part works only one time...
i take it back, that's worse than nothing at all. and still no please or thankyou...
 
Old 09-22-2006, 02:27 PM   #6
harvest
LQ Newbie
 
Registered: Sep 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by acid_kewpie
i take it back, that's worse than nothing at all. and still no please or thankyou...
Thank you for your time.
 
Old 09-22-2006, 03:54 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
my appologies, that was a but short there... but if you want help from others then you need to provide information for them to work with. you have said nothing to actaully give any idea what is meant to happen in a correct scenario, let alone what is going wrong. you have to help yourself if you expect otehrs to do the same.
 
Old 09-23-2006, 03:04 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

That said: kewp is right :}
 
Old 09-23-2006, 11:07 AM   #9
harvest
LQ Newbie
 
Registered: Sep 2006
Posts: 8

Original Poster
Rep: Reputation: 0
The code is short and simple. To describe it in words will add more fog than clear existing one. The problem I have is that server gets only one message from the single client and I don't know how to make server respond every time. Please, look into the code it is really simple. There is something very small but very important what I don't know that creates this problem.
http://www.uwsg.iu.edu/hypermail/lin...06.0/0189.html
 
Old 09-23-2006, 07:40 PM   #10
wwwdev
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Rep: Reputation: 0
hi harvest,

Quote:
Originally Posted by harvest
The code is short and simple. To describe it in words will add more fog than clear existing one. The problem I have is that server gets only one message from the single client and I don't know how to make server respond every time. Please, look into the code it is really simple. There is something very small but very important what I don't know that creates this problem.
http://www.uwsg.iu.edu/hypermail/lin...06.0/0189.html
you have wrong sigaction code (mysig(int sig)).
you try reading from closed socket after first client.
your global variable 'c' must be zero after job.
insert something like

close(c);
c = 0;

It will be

if (ret < 0) {
perror("read()");
return;
}
fprintf(stderr, "received %d bytes <%s>\n", ret, buf);
close(c); // <----- see here
c = 0; // <-----
return;
}
...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to make mod_ssl work - virtualhosts make trouble Belize Linux - Software 0 02-26-2005 08:30 PM
How do I make a change to a current kernel? Would 'make oldconfig' work... jtp51 Slackware 11 11-01-2004 11:02 PM
configure, make and make install commands don't work for me Fenster Fedora 8 08-18-2004 10:58 AM
'make' and 'make install' commands dont work on my system? ginda Linux - Newbie 9 04-18-2004 11:17 AM
./configure - make - make install can't work ?!? JZL240I-U Linux - General 14 05-05-2003 03:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration