//
// SocketUDPServerClient.m
// socket_server_client
//
// Created by lujunjie on 2016/11/26.
// Copyright © 2016年 lujunjie. All rights reserved.
// #import "SocketUDPServerClient.h"
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import "UDPProtocolHerader.h"
#import <ifaddrs.h>
#include <net/if.h>
@interface SocketUDPServerClient()
{
in_addr_t broadcastClientAddr; // 发送广播的地址
}
@end
@implementation SocketUDPServerClient
int serverSockfd = -;
/**
启动服务监听接收广播
*/
- (void)startUDPServer
{
// 第一步:打开套节字描述
serverSockfd = socket(AF_INET, SOCK_DGRAM, );// 协议族、数据报、0
if(serverSockfd < )
{
NSLog(@"error:打开套节字描述符失败socket()");
}
NSLog(@"打开套节字描述sockfd:%d",serverSockfd);
// 第二步:设置广播包
int opt;
if ((setsockopt(serverSockfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(int)))< ) {
NSLog(@"error:广播包setsockopt");
}
// 第三步: bind
struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(); // 5000~655355
serveraddr.sin_addr.s_addr = INADDR_ANY;
// 当你调用bind()函数绑定IP时使用INADDR_ANY ,明接收来自任意IP、任意网卡的发给指定端口的数据。作为发送端,当用调用bind()函数绑定IP时使用INADDR_ANY,表明使用网卡号最低的网卡进行发送数据,也就是UDP数据广播。
if ((bind(serverSockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < ) {
NSLog(@"error:bind");
} [NSThread detachNewThreadSelector:@selector(recvFromThread) toTarget:self withObject:nil]; } - (void)recvFromThread
{
struct sockaddr_in clientaddr;
socklen_t clientaddrLen = sizeof(clientaddr);
int msgHeaderSize = sizeof(CC_searchBrodcastHeader);
char *buf = (char *)malloc(msgHeaderSize);
memset(buf, , sizeof(msgHeaderSize));
long recvSize = ;
while () { if ([self selectReadSockfd:serverSockfd]) { long recvRet = recvfrom(serverSockfd,buf + recvSize,msgHeaderSize -recvSize, ,(struct sockaddr *)&clientaddr,&clientaddrLen);
if (recvRet <= ) {
NSLog(@"<= 0 error:recvfrom errorcode:%zi",recvRet);
sleep();
continue;
}
recvSize += recvRet; if (recvSize >= msgHeaderSize) {
NSString *ipaddr = [self getIPAddress];
const char *addr = [ipaddr UTF8String];
NSString* tempIPString=[NSString stringWithCString:inet_ntoa(clientaddr.sin_addr) encoding:NSUTF8StringEncoding];
NSLog(@"RECV:::::IPADDR: %@",tempIPString);
if (clientaddr.sin_addr.s_addr != inet_addr(addr)) {// 自己不能收到自己的广播
// 接收到的广播
broadcastClientAddr = clientaddr.sin_addr.s_addr;
[self recvSuccessWithBuf:buf];
}
// 清空出来
recvSize = ;
memset(buf, , sizeof(msgHeaderSize));
}
} }
} - (BOOL)selectReadSockfd:(int)sockfd
{
fd_set read_set;
struct timeval tmval;
tmval.tv_sec = ;
tmval.tv_usec = ; FD_ZERO(&read_set); // 将指定的文件描述符集清空
FD_SET(sockfd,&read_set); // 用于在文件描述符集合中增加一个新的文件描述符
int ret =select(sockfd+,&read_set, NULL, NULL,&tmval);
if (ret <= ) {
NSLog(@"<= 0 error:select errorcode:%zi",ret);
return NO;
}
if (FD_ISSET(sockfd, &read_set)) {
return YES;
}
return NO;
} - (BOOL)selectWriteSockfd:(int)sockfd
{
fd_set w_set;
struct timeval tmval;
tmval.tv_sec = ;
tmval.tv_usec = ; FD_ZERO(&w_set); // 将指定的文件描述符集清空
FD_SET(sockfd,&w_set); // 用于在文件描述符集合中增加一个新的文件描述符
int ret =select(sockfd+,NULL, &w_set, NULL,&tmval);
if (ret <= ) {
NSLog(@"<= 0 error:select errorcode:%zi",ret);
return NO;
}
if (FD_ISSET(sockfd, &w_set)) {
return YES;
}
return NO;
} - (void)recvSuccessWithBuf:(char *)buf
{
CC_searchBrodcastHeader *header = (CC_searchBrodcastHeader *)buf;
char pheader[] = {};
memcpy(pheader,header->protocolHeader, sizeof(pheader));
memset(pheader+, , );
NSString *protocolHeader=[NSString stringWithCString:pheader encoding:NSASCIIStringEncoding];
if ([protocolHeader isEqualToString:@"CC"]) {
if (header->controlMask == udp_broadcast_request) { [self sendtoClient]; }else if(header->controlMask == udp_broadCast_reply)
{
struct in_addr temp_in_addr;
memset(&temp_in_addr, , sizeof(temp_in_addr)); memcpy(&temp_in_addr, &header->IP, sizeof(header->IP));
NSString* tempIPString=[NSString stringWithCString:inet_ntoa(temp_in_addr) encoding:NSUTF8StringEncoding];
NSLog(@"对方的IP地址是: %@",tempIPString);
}
}
} - (void)sendtoClient
{
// 回复
NSString *ipaddr = [self getIPAddress];
const char *localAddr = [ipaddr UTF8String]; CC_searchBrodcastHeader header;
memset(&header, , sizeof(header));
header.controlMask = udp_broadCast_reply;
header.protocolHeader[] = 'C';
header.protocolHeader[] = 'C';
header.IP = inet_addr(localAddr); char *buf = (char *)malloc(sizeof(header));
memcpy(buf, &header, sizeof(header)); if ([self selectWriteSockfd:serverSockfd]) {
if ([self sendtoWithSockfd:serverSockfd Buffer:buf size:sizeof(header) addr:broadcastClientAddr]) {
NSLog(@"=====IP:%@",ipaddr);
NSLog(@"=====IP发送成功");
}
}
} - (BOOL)sendtoWithSockfd:(int)sockfd Buffer:(char *)buffer size:(int)size addr:(in_addr_t)addr
{
struct sockaddr_in clientaddr;
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htons();
clientaddr.sin_addr.s_addr = addr; long sendSize = ;
while (sendSize < size) {
long retSize = sendto(sockfd, buffer+sendSize, size-sendSize, , (struct sockaddr*)&clientaddr, sizeof(clientaddr));
if (retSize <= ) {
continue;
}
sendSize += retSize;
if (sendSize >= size) {
// 发送成功
return true;
}
} return true;
} int clientSockfd = -;
/**
发送广播
*/
- (void)searchUDPServer
{
// 第一步:打开套节字描述
clientSockfd = socket(AF_INET, SOCK_DGRAM, );// 协议族、数据报、0
if(clientSockfd < )
{
NSLog(@"error:打开套节字描述符失败socket()");
}
NSLog(@"打开套节字描述sockfd:%d",clientSockfd);
// 第二步:设置广播包
int clientOpt;
if ((setsockopt(clientSockfd, SOL_SOCKET, SO_BROADCAST, &clientOpt, sizeof(int)))< ) {
NSLog(@"error:广播包setsockopt");
} if ([self selectSockfd:clientSockfd]) {
CC_searchBrodcastHeader header;
memset(&header, , sizeof(header));
header.controlMask = udp_broadcast_request;
header.protocolHeader[] = 'C';
header.protocolHeader[] = 'C'; char *buf = (char *)malloc(sizeof(header));
memcpy(buf, &header, sizeof(header)); if ([self sendtoWithSockfd:clientSockfd Buffer:buf size:sizeof(header) addr:INADDR_BROADCAST]) {
NSLog(@"=====广播发送成功");
}
} close(clientSockfd);
clientSockfd = -;
} - (BOOL)selectSockfd:(int)sockfd
{
fd_set w_set;
struct timeval tmval;
tmval.tv_sec = ;
tmval.tv_usec = ; FD_ZERO(&w_set); // 将指定的文件描述符集清空
FD_SET(sockfd,&w_set); // 用于在文件描述符集合中增加一个新的文件描述符
int ret =select(sockfd+,NULL, &w_set, NULL,&tmval);
if (ret <= ) {
NSLog(@"<= 0 error:select errorcode:%zi",ret);
return NO;
}
if (FD_ISSET(sockfd, &w_set)) {
return YES;
}
return NO;
} - (NSString *)getIPAddress
{
NSArray *searchArray = @[@"en1/ipv4",@"en0/ipv4"]; NSDictionary *addresses = [self getIPAddresses];
NSLog(@"addresses: %@", addresses); __block NSString *address;
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
{
address = addresses[key];
if(address) *stop = YES;
} ];
return address ? address : @"0.0.0.0";
}
- (NSDictionary *)getIPAddresses
{
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:]; // retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
struct ifaddrs *interface;
for(interface=interfaces; interface; interface=interface->ifa_next) {
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
continue; // deeply nested code harder to read
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
NSString *type;
if(addr->sin_family == AF_INET) {
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type = @"ipv4";
}
} else {
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
type = @"ipv6";
}
}
if(type) {
NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
// Free memory
freeifaddrs(interfaces);
}
return [addresses count] ? addresses : nil;
}
@end

socket UDP简单通讯的更多相关文章

  1. socket TCP简单通讯

    socket 服务器 // // main.m // socket_server // // Created by lujunjie on 2016/11/23. // Copyright © 201 ...

  2. C++ 利用socket实现TCP,UDP网络通讯

    学习孙鑫老师的vc++深入浅出,有一段时间了,第一次接触socket说实话有点儿看不懂,第一次基本上是看他说一句我写一句完成的,第二次在看SOCKET多少有点儿感觉了,接下来我把利用SOCKET完成T ...

  3. socket.io简单说明及在线抽奖demo

    socket.io简单说明及在线抽奖demo socket.io 简介 Socket.IO可以实现实时双向的基于事件的通信. 它适用于各种平台,浏览器或设备,也同样注重可靠性和速度. socket.i ...

  4. Nginx学习笔记(四) 源码分析&socket/UDP/shmem

    源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_socket.h&Ngx_s ...

  5. socket.io简单入门(一.实现简单的图表推送)

    引子:随着nodejs蓬勃发展,虽然主要业务系统因为架构健壮性不会选择nodejs座位应用服务器.但是大量的内部系统却可以使用nodejs试水,大量的前端开发人员转入全堆开发也是一个因素. 研究本例主 ...

  6. Winform MDI窗体容器、权限、简单通讯

    MDI窗体容器: 一般来说,窗体是顶级容器,不允许放在其他任何容器内,但是如果将某个窗体的IsMdiContainer属性设置为True,那此窗体就会成为窗体容器,可以在其中放入其他窗体 在内部的窗体 ...

  7. Winform MDI窗体容器,权限以及简单通讯

    MDI窗体容器: 一般来说,窗体是顶级容器,不允许放在其他任何容器内,但是如果将某个窗体的IsMdiContainer属性设置为True,那此窗体就会成为窗体容器,可以在其中放入其他窗体 在内部的窗体 ...

  8. Winform MDI窗体容器 权限 简单通讯

    MDI窗体容器 权限  using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...

  9. 重新想象 Windows 8 Store Apps (62) - 通信: Socket TCP, Socket UDP

    [源码下载] 重新想象 Windows 8 Store Apps (62) - 通信: Socket TCP, Socket UDP 作者:webabcd 介绍重新想象 Windows 8 Store ...

随机推荐

  1. BZOJ1194: [HNOI2006]潘多拉的盒子(tarjan)

    Description 传说中,有个神奇的潘多拉宝盒.如果谁能打开,便可以拥有幸福.财富.爱情.可是直到真的打开,才发现与之 相随的还有灾难.不幸.其实,在潘多拉制造这个宝盒的时候,设置了一些咒语来封 ...

  2. 洛谷 P2117 小Z的矩阵

    P2117 小Z的矩阵 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1,则G(A)等于所有A[i][j]*A[j][i]的和对2取余 ...

  3. 【Swift初见】Swift数组(二)

    在苹果的开发文档中对Array还提供了其它的操作算法: 1.Sort函数: 对数组进行排序.依据指定的排序规则,看以下的代码: var array = [2, 3, 4, 5] array.sort{ ...

  4. OpenCASCADE Face Normals

    OpenCASCADE Face Normals eryar@163.com Abstract. 要显示一个逼真的三维模型,其顶点坐标.顶点法向.纹理坐标这三个信息必不可少.本文主要介绍如何在Open ...

  5. 运行maven项目出现的报错

    java问题:严重: Error configuring application listener of class org.springframework.web.context.Cont 解决方案 ...

  6. 56.ERR! configure error gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.

    Node.js 在安装模块的时候报错,缺少python环境. ERR! configure error gyp ERR! stack Error: Can't find Python executab ...

  7. js --- 递归结构图

    // 用递归 来求 5 的阶乘 // n! = n * (n-1)! // 定义一个函数,用于求 n 的阶乘 function func(n) { ) { ; } // func(n-1) 因为传递的 ...

  8. POJ 2323 贪心

    题意: 思路: 贪 贪 贪 如果当前的c>之前的c+s 那么之前的合适 一直贪下去就好了 //By SiriusRen #include <cstdio> #include < ...

  9. java knowledge record

    javax.accessibility.Accessible       给予private  或者 final 变量可以改变的机会

  10. Codefroces 415B Mashmokh and Tokens

    B. Mashmokh and Tokens time limit per test 1 second memory limit per test 256 megabytes input standa ...