广播
brdRcv.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
//create socket
int bs_skt_fd=socket(AF_INET,SOCK_DGRAM,0);
if(bs_skt_fd==-1){
perror("socket error");
return -1;
}
//form the broadcast address
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=inet_addr("255.255.255.255");
sin.sin_port=htons(6789);
//bind
if(bind(bs_skt_fd,(struct sockaddr *)&sin,sizeof(sin))==-1){
perror("bind error");
return -1;
}
//recieve message
char buf[128]="";
while(1){
recv(bs_skt_fd,buf,sizeof(buf),0);
printf("recieve message:%s\n",buf);
}
//close socket
close(bs_skt_fd);
return 0;
}
brdSnd.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//create socket
int bs_skt_fd=socket(AF_INET,SOCK_DGRAM,0);
if(bs_skt_fd==-1){
perror("socket error");
return -1;
}
int broadcast=1;
//get broadcast permmission
if(setsockopt(bs_skt_fd,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast))==-1){
perror("setsockopt error");
return -1;
}
//form broadcast address stucture
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=inet_addr("255.255.255.255");
sin.sin_port=htons(6789);
//send messsage
char buf[128]="";
while(1){
printf("send message>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
sendto(bs_skt_fd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin));
bzero(buf,sizeof(buf));
}
//close socket
close(bs_skt_fd);
return 0;
}
组播
mltRcv.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//create socket
int bs_skt_fd=socket(AF_INET,SOCK_DGRAM,0);
if(bs_skt_fd==-1){
perror("socket error");
return -1;
}
//get permission to the group
struct ip_mreqn im;
im.imr_multiaddr.s_addr=inet_addr("224.1.2.3");
im.imr_address.s_addr=inet_addr("192.168.122.150");
im.imr_ifindex=2;
if(setsockopt(bs_skt_fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(struct ip_mreqn *)&im,sizeof(im))==-1){
perror("setsockopt error");
return -1;
}
//form the multicast group address structure
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(6789);
sin.sin_addr.s_addr=inet_addr("224.1.2.3");
//bind
if(bind(bs_skt_fd,(struct sockaddr *)&sin,sizeof(sin))==-1){
perror("bind error");
return -1;
}
//recieve message
char buf[128]="";
while(1){
bzero(buf,sizeof(buf));
recv(bs_skt_fd,buf,sizeof(buf),0);
printf("recieve message:%s\n",buf);
}
//close socket
return 0;
}
mltSnd.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//create socket
int bs_skt_fd=socket(AF_INET,SOCK_DGRAM,0);
if(bs_skt_fd==-1){
perror("socket error");
return -1;
}
//form the multicast address structure
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(6789);
sin.sin_addr.s_addr=inet_addr("224.1.2.3");
//send message
char buf[128]="";
while(1){
printf("send message>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
sendto(bs_skt_fd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin));
}
//close socket
return 0;
}
流式域套接字
localTcpSer.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
//create socket
int sktfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sktfd==-1){
perror("socket error");
return -1;
}
//delete the socket file if exist already
if(access("./unix",F_OK)==0){
if((unlink("./unix"))==-1){
perror("unlink error");
return -1;
}
}
//set address
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
//bind address to socket
if((bind(sktfd,(struct sockaddr *)&sun,sizeof(sun)))==-1){
perror("bind error");
return -1;
}
//listen
if((listen(sktfd,128))==-1){
perror("listen error");
return -1;
}
printf("------------------4\n");
//accept
struct sockaddr_un rsun;
socklen_t len=sizeof(rsun);
int rsktfd=accept(sktfd,(struct sockaddr *)&rsun,&len);
printf("rsktfd=%d\n",rsktfd);
if(rsktfd==-1){
perror("accept error");
return -1;
}
printf("------------------5\n");
//recieve
char buf[128]="";
while(1){
printf("---rsktfd=%d\n",rsktfd);
int res=recv(rsktfd,buf,sizeof(buf),0);
printf("recvfrom=%d\n",res);
//buf[strlen(buf)-1]='\0';
printf("recieve message:%s\n",buf);
}
//close
close(rsktfd);
close(sktfd);
return 0;
}
localTcpCli.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
//create socket
int sktfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sktfd==-1){
perror("socket error");
return -1;
}
//delete the socket file if exist already
if(access("./linux",F_OK)==0){
if((unlink("./linux"))==-1){
perror("unlink error");
return -1;
}
}
//set address
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./linux");
//bind address to socket
if((bind(sktfd,(struct sockaddr *)&sun,sizeof(sun)))==-1){
perror("bind error");
return -1;
}
//connect
struct sockaddr_un ssun;
ssun.sun_family=AF_UNIX;
strcpy(ssun.sun_path,"./unix");
if((connect(sktfd,(struct sockaddr *)&ssun,sizeof(ssun))==-1)){
perror("connect error");
return -1;
}
//send
char buf[128]="";
while(1){
printf("your message>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
printf("buf=%s\n",buf);
int sndto=send(sktfd,buf,sizeof(buf),0);
printf("sndto=%d\n",sndto);
}
//close
//close(sktfd);
close(sktfd);
return 0;
}
报式域套接字
localUdpSer.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
//create socket
int ssktfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(ssktfd==-1){
perror("socket error");
return -1;
}
//delete address file if exist
if((access("./SF",F_OK))==0){
if(unlink("./SF")==-1){
perror("unlink error");
return -1;
}
}
//form address
struct sockaddr_un ssun;
ssun.sun_family=AF_UNIX;
strcpy(ssun.sun_path,"./SF");
//bind address to socket
if((bind(ssktfd,(struct sockaddr *)&ssun,sizeof(ssun))==-1)){
perror("bind error");
return -1;
}
//revieve and send
char buf[128]="";
struct sockaddr_un csun;
socklen_t len=sizeof(csun);
while(1){
bzero(buf,sizeof(buf));
recvfrom(ssktfd,buf,sizeof(buf),0,(struct sockaddr *)&csun,&len);
printf("message from [%s]:%s\n",csun.sun_path,buf);
strcat(buf,"--++--");
sendto(ssktfd,buf,sizeof(buf),0,(struct sockaddr *)&csun,len);
}
//close socket
close(ssktfd);
return 0;
}
localUdpCli.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/un.h>
int main(int argc, const char *argv[])
{
//create socket
int csktfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(csktfd==-1){
perror("socket error");
return -1;
}
//delete address file if exist
if((access("./CF",F_OK))==0){
if(unlink("./CF")==-1){
perror("unlink error");
return -1;
}
}
//form address
struct sockaddr_un csun;
csun.sun_family=AF_UNIX;
strcpy(csun.sun_path,"./CF");
//bind address to socket
if((bind(csktfd,(struct sockaddr *)&csun,sizeof(csun))==-1)){
perror("bind error");
return -1;
}
//input server address structure
struct sockaddr_un ssun;
ssun.sun_family=AF_UNIX;
strcpy(ssun.sun_path,"./SF");
socklen_t len=sizeof(ssun);
//revieve and send
char buf[128]="";
struct sockaddr_un csun2;
while(1){
printf("input>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
sendto(csktfd,buf,sizeof(buf),0,(struct sockaddr *)&ssun,len);
recvfrom(csktfd,buf,sizeof(buf),0,(struct sockaddr *)&csun2,&len);
printf("message from server[%s]: %s\n",csun2.sun_path,buf);
}
//close socket
close(csktfd);
return 0;
}