unix域套接字
头文件sock.h
封装函数wrap.c
server.c
//:server.c
#include <stdio.h>
#include <sock.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define LISTENQ 5
int main(int argc, char **argv)
{
if(argc != 2){
fprintf(stderr,"Usage:%s <path>\n",argv[0]);
exit(1);
}
int fd;
fd = Socket(AF_LOCAL, SOCK_STREAM, 0);
//unlink(argv[1]);
struct sockaddr_un local_address;
bzero(&local_address, sizeof local_address);
local_address.sun_family = AF_LOCAL;
memcpy(local_address.sun_path,argv[1],strlen(argv[1]));
Bind(fd,(struct sockaddr *)&local_address,sizeof(local_address));
Listen(fd, LISTENQ);
int connfd;
struct sockaddr_un peer_address;
socklen_t peer_address_length;
ssize_t written;
char *welcome_message="welcome to unix domain programming.";
while(1){
peer_address_length = sizeof(peer_address);
connfd = Accept(fd,(struct sockaddr *)&peer_address,&peer_address_length);
//printf("new connection %d from %s.\n", connfd, peer_address.sun_path);
written = write(connfd, welcome_message, strlen(welcome_message));
printf("written %d bytes to %d.\n",written,connfd);
close(connfd);
}
close(fd);
exit(0);
}
client.c
//:client.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sock.h>
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
{
if(argc != 2){
fprintf(stderr,"Usage:%s <path>\n",argv[0]);
exit(1);
}
int fd;
fd = Socket(AF_LOCAL,SOCK_STREAM,0);
struct sockaddr_un remote_address;
bzero(&remote_address, sizeof remote_address);
remote_address.sun_family = AF_LOCAL;
memcpy(remote_address.sun_path,argv[1],strlen(argv[1]));
Connect(fd,(struct sockaddr *)&remote_address,sizeof(remote_address));
/*read and write*/
char buffer[BUFFER_SIZE];
ssize_t n;
again:
if((n=read(fd,buffer,BUFFER_SIZE)) < 0){
if(errno == EINTR)
goto again;
else{
fprintf(stderr,"read() failed\n");
close(fd);
exit(1);
}
}
else if(n == 0)
fprintf(stdout,"Connection closed by peer.\n");
else{
buffer[n] = 0;
fprintf(stdout,"read %d bytes from peer:%s\n",n,buffer);
}
/*close*/
close(fd);
exit(0);
}