#include <stdlib.h>
#include <stdio.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#define PATH "xxxxxx"
//#define PATH "/dev/log"
int main(int argc, char *argv[])
{
char buf[100];
int sock_fd, len, ret, client_fd, result, flag, val;
pid_t fd;
fd_set R_FD;
struct timeval timeout;
struct sockaddr_un server_addr, cilent_addr;
unlink(PATH);
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_fd < 0) {
printf("socket fail, %s\n", strerror(errno));
return 0;
}
if (fcntl(sock_fd, F_GETFL, flag) >= 0) {
printf("flag: %x\n", flag);
flag |= O_NONBLOCK;
if (fcntl(sock_fd, F_SETFL, flag) <0)
printf("set noblock error:%s\n", strerror(errno));
}
server_addr.sun_family = AF_UNIX;
strcpy(server_addr.sun_path, PATH);
len = sizeof(server_addr);
val = 1;
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
ret = bind(sock_fd, (struct sockaddr *)&server_addr, len);
if (ret < 0) {
printf("bind error%s\n", strerror(errno));
return 0;
}
listen(sock_fd, 2);
printf("sever running...\n");
while (1) {
timeout.tv_sec = 10; //10 seconds
timeout.tv_usec = 0;
len = sizeof(cilent_addr);
client_fd = accept(sock_fd, (struct sockaddr *)&cilent_addr, &len);
/*
if (client_fd < 0) {
printf("client_fd: %d, %s\n", client_fd, strerror(errno));
}
*/
if (client_fd > 0) {
if (fork() == 0) { //child process
FD_ZERO(&R_FD);
FD_SET(client_fd, &R_FD);
printf("create child process: %d\n", getpid());
while (1) {
//result = select(client_fd + 1, &R_FD, NULL, NULL, &timeout);
result = select(client_fd + 1, &R_FD, NULL, NULL, NULL);
if (result > 0 && FD_ISSET(client_fd, &R_FD)) {
memset(buf, 0, strlen(buf));
ret = read(client_fd, buf, sizeof(buf));
if (ret > 0) {
printf("Server receive data: %s\n", buf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "time: %d, pid: %d", time(NULL), getpid());
ret = write(client_fd, buf, strlen(buf));
if (ret > 0)
printf("Server Send data: %s\n", buf);
}
}
}
}
else //father process
{
close(client_fd); //涓昏繘绋嬩笉鍐嶄娇鐢ㄨ瀹㈡埛绔殑client_fd锛屽瓙杩涚▼浠ュ鐞? continue;
}
}
}
}
#include <stdlib.h>
#include <stdio.h>
#include <sys/syslog.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#define PATH "xxxxxx"
int main(int argc, char *argv[])
{
const char *ptr = "Unix Socket syslog test";
char buf[100];
int len, sockfd, result;
pid_t pid;
fd_set readfd;
struct sockaddr_un address;
struct timeval timeout;
memset(buf, 0, sizeof(buf));
timeout.tv_sec = 10; //2 seconds
timeout.tv_usec = 0;
sockfd=socket(AF_UNIX,SOCK_STREAM,0);
if (sockfd < 0) {
printf("%s\n", strerror(errno));
exit(1);
}
address.sun_family=AF_UNIX;
strcpy(address.sun_path,PATH);
len=sizeof(address);
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result==-1){
perror("oops:client1");
exit(1);
}
FD_ZERO(&readfd);
FD_SET(sockfd, &readfd);
while (1) {
timeout.tv_sec = 10; //2 seconds
timeout.tv_usec = 0;
sleep(3);
sprintf(buf, "%d", time(NULL));
result = write(sockfd, buf, strlen(buf));
if (result > 0) {
printf("Client Send data: %s\n", buf);
memset(buf, 0, sizeof(buf));
result = select(sockfd + 1, &readfd, NULL, NULL, &timeout);
if (result > 0 && FD_ISSET(sockfd, &readfd)) {
result = read(sockfd, buf, sizeof(buf));
if (result > 0)
printf("Client Receive data: %s\n", buf);
}
}
}
}
结果: