socket进程通信与网络通信使用的是统一套接口,只是地址结构与某些参数不同。
一。创建socket服务端的流程如下:
(1)创建socket,类型为AF_LOCAL或AF_UNIX,表示用于进程通信:
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);
}
第一种方式:
//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;
//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);
#define SERVER_NAME @socket_server
前面的@符号就表示占位符,不算为实际名称。
或者可以把第二种方式的实现封装成一个函数:
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;
}
makeAddr("server_socket", &server_addr, &server_len);
(3)绑定并侦听
bind(server_sockfd, (struct sockaddr *)&server_addr, server_len);
//listen the server
listen(server_sockfd, 5);
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
}
(1)创建socket
(2)命名socket
(3)连接到服务端:
//connect to server
result = connect(sockfd, (struct sockaddr*)&address, len);
//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:
#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;
}
#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);
}