赞
踩
socket的地址数据结构根据不同的系统以及网络环境有不同形式。为了使不同格式地址能够被传入套接字函数,必须强制将地址结构转换为:
- struct sockaddr{
- sa_family_t sa_family; /* address family*/
- char sa_data[]; /* variable-length address*/
- ...
- };
- struct sockaddr{
- sa_family_t sa_family; /* address family*/
- char sa_data[14]; /* variable-length address*/
- };
其中sa_family_t表示套接字的通信域。主要有以下四个值
域 | 描述 |
AF_INET | IPv4因特网域 |
AF_INET6 | IPv6因特网域 |
AF_UNIX | UNIX域 |
AF_UNSPEC | 未指定 |
创建套接字的函数如下
- #include <sys/socket.h>
- int socket(int domain, int type, int protocol);/*成功返回文件(套接字)描述符,出错返回-1
类型 | 描述 |
SOCK_DGRAM | 长度固定的、无连接的不可靠报文传递 |
SOCK_RAM | IP协议的数据报接口 |
SOCK_SEQPACKET | 长度固定、有序、可靠的面向连接报文传递 |
SOCK_STREAM | 有序、可靠、双向的面向连接字节流 |
- /*
- domain_socket.h
- @Author: duanjigang @2006-4-11
- @Desp: declaratin of methods used for unix-domain-socket communication
- */
- #ifndef _H_
- #define _H_
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/un.h>
- #include <sys/socket.h>
- #define MSG_SIZE 1024
- int init_send_socket(struct sockaddr_un * addr,char * path)
- {
- int sockfd,len;
- sockfd=socket(AF_UNIX,SOCK_DGRAM,0);
- if(sockfd<0)
- {
- exit(1);
- }
- bzero(addr,sizeof(struct sockaddr_un));
- addr->sun_family=AF_UNIX;
- strcpy(addr->sun_path,path);
- return sockfd;
- }
- int init_recv_socket(char * path)
- {
- int sockfd,len;
- struct sockaddr_un addr;
- sockfd=socket(AF_UNIX,SOCK_DGRAM,0);
- if(sockfd<0)
- {
- return -1;
- }
- bzero(&addr,sizeof(struct sockaddr_un));
- addr.sun_family = AF_UNIX;
- strcpy(addr.sun_path, path);
- unlink(path);
- len = strlen(addr.sun_path) + sizeof(addr.sun_family);
- if(bind(sockfd,(struct sockaddr *)&addr,len)<0)
- {
- return -1;
- }
- return sockfd;
-
- }
- int receive_from_socket(int sockfd, char msg[])
- {
- int n;
- memset(msg, 0, MSG_SIZE);
- n=recvfrom(sockfd, msg, MSG_SIZE, 0, NULL, NULL);
- if(n<=0)
- {
- return -1;
- }
- msg[n]=0;
- return n;
- }
- int send_to_socket(int sockfd, char msg[], const struct sockaddr_un * addr)
- {
- int len;
- len = strlen(addr->sun_path)+sizeof(addr->sun_family);
- sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr*)addr,len);
- return 1;
- }
- #endif
- /*
- main.c
- @Author: duanjigang @ 2006-4-11
- @Desp: Two processes communicate with unix domain socket
- */
- #include "domain_socket.h"
- #define PATH "/home/useless"
- /*
- 进程间通过域进行通讯-举例:父子进程,一个发送,一个接收
- */
- int main(void)
- {
- int pid;
- /*
- 子进程用于发送消息
- */
- if((pid = fork()) == 0)
- {
- int fd, counter = 0;
- char send_buffer[MSG_SIZE];
- struct sockaddr_un addr;
- if( (fd = init_send_socket(&addr, PATH)) > 0)
- while(1)
- {
- memset(send_buffer, 0 , MSG_SIZE);
- /*
- 防止计数器越界,所以做一个复位判断
- */
- sprintf(send_buffer,"message for %d times",counter++ >= 10000 ? 1 : counter);
- send_to_socket(fd, send_buffer, &addr);
- printf("Sender: %s\n", send_buffer);
- sleep(1);
- }
- }/*
- 父进程用于接收消息
- */
- else
- {
- int fd;
- char recv_buffer[MSG_SIZE];
- if( (fd = init_recv_socket(PATH))> 0)
- while(1)
- {
- memset(recv_buffer, 0, MSG_SIZE);
- if(receive_from_socket(fd, recv_buffer))
- {
- printf("Receiver: %s\n", recv_buffer);
- }
- }
- }
- }
- Sender: message for 1 times
-
- Sender: message for 2 times
-
- Receiver: message for 2 times
-
- Sender: message for 3 times
-
- Receiver: message for 3 times
-
- Sender: message for 4 times
-
- Receiver: message for 4 times
-
- Sender: message for 5 times
-
- Receiver: message for 5 times
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。