博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS Socket编程-C语言版(UDP)
阅读量:4969 次
发布时间:2019-06-12

本文共 7638 字,大约阅读时间需要 25 分钟。

1. UDP Socket编程先讲一讲UDP编程,因为比TCP要简单多了。首先,我们需要明白UDP是用户数据报协议,英文名为User Datagram Protocol,它是面向无连接的。注意:Socket通信一定有要服务端和客户端。1.1 UDP Socket客户端客户端的工作流程:首先调用socket函数创建一个Socket,然后指定服务端的IP地址和端口号,就可以调用sendto将字符串传送给服务器端,并可以调用recvfrom接收服务器端返回的字符串,最后关闭该socket。笔者这里分成了四步:    第一步:创建socket并配置socket,如服务端ip地址和端口号    第二步:调用sendto发送消息到服务器端    第三步:调用recvfrom接收来自服务器端的消息    第四步:调用close关闭socket1.1.1 客户端的代码实现:- (void)udpClient {  int clientSocketId;  ssize_t len;  socklen_t addrlen;  struct sockaddr_in client_sockaddr;  char buffer[256] = "Hello, server, how are you?";    // 第一步:创建Socket  clientSocketId = socket(AF_INET, SOCK_DGRAM, 0);  if(clientSocketId < 0) {    NSLog(@"creat client socket fail\n");    return;  }    addrlen = sizeof(struct sockaddr_in);  bzero(&client_sockaddr, addrlen);  client_sockaddr.sin_family = AF_INET;  client_sockaddr.sin_addr.s_addr = inet_addr("192.168.1.107");  client_sockaddr.sin_port = htons(1024);    int count = 10;  do {    bzero(buffer, sizeof(buffer));    sprintf(buffer, "%s", "Hello, server, how are you?");        // 第二步:发送消息到服务端    // 注意:UDP是面向无连接的,因此不用调用connect()    // 将字符串传送给server端   len = sendto(clientSocketId, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_sockaddr, addrlen);        if (len > 0) {      NSLog(@"发送成功");    } else {      NSLog(@"发送失败");    }        // 第三步:接收来自服务端返回的消息    // 接收server端返回的字符串    bzero(buffer, sizeof(buffer));    len = recvfrom(clientSocketId, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_sockaddr, &addrlen);    NSLog(@"receive message from server: %s", buffer);        count--;  } while (count >= 0);    // 第四步:关闭socket  // 由于是面向无连接的,消息发出处就可以了,不用管它收不收得到,发完就可以关闭了  close(clientSocketId);}1.1.2 客户端的打印日志2017-12-06 15:38:36.095 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.286 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.286 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.291 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.291 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.296 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.296 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.316 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.317 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.324 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.324 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.328 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.329 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.339 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.339 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.355 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.356 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.366 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.366 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.372 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?2017-12-06 15:38:36.373 iOS-Socket-C-Version-Client[9709:4234848] 发送成功2017-12-06 15:38:36.392 iOS-Socket-C-Version-Client[9709:4234848] receive message from server: Hello, server, how are you?1.2 UDP Socket服务器端服务器端的工作流程:首先调用socket函数创建一个套接字,然后调用bind函数将其与本机地址以及一个本地端口号绑定,接收到一个客户端时,服务器显示该客户端的IP地址,并将字串返回给客户端。笔者这里分成了五步:    第一步:创建socket并配置socket    第二步:调用bind绑定服务器本机ip及端口号    第三步:调用recvfrom接收来自客户端的消息    第四步:调用sendto将接收到服务器端的信息返回给客户端    第四步:调用close关闭socket1.2.1 服务器端代码实现- (void)udpServer {  int serverSockerId = -1;  ssize_t len = -1;  socklen_t addrlen;  char buff[1024];  struct sockaddr_in ser_addr;    // 第一步:创建socket  // 注意,第二个参数是SOCK_DGRAM,因为udp是数据报格式的  serverSockerId = socket(AF_INET, SOCK_DGRAM, 0);    if(serverSockerId < 0) {    NSLog(@"Create server socket fail");    return;  }    addrlen = sizeof(struct sockaddr_in);  bzero(&ser_addr, addrlen);    ser_addr.sin_family = AF_INET;  ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);  ser_addr.sin_port = htons(1024);    // 第二步:绑定端口号  if(bind(serverSockerId, (struct sockaddr *)&ser_addr, addrlen) < 0) {    NSLog(@"server connect socket fail");    return;  }    do {    bzero(buff, sizeof(buff));        // 第三步:接收客户端的消息    len = recvfrom(serverSockerId, buff, sizeof(buff), 0, (struct sockaddr *)&ser_addr, &addrlen);    // 显示client端的网络地址    NSLog(@"receive from %s\n", inet_ntoa(ser_addr.sin_addr));    // 显示客户端发来的字符串    NSLog(@"recevce:%s", buff);        // 第四步:将接收到的客户端发来的消息,发回客户端    // 将字串返回给client端    sendto(serverSockerId, buff, len, 0, (struct sockaddr *)&ser_addr, addrlen);  } while (strcmp(buff, "exit") != 0);    // 第五步:关闭socket  close(serverSockerId);}1.2.2 服务器端的打印日志2017-12-06 15:38:36.268 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.269 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.372 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.372 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.377 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.377 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.382 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.382 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.405 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.405 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.409 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.410 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.414 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.415 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.425 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.426 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.441 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.441 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.452 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.452 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?2017-12-06 15:38:36.472 iOS-Socket-C-Version-Server[39130:2473780] receive from 192.168.1.1002017-12-06 15:38:36.473 iOS-Socket-C-Version-Server[39130:2473780] recevce:Hello, server, how are you?我们这里打印出了客户端发来的消息,由于上面实现的代码中,只发10次,所以这里只有10条。参考:https://github.com/CoderJackyHuang/iOS-Socket-C-Version注意:这里面有两个工程,一个是客户端,一个是服务器端。运行时,先运行服务器端,然后再选择客户端。另外,客户端所指定的服务器端的ip地址一定要修改成您本机对应的ip,不然使用笔者这里的ip就会失败。

 

转载于:https://www.cnblogs.com/Free-Thinker/p/10442400.html

你可能感兴趣的文章
Python计算pi及其进度条显示
查看>>
News about MapXtreme 2005 for Windows Evaluation (V6.6) Beta 2
查看>>
AlertDialog(自定义对话框)示例
查看>>
DB_OWNER或SA权限备份bat到启动项提权
查看>>
svn提交后利用hooks自动更新web服务器
查看>>
ICE系列之2——ICE的服务与好处
查看>>
Google影像下载自动拼接校正
查看>>
LeetCode OJ 73. Set Matrix Zeroes
查看>>
LeetCode 2. Add Two Numbers
查看>>
MCMC 破译密码 http://mlwhiz.com/blog/2015/08/21/MCMC_Algorithms_Cryptography/
查看>>
Laplacian eigenmap 拉普拉斯特征映射
查看>>
wordpress-技术博客主题推荐
查看>>
SharePoint 2010 PowerShell 系列 之 准备工作
查看>>
分布式系统ID生成办法
查看>>
OpenCL
查看>>
iphone ios XCode4如何调试程序忽然崩溃而找不到挂的代码
查看>>
MD5加密文件
查看>>
QT中QPainterPath类的功能和使用方法
查看>>
通过ftp模拟网盘
查看>>
ruby 状态转移
查看>>