socket domain 样例
服务端
#include<stdio.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#define LISTEN_SOCKET "/tmp/liuyi.sock" //
#define LISTEN_BACKLOG 5
#define BIG_BUF 4096
int main()
{
int listenfd,connfd;
struct sockaddr_un un_s,un_c;
int len;
struct stat statbuf;
uid_t uid;
int ret;
char recvBuf[BIG_BUF];
char sendBuf[BIG_BUF];
listenfd=socket(AF_UNIX, SOCK_STREAM, 0);
if(listenfd<=0){
perror("socket create error");
exit(-1);
}
unlink(LISTEN_SOCKET);
memset(&un_s,0,sizeof(struct sockaddr_un));
un_s.sun_family = AF_UNIX;
strcpy(un_s.sun_path, LISTEN_SOCKET);
len = offsetof(struct sockaddr_un, sun_path) + strlen(LISTEN_SOCKET);
if(bind(listenfd,(struct sockaddr *)&un_s, len)<0){
perror("socket bind error");
exit(-1);
}
if(listen(listenfd,LISTEN_BACKLOG)<0){
perror("socket listen error");
exit(-1);
}
memset(&un_c,0,sizeof(struct sockaddr_un));
len=sizeof(struct sockaddr_un);
connfd=accept(listenfd,(struct sockaddr *)&un_c,&len);
if(connfd<=0){
perror("socket accept error");
exit(-1);
}
printf("connfd=[%d]\n",connfd);
len -= offsetof(struct sockaddr_un, sun_path);
un_c.sun_path[len] ='\0';
//取文件状态
if(stat(un_c.sun_path, &statbuf)<0){
perror("stat error");
exit(-1);
}
//判断是不是socket 文件 srwxr-xr-x 1 sandjmk liuyi.sock
if(S_ISSOCK(statbuf.st_mode) == 0){
perror("Is not a socket");
exit(-1);
}
printf("useid[%d]socket[%s]connect\n",statbuf.st_uid,un_c.sun_path);
unlink(un_c.sun_path);
memset(recvBuf,0,sizeof(recvBuf));
ret = read(connfd,recvBuf,sizeof(recvBuf));
printf("read ret[%d][%d]\n",ret,errno);
printf("recv:[%s]\n",recvBuf);
memset(sendBuf,'B',sizeof(sendBuf)-1);
ret = write(connfd,sendBuf,sizeof(sendBuf));
printf("write ret=[%d]\n",ret);
close(connfd);
return 0;
}
客户端
#include<stdio.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#define SOCKET_PATH "/tmp/"
#define LISTEN_SOCKET "/tmp/liuyi.sock"
#define BIG_BUF 4096
int main()
{
int connfd;
struct sockaddr_un un_s,un_c;
int len;
int ret;
char recvBuf[BIG_BUF];
char sendBuf[BIG_BUF];
connfd=socket(AF_UNIX, SOCK_STREAM, 0);
if(connfd<=0){
perror("socket create error");
exit(-1);
}
memset(&un_s,0,sizeof(struct sockaddr_un));
un_s.sun_family = AF_UNIX;
sprintf(un_s.sun_path, "%sliuyi.%05d",SOCKET_PATH,getpid());
len = offsetof(struct sockaddr_un, sun_path) + strlen(un_s.sun_path);
unlink(un_s.sun_path);
if(bind(connfd,(struct sockaddr *)&un_s, len)<0){
perror("socket bind error");
exit(-1);
}
memset(&un_s,0,sizeof(struct sockaddr_un));
un_s.sun_family = AF_UNIX;
sprintf(un_s.sun_path,LISTEN_SOCKET);
len = offsetof(struct sockaddr_un, sun_path) + strlen(un_s.sun_path);
if(connect(connfd,(struct sockaddr *)&un_s, len)<0){
perror("socket connect error");
exit(-1);
}
memset(sendBuf,'A',sizeof(sendBuf)-1);
ret = write(connfd,sendBuf,sizeof(sendBuf));
printf("write ret=[%d]\n",ret);
//sleep(1);
memset(recvBuf,0,sizeof(recvBuf));
ret = read(connfd,recvBuf,sizeof(recvBuf));
printf("read ret[%d][%d]\n",ret,errno);
printf("recv:[%s]\n",recvBuf);
return 0;
}
socket domain 样例的更多相关文章
- 【go】go语言socket通信样例
server.go package main import ( "net" "fmt" "io" ) func main() { liste ...
- socket编程——一个简单的样例
从一个简单的使用TCP样例開始socket编程,其基本过程例如以下: server client ++ ...
- Swift - 使用socket进行通信(附聊天室样例)
在Swift开发中,如果我们需要保持客服端和服务器的长连接进行双向的数据通信,使用socket是一种很好的解决方案. 下面通过一个聊天室的样例来演示socket通信,这里我们使用了一个封装好的sock ...
- python网络编程-socket样例
socket样例 一:只能一个客户端发送一个信息. 客户端 # -*- coding:utf-8 -*- __author__ = 'shisanjun' import socket sock=soc ...
- Python Socket 编程——聊天室演示样例程序
上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和client的代码了解主要的 Python Socket 编程模型.本文再通过一个样例来加强一下对 Socket ...
- 【Java】Java Socket 通信演示样例
用socket(套接字)实现client与服务端的通信. 这里举两个样例: 第一种是每次client发送一个数据,服务端就做一个应答. (也就是要轮流发) 另外一种是client能够连续的向服务端发数 ...
- myloader恢复mysql数据库演示样例
mydumper是针对mysql数据库备份的一个轻量级第三方的开源工具.备份方式为逻辑备份.它支持多线程.备份速度远高于原生态的mysqldump以及众多优异特性.与其相配套的恢复工具则是mylo ...
- Linux crontab 命令格式与具体样例
基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...
- libcurl使用演示样例
简要说明:C++使用libcurl訪问"www.baidu.com".获取返回码和打印出http文件 /* * @ libcurl使用演示样例 * @ 2014.04.29 * @ ...
随机推荐
- springboot sybase 数据库
依赖:(驱动) <!-- https://mvnrepository.com/artifact/net.sourceforge.jtds/jtds --> <dependency&g ...
- 关于消息队列的使用----ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ
一.消息队列概述消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构.目前使用较多的消息队列有ActiveMQ,RabbitM ...
- Redis need tcl 8.5 or newer
hadoop@stormspark:~/workspace/redis2.6.13/src$ make testYou need tcl 8.5 or newer in order to run th ...
- VirtualBox“切换到无缝模式”和“自动调整显示尺寸”菜单无法使用
现象:VirtualBox“切换到无缝模式”和“自动调整显示尺寸”菜单无法使能,无法全窗口显示虚拟机桌面,菜单状态如下图所示: 原因:该功能的支持需要安装VirtualBox增强功能 方法:安装Vir ...
- delphi 图片加水印源代码
unit UWaterMark; interface uses {$IFNDEF DELPHIXE2ANDUP} windows,SysUtils,classes,graphics,Gdiplus; ...
- Ansible 从远程主机添加或删除MySQL数据库
mysql_db - 从远程主机添加或删除MySQL数据库. 概要 要求(在执行模块的主机上) 选项 例子 笔记 状态 支持 概要 从远程主机添加或删除MySQL数据库. 要求(在执行模块的主机上) ...
- Wilcoxon Signed Rank Test
1.Wilcoxon Signed Rank Test Wilcoxon有符号秩检验(也称为Wilcoxon有符号秩和检验)是一种非参数检验.当统计数据中使用“非参数”一词时,并不意味着您对总体一无所 ...
- xpath定位--绝对与相对的定位
xpath定位--绝对与相对的定位: xpath定位即为xml路径语言,它是一种用来确定xml文档中某部分位置的语言,xpath基于xml的树状结构,提供在数据结构中找寻节点的能力 xpath的相对定 ...
- Dungeon Game (GRAPH - DP)
QUESTION The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a ...
- Codeforces 1136E Nastya Hasn't Written a Legend (线段树教做人系列)
题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比如a[i]加上x之后, ...