unix local domain, signals, and locking help!
jimi_xyz_at_hotmail.com
Date: 01/29/05
- Next message: Martin Blume: "Re: generic time parsing functions, getdate() DATEMSK examples, and strptime()"
- Previous message: Paul Sheer: "generic time parsing functions, getdate() DATEMSK examples, and strptime()"
- Next in thread: Barry Margolin: "Re: unix local domain, signals, and locking help!"
- Reply: Barry Margolin: "Re: unix local domain, signals, and locking help!"
- Reply: Michael B Allen: "Re: unix local domain, signals, and locking help!"
- Reply: Paul Sheer: "Re: unix local domain, signals, and locking help!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Jan 2005 10:44:30 -0800
Hi all,
I have written a server/client program, just the basics, the program
connect fine, but I am not to sure on how to accomplish the other
tasks. The program is suppose to use locking to avoid conflicts when
two clients attempt to connect to the server at the same time, which I
have no idea on how to do this. Last the program is suppose to allow
the client to send signals to the server, the server just has to print
"signal control -c received". I have a alright understanding with
signals, not the greatest, I am not to sure how to accomplish this
task. here is my code...
p.s: this is homework, and im not expecting anyone to do this for me,
just how it should be done, also could you please show me any thing
wrong that i have done with the program so far, I am new to socket
programming, so please know wise @ss remarks:)
server code..
/****************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define NAME "my_sock"
main(void)
{
int orig_sock;
int new_sock;
int clnt_len;
int i;
int true = 1;
int pid;
static struct sockaddr_un clnt_adr, serv_adr;
static char buf[200];
void clean_up(int, char *);
if((orig_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
perror("generate error");
exit(11);
}
serv_adr.sun_family = AF_UNIX;
strcpy(serv_adr.sun_path, NAME);
/*unlink(NAME);*/
setsockopt(orig_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&true,
sizeof(true));
if (bind(orig_sock, (struct sockaddr *) &serv_adr,
sizeof(serv_adr.sun_family) + strlen(serv_adr.sun_path)) != 0)
{
perror("!!bind error");
clean_up(orig_sock, NAME);
exit(2);
}
listen(orig_sock, 1);
clnt_len = sizeof(clnt_adr);
for(;;)
{
if ((new_sock = accept( orig_sock, (struct sockaddr *) &clnt_adr,
&clnt_len)) < 0 )
{
perror("accept error");
clean_up(orig_sock, NAME);
exit(3);
}
pid = fork();
switch(pid)
{
case -1:
perror("fork error:");
exit(1);
case 0:
/*read from socket descriptor and print to screen*/
sleep(1);
read(new_sock, buf, sizeof(buf));
printf("\n%s\n", buf);
/*////////////////////////////////////////////////*/
exit(0);
default:
close(new_sock);
clean_up(orig_sock, NAME);
exit(0);
}
}/*end switch*/
}/*end main*/
void clean_up(int sd, char *the_file)
{
close(sd);
unlink(the_file);
system("rm my_soc");
}
client code..
/****************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define NAME "my_sock"
int BUFFSIZE = 2000;
main(void)
{
int orig_sock;
int i;
int c;
char inbuf[BUFFSIZE];
static struct sockaddr_un serv_adr;
static char buf[10];
if((orig_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
perror("genarate error");
exit(1);
}
serv_adr.sun_family = AF_UNIX;
strcpy(serv_adr.sun_path, NAME);
for(;;)
{
if(connect( orig_sock, (struct sockaddr *) &serv_adr,
sizeof(serv_adr.sun_family) + strlen(serv_adr.sun_path)) < 0)
{
perror("connect error");
exit(1);
}
for(i = 0; (i < BUFFSIZE - 1) && ((c = getchar()) != '\n'); i++)
{
inbuf[i] = c;
}
inbuf[i] = '\0';
write(orig_sock, inbuf, BUFFSIZE);
}
close(orig_sock);
exit(0);
}/*end main*/
/****************************************************************/
Thank you, any help will be greatly appreciated.
Jimi Barnett
- Next message: Martin Blume: "Re: generic time parsing functions, getdate() DATEMSK examples, and strptime()"
- Previous message: Paul Sheer: "generic time parsing functions, getdate() DATEMSK examples, and strptime()"
- Next in thread: Barry Margolin: "Re: unix local domain, signals, and locking help!"
- Reply: Barry Margolin: "Re: unix local domain, signals, and locking help!"
- Reply: Michael B Allen: "Re: unix local domain, signals, and locking help!"
- Reply: Paul Sheer: "Re: unix local domain, signals, and locking help!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|