socket进程通信与网络通信使用的是统一套接口,只是地址结构与某些参数不同。
一。创建socket服务端的流程如下:
(1)创建socket,类型为AF_LOCAL或AF_UNIX,表示用于进程通信:
[cpp] view plain copy
int server_fd;
int client_fd;//client file descriptor
struct sockaddr_un server_addr;
struct sockaddr_un client_addr;
size_t server_len,client_len;
//cteate server socket
//delete old socket file
unlink(SERVER_NAME);
if ((server_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
(2)命名socket。这里面有一个很关键的东西,socket进程通信命名方式有两种。一是普通的命名,socket会根据此命名创建一个同名的socket文件,客户端连接的时候通过读取该socket文件连接到socket服务端。这种方式的弊端是服务端必须对socket文件的路径具备写权限,客户端必须知道socket文件路径,且必须对该路径有读权限。另外一种命名方式是抽象命名空间,这种方式不需要创建socket文件,只需要命名一个全局名字,即可让客户端根据此名字进行连接。后者的实现过程与前者的差别是,后者在对地址结构成员sun_path数组赋值的时候,必须把第一个字节置0,即sun_path[0] = 0,下面用代码说明:
第一种方式:
[cpp] view plain copy
//name the server socket
server_addr.sun_family = AF_UNIX;
strcpy(server_addr.sun_path,SERVER_NAME);
server_len = sizeof(struct sockaddr_un);
client_len = server_len;
第二种方式:
[cpp] view plain copy
//name the socket
server_addr.sun_family = AF_UNIX;
strcpy(server_addr.sun_path, SERVER_NAME);
server_addr.sun_path[0]=0;
//server_len = sizeof(server_addr);
server_len = strlen(SERVER_NAME) + offsetof(struct sockaddr_un, sun_path);
其中,offsetof函数在#include 头文件中定义。因第二种方式的首字节置0,我们可以在命名字符串SERVER_NAME前添加一个占位字符串,例如:
[cpp] view plain copy
#define SERVER_NAME @socket_server
前面的@符号就表示占位符,不算为实际名称。
或者可以把第二种方式的实现封装成一个函数:
[cpp] view plain copy
int makeAddr(const char* name, struct sockaddr_un* pAddr, socklen_t* pSockLen)
{
int nameLen = strlen(name);
if (nameLen >= (int) sizeof(pAddr->sun_path) -1) /* too long? */
return -1;
pAddr->sun_path[0] = '\0'; /* abstract namespace */
strcpy(pAddr->sun_path+1, name);
pAddr->sun_family = AF_UNIX;
*pSockLen = 1 + nameLen + offsetof(struct sockaddr_un, sun_path);
return 0;
}
像下面这样使用这个函数:
[cpp] view plain copy
makeAddr("server_socket", &server_addr, &server_len);
提示:客户端连接服务器的时候,必须与服务端的命名方式相同,即如果服务端是普通命名方式,客户端的地址也必须是普通命名方式;如果服务端是抽象命名方式,客户端的地址也必须是抽象命名方式。
(3)绑定并侦听
[cpp] view plain copy
bind(server_sockfd, (struct sockaddr *)&server_addr, server_len);
//listen the server
listen(server_sockfd, 5);
(4)等待客户端连接,并读写数据。
[cpp] view plain copy
while(1){
printf("server waiting...\n");
//accept client connect
client_len = sizeof(client_addr);
client_sockfd = accept(server_sockfd,(struct sockaddr*)&client_addr, &client_len);
//read data from client socket
read(client_sockfd, &ch, 1);
printf("read from client %d: %c",client_sockfd,ch);
ch ++;
write(client_sockfd, &ch, 1);
close(client_sockfd);
usleep(100);//1000 miliseconds = 1 second
}
二 socket客户端创建流程
(1)创建socket
(2)命名socket
(3)连接到服务端:
[cpp] view plain copy
//connect to server
result = connect(sockfd, (struct sockaddr*)&address, len);
(4)与服务端进行通信
[cpp] view plain copy
//communicate with server socket
while(1)
{
printf("set send content:");
scanf("%c",&ch);
write(sockfd, &ch, 1);
printf("send to server:%c \n",ch);
read(sockfd, &ch, 1);
printf("read from server: %c\n", ch);
}
完整代码如下:
(1)服务端server.c:
[cpp] view plain copy
#include
#include
#include
#include
#include
#include
#include
#define SERVER_NAME "@server_socket"
/*
* Create a UNIX-domain socket address in the Linux "abstract namespace".
*
* The socket code doesn't require null termination on the filename, but
* we do it anyway so string functions work.
*/
int makeAddr(const char* name, struct sockaddr_un* pAddr, socklen_t* pSockLen)
{
int nameLen = strlen(name);
if (nameLen >= (int) sizeof(pAddr->sun_path) -1) /* too long? */
return -1;
pAddr->sun_path[0] = '\0'; /* abstract namespace */
strcpy(pAddr->sun_path+1, name);
pAddr->sun_family = AF_UNIX;
*pSockLen = 1 + nameLen + offsetof(struct sockaddr_un, sun_path);
return 0;
}
int main()
{
int server_sockfd, client_sockfd;
socklen_t server_len, client_len;
struct sockaddr_un server_addr;
struct sockaddr_un client_addr;
char ch;
int nread;
//delete the old server socket
//unlink("server_socket");
//create socket
server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
//name the socket
server_addr.sun_family = AF_UNIX;
strcpy(server_addr.sun_path, SERVER_NAME);
server_addr.sun_path[0]=0;
//server_len = sizeof(server_addr);
server_len = strlen(SERVER_NAME) + offsetof(struct sockaddr_un, sun_path);
//makeAddr("server_socket", &server_addr, &server_len);
bind(server_sockfd, (struct sockaddr *)&server_addr, server_len);
//listen the server
listen(server_sockfd, 5);
client_sockfd = -1;
client_len = sizeof(client_addr);
while(1){
printf("server waiting...\n");
//accept client connect
if(client_sockfd == -1){
client_sockfd = accept(server_sockfd,(struct sockaddr*)&client_addr, &client_len);
}
//read data from client socket
nread = read(client_sockfd, &ch, 1);
if(nread == 0){//client disconnected
printf("client %d disconnected\n",client_sockfd);
client_sockfd = -1;
}
else{
printf("read from client %d: %c\n",client_sockfd,ch);
ch ++;
write(client_sockfd, &ch, 1);
}
usleep(100);//1000 miliseconds = 1 second
}
return 0;
}
(2)客户端client.c
[cpp] view plain copy
#include
#include
#include
#include
#include
#include
#include
#define SERVER_NAME "@server_socket"
/*
* Create a UNIX-domain socket address in the Linux "abstract namespace".
*
* The socket code doesn't require null termination on the filename, but
* we do it anyway so string functions work.
*/
int makeAddr(const char* name, struct sockaddr_un* pAddr, socklen_t* pSockLen)
{
int nameLen = strlen(name);
if (nameLen >= (int) sizeof(pAddr->sun_path) -1) /* too long? */
return -1;
pAddr->sun_path[0] = '\0'; /* abstract namespace */
strcpy(pAddr->sun_path+1, name);
pAddr->sun_family = AF_UNIX;
*pSockLen = 1 + nameLen + offsetof(struct sockaddr_un, sun_path);
return 0;
}
int main()
{
int sockfd;
socklen_t len;
struct sockaddr_un address;
int result;
char ch = 'A';
//create socket
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
//name the server socket
//makeAddr("server_socket", &address, &len);
address.sun_family = AF_UNIX;
strcpy(address.sun_path, SERVER_NAME);
address.sun_path[0]=0;
//len = sizeof(address);
len = strlen(SERVER_NAME) + offsetof(struct sockaddr_un, sun_path);
//connect to server
result = connect(sockfd, (struct sockaddr*)&address, len);
if(result == -1)
{
perror("opps:client1");
exit(1);
}
//communicate with server socket
while(1)
{
printf("set send content:");
scanf("%c",&ch);
write(sockfd, &ch, 1);
printf("send to server:%c \n",ch);
read(sockfd, &ch, 1);
printf("read from server: %c\n", ch);
}
exit(0);
}
评论
查看更多