Makefile
all:client server
client:process.o client.o
gcc -o client process.o client.o
server:process.o server.o
gcc -o server process.o server.o
process.o:
gcc -c process.c -o process.o
clean:
rm -f client server *.o
client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <stddef.h>
#define PATH "/tmp/test_for_unix"
#define PATH_C "/tmp/test_for_unit_client"
int s;
void sig_handle(int sign){
printf("got sign\n");
close(s);
exit(0);
}
int main(int argc, char *argv[])
{
struct sockaddr_un server_addr, client_addr;
int size;
int client;
signal(SIGINT, sig_handle);
s=socket(AF_UNIX, SOCK_STREAM, 0);
printf("client socket fd:%d\n",s);
if ( s < 0)
{
printf("socket error\n");
return -1;
}
/*
bzero(&client_addr, sizeof(client_addr));
client_addr.sun_family = AF_UNIX;
strncpy(client_addr.sun_path, PATH_C, sizeof(client_addr.sun_path));
size=offsetof(struct sockaddr_un, sun_path) + strlen(client_addr.sun_path);
unlink(client_addr.sun_path);
bind(s, (struct sockaddr_un *)&client_addr, size);
*/
bzero(&server_addr, sizeof(server_addr));
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, PATH, sizeof(server_addr.sun_path));
size=offsetof(struct sockaddr_un, sun_path) + strlen(server_addr.sun_path);
connect(s, (struct sockaddr*)&server_addr, size);
printf("connected \n");
process_conn_client(s);
close(s);
printf("done \n");
return 0;
}
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <stddef.h>
#include <string.h>
#define PATH "/tmp/test_for_unix"
#define BACKLOG 2
int ss, sc;
void sig_handle(int sign){
printf("got sign\n");
close(ss);
close(sc);
exit(0);
}
int main(int argc, char *argv[])
{
struct sockaddr_un server_addr;
struct sockaddr_un client_addr;
int err;
int size;
pid_t pid;
signal(SIGINT, sig_handle);
ss = socket(AF_UNIX, SOCK_STREAM, 0);
if ( ss < 0 )
{
printf("socket error\n");
return -1;
}
bzero(&server_addr, sizeof(server_addr));
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, PATH, sizeof(server_addr.sun_path));
size=offsetof(struct sockaddr_un, sun_path) + strlen(server_addr.sun_path);
unlink(PATH);
err = bind(ss, (struct sockaddr*)&server_addr, size);
printf("bind err is %d \n", err);
if ( err < 0 )
{
printf("bind error\n");
return -1;
}
err=listen(ss, BACKLOG);
if ( err < 0 )
{
printf("listen error\n");
return -1;
}
printf("listen:%s\n",PATH);
for(;;)
{
//printf("in for loop before acceptn\n");
socklen_t addrlen = sizeof(struct sockaddr_un);
sc = accept(ss, (struct sockaddr*)&client_addr, &addrlen);
if(sc <= 0)
break;
printf("in for loop afater acceptn");
if(sc < 0 )
{
printf("accept is %d ", sc);
continue;
}
pid =fork();
if (pid ==0 )
{
printf("have a client in 1\n");
close(ss);
process_conn_server(sc);
}
else
{ printf("have a client in 1\n");
close(sc);
}
}
}
process.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
void process_conn_server(int s)
{
ssize_t size=0;
char buffer[1024];
char cmd[1024]={0};
for(;;)
{
memset(buffer, 0, sizeof(buffer));
memset(cmd, 0, sizeof(cmd));
size=read(s, buffer,1024);
if(size <= 0)
return ;
printf("\n server get command from client:%s*size:%d**", buffer);
strcpy(cmd, buffer);
printf("cmd:%s*strlen:%d**\n", cmd, strlen(cmd));
cmd[sizeof(strlen)]='\0';
printf("handle cmd:%s***\n", cmd);
if(strcmp(cmd, "a") == 0){
printf("will run command a\n");
}
else if (strcmp(cmd, "b") == 0){
printf("will run command b\n");
}
else
printf("will run default command\n");
}
}
void process_conn_client(int s)
{
ssize_t size=0;
char buffer[1024];
for(;;)
{
//printf("client 1\n");
size=read(0, buffer,1024);
printf("client -> server command:%s* size:%d**\n", buffer, size);
if(size > 0)
{
write(s, buffer, size);
close(s);
}
}
}