unix domain sockets
jimi_xyz_at_hotmail.com
Date: 01/31/05
- Next message: Fletcher Glenn: "Re: unix domain sockets"
- Previous message: David Resnick: "Re: debugging in linux"
- Next in thread: Fletcher Glenn: "Re: unix domain sockets"
- Reply: Fletcher Glenn: "Re: unix domain sockets"
- Reply: Anders Mikkelsen: "Re: unix domain sockets"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 31 Jan 2005 12:00:31 -0800
Ok I have written a domain socket, it seems to connect fine, but for
some reason when i #define NAME "my_sock" it doesn't create the socket
the full name, if i do this in the program it creates a file called
"my_soc" when it should be "my_sock". Another problem I have is when i
try to run the server i get a bind error, that says "address allready
in use", so in order for the server to run multiple times I need to do
a system("my_soc"), this isn't right, I beleive unlink should take care
of this problem when the program first runs, but it doesn't. Last
question is how would i go about sending a signal from the client to
the server, example the user sends c-control from the client, the
server says "signal received", then writes back to the client, "server
recieved signal".
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_LOCAL, SOCK_STREAM, 0)) < 0)
{
perror("generate error");
exit(1);
}
unlink(NAME);
bzero(&serv_adr, sizeof(serv_adr));
serv_adr.sun_family = AF_LOCAL;
strcpy(serv_adr.sun_path, 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_LOCAL, SOCK_STREAM, 0)) < 0)
{
perror("genarate error");
exit(1);
}
serv_adr.sun_family = AF_LOCAL;
strcpy(serv_adr.sun_path, NAME);
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 input will help,
James Barnett
- Next message: Fletcher Glenn: "Re: unix domain sockets"
- Previous message: David Resnick: "Re: debugging in linux"
- Next in thread: Fletcher Glenn: "Re: unix domain sockets"
- Reply: Fletcher Glenn: "Re: unix domain sockets"
- Reply: Anders Mikkelsen: "Re: unix domain sockets"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|