一.Socket的功能图示
二.使用Socket完成简单的聊天功能
1.服务端代码
- (void)viewDidLoad {
[super viewDidLoad];
//TCP套接字
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
//判断套接字的创建状态
if (serverSocket == -1) {
NSLog(@"创建socket失败");
}else{
NSLog(@"创建socket成功");
}
//绑定套接字
struct sockaddr_in addr;
addr.sin_family = AF_INET; //IP地址类型
addr.sin_addr.s_addr = inet_addr("127.0.0.1");//IP地址
addr.sin_port = htons(1234);//端口号
int result = bind(serverSocket, (const struct sockaddr*)&addr, sizeof(addr));
//判断绑定状态
if (result == -1) {
NSLog(@"绑定出错");
}else{
NSLog(@"绑定成功");
}
//被动监听
result = listen(serverSocket, 5);
//判断监听是否成功
if (result == -1) {
NSLog(@"监听失败");
}else{
NSLog(@"监听成功");
}
//保存客户端的信息
struct sockaddr_in c_addr;
//保存客户端信息的字节
socklen_t c_addr_len;
//接受数据的缓冲区
char buff[1024];
//死循环持续接受客户端请求
while (1) {
//获得用于和客户端通信的socket
int newSocket = accept(serverSocket, (struct sockaddr *)&c_addr, &c_addr_len);
//判断接受客户端请求是否成功
if (newSocket == -1) {
NSLog(@"接受数据失败");
}else{
//客户端的IP地址为
NSLog(@"开始聊天了:%s",inet_ntoa(c_addr.sin_addr));
//接受数据
size_t length = read(newSocket, buff, 1024);
NSLog(@"接受了%zu字节: %s",length,buff);
//发送数据
char *message = "Hello World";
length = write(newSocket, message, strlen(message));
NSLog(@"成功发送给客户端%zu字节",length);
}
}
}
2.客户端代码
- (void)viewDidLoad {
[super viewDidLoad];
//TCP套接字
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
//判断套接字是否创建成功
if (clientSocket == -1) {
NSLog(@"创建socket失败");
}else{
NSLog(@"创建socket成功");
}
//发起连接
struct sockaddr_in addr;
addr.sin_family = AF_INET;//IP地址类型
addr.sin_addr.s_addr = inet_addr("127.0.0.1");//IP地址
addr.sin_port = htons(1234);//端口号
int result = connect(clientSocket, (const struct sockaddr*)&addr, sizeof(addr));
//判断连接是否成功
if (result == -1) {
NSLog(@"连接出错");
}else{
NSLog(@"连接成功");
}
//接受数据的缓冲区
char buff[1024];
//发送数据
char *message = "Hello World";
ssize_t length = write(clientSocket, message, strlen(message));
NSLog(@"成功发送给客户端%zu字节",length);
//接受数据
length = read(clientSocket, buff, 1024);
NSLog(@"接受了%zu字节: %s",length,buff);
}
3.运行结果
127.0.0.1的另一种形式是16777343
unsigned long ip = inet_addr("127.0.0.1");
printf("%ld\n", ip);
运行结果:16777343
运行服务端打印结果
2019-05-16 20:44:18.021821+0800 Server-服务端[4345:158908] 创建socket成功
2019-05-16 20:44:18.022118+0800 Server-服务端[4345:158908] 绑定成功
2019-05-16 20:44:18.022296+0800 Server-服务端[4345:158908] 监听成功
运行客户端打印结果
2019-05-16 20:44:34.213811+0800 客户端-Client[4362:159468] 创建socket成功
2019-05-16 20:44:34.214072+0800 客户端-Client[4362:159468] 连接成功
2019-05-16 20:44:34.214346+0800 客户端-Client[4362:159468] 成功发送给客户端11字节
2019-05-16 20:44:34.215025+0800 客户端-Client[4362:159468] 接受了11字节: Hello World
运行客户端之后服务端的打印结果
2019-05-16 20:44:18.021821+0800 Server-服务端[4345:158908] 创建socket成功
2019-05-16 20:44:18.022118+0800 Server-服务端[4345:158908] 绑定成功
2019-05-16 20:44:18.022296+0800 Server-服务端[4345:158908] 监听成功
2019-05-16 20:44:34.214103+0800 Server-服务端[4345:158908] 开始聊天了:16777216
2019-05-16 20:44:34.214593+0800 Server-服务端[4345:158908] 接受了11字节: Hello World��
2019-05-16 20:44:34.214843+0800 Server-服务端[4345:158908] 成功发送给客户端11字节
四.代码链接
工程链接:
https://pan.baidu.com/s/1R7ng8v1RFjkYAJRqS0EoRA
密码:0sj6