#include #include int getsockopt(int sockfd, int level, int optname,void *optval, socklen_t *optlen);int setsockopt(int sockfd, int level, int optname,const void *optval, socklen_t optlen);
int on=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
int on=1
setsockopt(fd,SOL_SOCKET, SO_BROADCAST,&on,sizeof(on))
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
struct timeval tv={5,0};
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv));
struct ip_mreq iq;
iq.imr_multiaddr.s_addr=inet_addr("234.234.234.234"); //224-239
iq.imr_interface.s_addr=inet_addr("192.168.10.251"); //自己 ip 加入组播 ip
setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,&iq,sizeof(iq));
struct ip_mreq {
struct in_addr imr_multiaddr; /* IP multicast address of group 多播组的地址 */
struct in_addr imr_interface; /* local IP address of interface 本地 ip 地址 */
};
查找
zhongl@ubuntu:/usr/include$ grep -nir "struct ip_mreq" *
linux/in.h:166:struct ip_mreq {
..
zhongl@ubuntu:/usr/include$ vi linux/in.h +166
zhongl@ubuntu:/usr/include$
#include
#include
#include
#include
#include
#include
#include
#include
#include
void *send_mesg(void *arg)
{
int newfd=*((int *)arg);
char buf[50]={};
while(1){
bzero(buf,50);
printf("pls input your message:\n");
scanf("%s",buf);
send(newfd,buf,strlen(buf),0);
}
}
int main(void)
{
int fd=socket(AF_INET,SOCK_STREAM,0);
if(fd==-1){
perror("socket");
}
int on;
int len=sizeof(on);
getsockopt(fd, SOL_SOCKET,SO_REUSEADDR,&on,&len);// on=0 刚开始没有
printf("on=%d\n",on);
printf("len=%d\n",len);
on=1;
setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
getsockopt(fd, SOL_SOCKET,SO_REUSEADDR,&on,&len); //on=1 现在有了
printf("on=%d\n",on);
printf("len=%d\n",len);
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(10001);
//server.sin_addr.s_addr=inet_addr("192.168.7.3");
inet_aton("192.168.0.251", &server.sin_addr);
int ret=bind(fd,(struct sockaddr *)(&server),sizeof(server));
if(ret==-1){
perror("bind");
}
listen(fd,5);
struct sockaddr_in client;
len=sizeof(client);
int newfd=accept(fd,(struct sockaddr *)(&client),&len);
//运行服务器代码,程序阻塞在这个位置
printf("ip=%s\n",inet_ntoa(client.sin_addr));
printf("port=%d\n",ntohs(client.sin_port));
//创建线程
pthread_t tid;
pthread_create(&tid,NULL,send_mesg,&newfd);//传入文件描述符
char buf[50]={};
while(1){
bzero(buf,50);
ret=recv(newfd,buf,sizeof(buf),0);
if(ret==0){
break;
}
printf("server recv mess:%s\n",buf);
}
close(newfd);
close(fd);
return 0;
}
结果
zhongl@ubuntu:~/net/3day$ gcc getsockopt_server.c -o getsockopt_server -lpthread
zhongl@ubuntu:~/net/3day$ ./getsockopt_server
on=0
len=4
on=1
len=4
struct timeval tv={5,0}; //5 秒select(sofd+1,&rdfs,NULL,NULL,&tv);
struct timeval tv={5,0};setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv));
void handler(int signo){return;}// 信号处理函数struct sigaction act;sigaction(SIGALRM,NULL,&act);// 注册闹钟信号act.sa_handler=handler;// 设置信号处理函数act.sa_flag&=~SA_RESTART;// 设置自重启属性为假sigaction(SIGALRM,&act,NULL);// 信号处理函数alarm(5);// 定时器设置 5 秒钟if(recv()<0)...
sockfd = socket(,,);...int on = 1;setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));...sendto();
//#define INADDR_ANY ((unsigned long int) 0x00000000)inet_addr("192.168.0.255");htonl(INADDR_ANY); // 用宏是长整型inet_addr("0.0.0.0") // 宏实际转换为 "0.0.0.0"查找ron@ubuntu:/usr/include$ grep -nir "INADDR_ANY" *linux/in.h:273:#define INADDR_ANY ((unsigned long int) 0x00000000)接收端可以使用3种://recv.sin_addr.s_addr=inet_addr("192.168.0.255");//recv.sin_addr.s_addr=inet_addr("0.0.0.0");recv.sin_addr.s_addr=htonl(INADDR_ANY);
struct sockaddr_un{sa_family_t sun_family; // 协议类型char sun_path[108]; // 套接字文件路径}
struct sockaddr_un myaddr;bzero(&myaddr, sizeof(myaddr));myaddr.sun_family = AF_UNIX;strcpy(myaddr.sun_path, "mysocket");
UNIX 域双向通信,UDP 要两个 socket 文件