unix local domain, signals, and locking help!

jimi_xyz_at_hotmail.com
Date: 01/29/05


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



Relevant Pages

  • Re: Adding a second site
    ... DCPROMO the new W2k server and add it to an existing DOMAIN ... What you are trying to accomplish is a very basic set up. ... >> between Site A and B but client authentication would be just at Site B. ... >> Alex Anderson ...
    (microsoft.public.win2000.active_directory)
  • Re: IP stack - ip translation
    ... So if the outgoing server is host1.domain1.com or some ip number, ... Using firewalls like iptables on unix/linux, it's pretty easy to accomplish ... Where does the certificate end up? ...
    (microsoft.public.vc.mfc)
  • Add addl Exchange server
    ... Have searched both the Exch 2000 and Exch 2003 newsgroups ... - is there a way to accomplish what the owner wants ... without having to add another Exchange server to our ...
    (microsoft.public.exchange2000.admin)
  • Re: upgrade wizard 6.5 to 2000
    ... I finally figured out a way to accomplish the upgrade... ... why the heck couldn't the darn Wizard figure out ... > But my import server IS sql server 2000! ...
    (microsoft.public.sqlserver.setup)
  • Re: Exchange 2003 backup
    ... >What I am really trying to accomplish is a way to ... "Front-End" server (in Exchange) is just a protocol proxy. ... >install Exchange server 2003 on that server as ...
    (microsoft.public.exchange.admin)