Unix domain socket

Unix domain socket

原文:https://www.cnblogs.com/sparkdev/p/8359028.html

UNIX Domain Socket是在socket架构上发展起来的用于同一台主机的进程间通讯(IPC),它不需要经过网络协议栈,不需要打包拆包、计算校验和、维护序号和应答等,只是将应用层数据从一个进程拷贝到另一个进程。UNIX Domain Socket有SOCK_DGRAM或SOCK_STREAM两种工作模式,类似于UDP和TCP,但是面向消息的UNIX Domain Socket也是可靠的,消息既不会丢失也不会顺序错乱。


#include <stdlib.h>

#include <stdio.h>

#include <stddef.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#include <ctype.h>

#define MAXLINE 80

char *socket_path = "server.socket";

int main(void)

{

struct sockaddr_un serun, cliun;

socklen_t cliun_len;

int listenfd, connfd, size;

char buf[MAXLINE];

int i, n;

if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {

perror("socket error");

exit(1);

}

memset(&serun, 0, sizeof(serun));

serun.sun_family = AF_UNIX;

strcpy(serun.sun_path, socket_path);

size = offsetof(struct sockaddr_un, sun_path) + strlen(serun.sun_path);

unlink(socket_path);

if (bind(listenfd, (struct sockaddr *)&serun, size) < 0) {

perror("bind error");

exit(1);

}

printf("UNIX domain socket bound\n");

if (listen(listenfd, 20) < 0) {

perror("listen error");

exit(1);

}

printf("Accepting connections ...\n");

while(1) {

cliun_len = sizeof(cliun);

if ((connfd = accept(listenfd, (struct sockaddr *)&cliun, &cliun_len)) < 0){

perror("accept error");

continue;

}

while(1) {

n = read(connfd, buf, sizeof(buf));

if (n < 0) {

perror("read error");

break;

} else if(n == 0) {

printf("EOF\n");

break;

}

printf("received: %s", buf);

for(i = 0; i < n; i++) {

buf[i] = toupper(buf[i]);

}

write(connfd, buf, n);

}

close(connfd);

}

close(listenfd);

return 0;

}


#include <stdlib.h>

#include <stdio.h>

#include <stddef.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#define MAXLINE 80

char *server_path = "server.socket";

int main() {

struct  sockaddr_un serun;

int len;

char buf[100];

int sockfd, n;

if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){

perror("client socket error");

exit(1);

}

memset(&serun, 0, sizeof(serun));

serun.sun_family = AF_UNIX;

strcpy(serun.sun_path, server_path);

len = offsetof(struct sockaddr_un, sun_path) + strlen(serun.sun_path);

if (connect(sockfd, (struct sockaddr *)&serun, len) < 0){

perror("connect error");

exit(1);

}

while(fgets(buf, MAXLINE, stdin) != NULL) {

write(sockfd, buf, strlen(buf));

n = read(sockfd, buf, MAXLINE);

if ( n < 0 ) {

printf("the other side has been closed.\n");

}else {

write(STDOUT_FILENO, buf, n);

}

}

close(sockfd);

return 0;

}

原文地址:https://www.cnblogs.com/sunnypoem/p/11782450.html

时间: 2024-08-29 15:06:19

Unix domain socket的相关文章

ndk学习16: unix domain socket

一.UNIX Domain Socket 概念: UNIX Domain Socket是在socket架构上发展起来的用于同一台主机的进程间通讯(IPC) 特点: 1. 它不需要经过网络协议栈,不需要打包拆包.计算校验和.维护序号和应答等 2. 只是将应用层数据从一个进程拷贝到另一个进程. 工作模式: SOCK_DGRAM     类似于UDP SOCK_STREAM    类似于TCP 用途: UNIX Domain Socket可用于两个没有亲缘关系的进程,是全双工的,是目前使用最广泛的IP

由一个简单需求到Linux环境下的syslog、unix domain socket

本文记录了因为一个简单的日志需求,继而对linux环境下syslog.rsyslog.unix domain socket的学习.本文关注使用层面,并不涉及rsyslog的实现原理,感兴趣的读者可以参考rsyslog官网.另外,本文实验的环境实在debian8,如果是其他linux发行版本或者debian的其他版本,可能会稍微有些差异. 需求: 工作中有一个在Linux(debian8)环境下运行的服务器程序,用python语言实现,代码中有不同优先级的日志需要记录,开发的时候都是使用pytho

unix domain socket示例一(SOCK_DGRAM)

unix domain socket 是IPC通信的一种方式,可用于与管理进程间通信,同时由和网络socket统一,所以很好管理,使用还是比较多.现举个例子: server.c 1 #include <stdio.h> 2 #include <string.h> 3 #include <unistd.h> 4 #include <stdlib.h> 5 #include <sys/types.h> 6 #include <sys/socke

UNIX Domain Socket IPC

目录 目录 概述 socket函数使用 struct sockaddr_un socket bind listen accept connect Socket IPC 实例 server client 运行结果 概述 socket API原本是为网络通讯设计的,但是后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket. 虽然网络socket也可用于同一台主机的进程间通讯(通过loopback地址127.0.0.1),但是UNIX Domain Socket用于

问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;?

错误提示: psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? 出现该问题的很多,以下是目前碰到的几种情况,之后碰到继续补充: 1.删除了/tmp路径中的.s.PGSQL.5432 与.s.

【转】PHP实现系统编程(四)--- 本地套接字(Unix Domain Socket)

原文:http://blog.csdn.net/zhang197093/article/details/78143687?locationNum=6&fps=1 -------------------------------------------------------------------------------------------------------------------------------------------------------- Socket API一开始是为了

UNIX DOMAIN SOCKET效率

关于UNIX DOMAIN SOCKET和普通udp socket的对比 在TX1(4核A57 1.7GHz)的板卡上进行测试,每个包大小设置为1024,全速收发,UDS的速度在90Mbps左右,UDP在120Mbps左右(略有丢包) CPU占用率,UDS比UDP低10%,但是实际上,如果码率相近时,CPU占用率是差不多的 下面是UDP的CPU占用: top - 08:44:46 up 3:04, 5 users, load average: 1.78, 1.70, 1.66 Threads:

[dev][socket] unix domain socket删除socket文件

问题 在使用unix domain socket的时候,bind之后,会在本地路径里 产生一个与path对应的socket文件. 如何正确的在用完socket之后,对其销毁呢? 方案 使用 unlink()函数手工删除. 见: https://stackoverflow.com/questions/34873151/how-can-i-delete-a-unix-domain-socket-file-when-i-exit-my-application syslogd的代码里就是这样干的: ht

redis性能测试tcp socket and unix domain

UNIX Domain Socket IPC socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络socket也可用于同一台主机的进程间通讯(通过loopback地址127.0.0.1),但是UNIX Domain Socket用于IPC更有效率:不需要经过网络协议栈,不需要打包拆包.计算校验和.维护序号和应答等,只是将应用层数据从一个进程拷贝到另一个进程.UNIX域套接字与TCP套接字相比较,在同一台主