美文网首页锻炼吃饭的家伙ios开发整理
2018年最新 底层Socket通讯,刷新你对Socket不一样

2018年最新 底层Socket通讯,刷新你对Socket不一样

作者: iOS雯Ping | 来源:发表于2018-03-25 22:17 被阅读597次

作为一个开发者,有一个学习的氛围跟一个交流圈子特别重要这是一个我的iOS交流群:687528266,点击链接加入群聊【iOS软件开发学习交流】:https://jq.qq.com/?_wv=1027&k=5wrTEZx不管你是小白还是大牛欢迎入驻 ,分享BAT,阿里面试题、面试经验,讨论技术, 大家一起交流学习成长!

Socket开发——导入头文件

Socket 开发的相关文档可以查阅百度百科

http://baike.baidu.com

#import<sys/socket.h>

#import<netinet/in.h>

#import<arpa/inet.h>

Socket开发——socket

/**

参数

domain:    协议域,AF_INET

type:      Socket 类型,SOCK_STREAM/SOCK_DGRAM

protocol: IPPROTO_TCP

返回值

socket

*/

int clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

Socket开发——connect

/**

参数

1> 客户端socket

2> 指向数据结构sockaddr的指针,其中包括目的端口和IP地址

3> 结构体数据长度

返回值

0 成功/其他 错误代号

*/

struct sockaddr_in serverAddr;

serverAddr.sin_family = AF_INET;

serverAddr.sin_port = htons(12345);

serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

int connectResult = connect(clientSocket, (const struct sockaddr *)&serverAddr, sizeof(serverAddr));

Socket开发——Netcat

$ nc -lk 12345

始终监听本地计算机12345端口的数据

Netcat

是终端下用于调试和检查网络的工具包

可用于创建 TCP/IP 连接

Socket开发——send

/**

参数

1> 客户端socket

2> 发送内容地址

3> 发送内容长度

4> 发送方式标志,一般为0

返回值

如果成功,则返回发送的字节数,失败则返回SOCKET_ERROR

*/

const char *sendMessage = "hello";

ssize_t sendLen = send(clientSocket, sendMessage, strlen(sendMessage), 0);

Socket开发——recv

/**

参数

1> 客户端socket

2> 接收内容缓冲区地址

3> 接收内容缓存区长度

4> 接收方式,0表示阻塞,必须等待服务器返回数据

返回值

如果成功,则返回读入的字节数,失败则返回SOCKET_ERROR

*/

uint8_t buffer[1024];

ssize_t recvLen = recv(clientSocket, &buffer, sizeof(buffer), 0);

NSData *data = [NSData dataWithBytes:buffer length:recvLen];

NSString *recvMessage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Socket开发——close

// 关闭socket

close(clientSocket);

Socket通讯UITests

#import

@interface _02__Socket__UITests : XCTestCase

@end

@implementation _02__Socket__UITests

- (void)setUp {

    [super setUp];

    // Put setup code here. This method is called before the invocation of each test method in the class.

    // In UI tests it is usually best to stop immediately when a failure occurs.

    self.continueAfterFailure = NO;

    // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.

    [[[XCUIApplication alloc] init] launch];

    // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.

}

- (void)tearDown {

    // Put teardown code here. This method is called after the invocation of each test method in the class.

    [super tearDown];

}

- (void)testExample {

    // Use recording to get started writing UI tests.

    // Use XCTAssert and related functions to verify your tests produce the correct results.

}

@end

Socket通讯ViewController.m

#import "ViewController.h"

#import

#import

#import

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self socketDemo];

}

#pragma mark --

-(void)socketDemo

{

    //1.创建一个socket

    /*

     domain: 协议域 IPV4 ,IPV6

     type: socket 类型  SOCKET_STREAM / SOCK_DGRAM

     protocol: TCP ? UDP ? 0 ,自动根据第二个参数来决定合适的协议

     */

     int clientSocket =  socket(AF_INET, SOCK_STREAM, 0);

    //2.连接服务器

    struct sockaddr_in  severAddr;

    severAddr.sin_family = AF_INET;

    //端口

    severAddr.sin_port = htons(12345);

    //地址

    severAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

今天给大家的分享就到这吧!有收获,或者喜欢小编的可以关注加小编的微信Pingwen20以及QQ:3366458405相互学习!大家一起交流成长!!

相关文章

网友评论

    本文标题:2018年最新 底层Socket通讯,刷新你对Socket不一样

    本文链接:https://www.haomeiwen.com/subject/jlvkcftx.html