socket domain 样例
2015-06-01 15:32
417 查看
服务端
#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;
}
收藏于 2014-07-21
来自于百度空间
#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;
}
收藏于 2014-07-21
来自于百度空间
相关文章推荐
- org.hibernate.AssertionFailure: null id don't flus
- socketpair初识
- LeetCode Container With Most Water
- http://blog.csdn.net/liuhe688/article/details/6715983/
- YT02-简单数学课堂题-1004 Fibonacci Again -(5.31日-烟台大学ACM预备队解题报告)
- Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的权利字符串" UserInfo=0x1bad30 {NSLocalizedDescription=未找到应用程序的“aps-environment”的权利字符串}
- Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER 解决方案
- HackerRank - "Detecting Valid Latitude and Longitude Pairs"
- http error 503. the service is unavailable. sharepoint
- Leetcode Problem.172—Factorial Trailing Zeroes
- AIX下的nmon的使用
- 在可以调用OLE之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute
- shrio 权限管理filterChainDefinitions过滤器配置
- httpd: Could not reliably determine the server's fully qualified domain name
- leetcode_Contains Duplicate II_easy
- EAI和ESB
- leetcode_Contains Duplicate_easy
- AEAI CRM客户关系管理V1.0.3版发版说明
- 【译】使用Rails 4.2+ 测试异步邮件系统
- CodeForces 538E Demiurges Play Again(博弈dp)