iOS 真机socket连不上问题
2022/2/27 6:23:02
本文主要是介绍iOS 真机socket连不上问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
https://cloud.tencent.com/developer/article/1597680
需要先提示权限
#define SOCKET_PORT htons(8040) #define IP_ADRR "192.168.1.3" char receive_msg[1024]; char reply_msg[1024]; int connectSocket(){ int server_socket = socket(AF_INET, SOCK_STREAM, 0); // int clientId = socketId; if (server_socket == -1) { NSLog(@"create socket fail"); return -1; } struct sockaddr_in socketAdrr; socketAdrr.sin_family = AF_INET; socketAdrr.sin_port = SOCKET_PORT; /* struct in_addr socketIn_adrr; socketIn_adrr.s_addr = inet_addr(IP_ADRR); //char* ip = [str UTF8String]; //socketIn_adrr.s_addr = inet_addr(ip); socketAdrr.sin_addr = socketIn_adrr; */ socketAdrr.sin_addr.s_addr = inet_addr(IP_ADRR); ///int connect(int sockfd, const struct sockaddr * servaddr, socklen_t addrlen) ///参数一:sockfd(socket描述符) ///参数二:servaddr(socket地址结构体指针) ///参数三:addrlen(socket地址结构体大小) int result = connect(server_socket, (const struct sockaddr *)&socketAdrr, sizeof(socketAdrr)); if(result != 0){ NSLog(@"客户端:连接失败"); return -1; }else{ bzero(reply_msg, 1024); printf("replay:"); //scanf("%s", reply_msg); strcpy(reply_msg,"hello_1234561111------>"); if (send(server_socket, reply_msg, 1024, 0) == -1) { perror("send error!"); return -1; } bzero(receive_msg, 1024); long byte_num = recv(server_socket, receive_msg, 1024, 0); receive_msg[byte_num] = '\0'; printf("server said:%s\n", receive_msg); } close(server_socket); return 0; } -(void)testHttp { NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];//此处修改为自己公司的服务器地址 NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error == nil) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; NSLog(@"%@",dict); } }]; [dataTask resume]; } - (IBAction)Click:(id)sender { [self testHttp]; //self.btn.titleLabel.text = @"hah"; connectSocket(); }
这篇关于iOS 真机socket连不上问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用
- 2025-01-03混合搜索:用LanceDB实现语义和关键词结合的搜索技术(应用于实际项目)
- 2025-01-03停止思考数据管道,开始构建数据平台:介绍Analytics Engineering Framework
- 2025-01-03如果 Azure-Samples/aks-store-demo 使用了 Score 会怎样?
- 2025-01-03Apache Flink概述:实时数据处理的利器
- 2025-01-01使用 SVN合并操作时,怎么解决冲突的情况?-icode9专业技术文章分享