socket TCP简单通讯
socket 服务器
// // main.m // socket_server // // Created by lujunjie on 2016/11/23. // Copyright © 2016年 lujunjie. All rights reserved. // #import <Foundation/Foundation.h> #import <sys/socket.h> #import <netinet/in.h> #import <arpa/inet.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); // 第一步:打开套节字描述 int sockfd = -1; sockfd = socket(AF_INET, SOCK_STREAM, 0);// 协议族、套节字可靠性传输、0 if(sockfd == -1) { NSLog(@"error:打开套节字描述符失败socket()"); return 0; } NSLog(@"打开套节字描述sockfd:%d",sockfd); // 第二步:bind 绑定sockfd和当前电脑的ip地址&端口号 struct sockaddr_in serveraddr; serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(30000); // 5000~655355 serveraddr.sin_addr.s_addr = inet_addr("192.168.1.103"); int ret =bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); if (ret < 0) { NSLog(@"error:bind"); return 0; } NSLog(@"bind-ret:%d",ret); // 第三步:listen监听端口 int listenRet = listen(sockfd, 10);// ..,监听多少个; if (listenRet < 0) { NSLog(@"error:listen"); return 0; } NSLog(@"listen-ret:%d",listenRet); // 第四步:accept阻塞等待客户端接入 struct sockaddr_in clientaddr; socklen_t len = 0; int clientfd = accept(sockfd, (struct sockaddr *)&clientaddr, &len); if (clientfd < 0) { NSLog(@"error:accept"); return 0; } NSLog(@"accept-clientfd:%d",clientfd); NSLog(@"成功建立连接"); // 建立连接之后通信 char recvbuf[100]; memset(recvbuf, 0, sizeof(recvbuf)); // // long recvRet = recv(clientfd, recvbuf, sizeof(recvbuf), 0); // // NSLog(@"成功接收了%ld个字节\n",recvRet); // NSLog(@"client发送过来的内容是:%s",recvbuf); // printf("client发送过来的内容是:%s",recvbuf); while (1) { long recvRet = recv(clientfd, recvbuf, sizeof(recvbuf), 0); NSLog(@"成功接收了%ld个字节\n",recvRet); NSLog(@"NSLogclient发送过来的内容是:%s",recvbuf); memset(recvbuf, 0, sizeof(recvbuf)); } } return 0; }
socket客户端
// // main.m // socket_client_Command // // Created by lujunjie on 2016/11/23. // Copyright © 2016年 lujunjie. All rights reserved. // #import <Foundation/Foundation.h> #import <sys/socket.h> #import <netinet/in.h> #import <arpa/inet.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"socket_client_Command"); // 第一步:打开套节字描述 int sockfd = -1; sockfd = socket(AF_INET, SOCK_STREAM, 0);// 协议族、套节字可靠性、0 if(sockfd == -1) { NSLog(@"error:打开套节字描述符失败socket()"); return 0; } NSLog(@"打开套节字描述sockfd:%d",sockfd); // 第二步:connect 链接服务器 struct sockaddr_in serveraddr; serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(30000); // 5000~655355 serveraddr.sin_addr.s_addr = inet_addr("192.168.1.103"); int ret = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); if (ret < 0) { NSLog(@"error:connect"); return 0; } NSLog(@"connect-ret:%d",ret); NSLog(@"成功建立连接"); char sendbuf[100]; // // strcpy(sendbuf, "cliend:hello world"); // long retstr=send(sockfd, sendbuf,strlen(sendbuf),0); // // NSLog(@"发送了%ld个字符\n",retstr); while (1) { NSLog(@"请输入要发送的内容\n"); scanf("%s",sendbuf); long retstr=send(sockfd, sendbuf,strlen(sendbuf),0); NSLog(@"发送了%ld个字符\n",retstr); } } return 0; }