ret = recv(SocketClient, buffer , need, 0);
时间: 2024-01-14 21:05:21 浏览: 98
这是一个使用套接字接收数据的代码行,其中:
- `SocketClient`是一个已连接的套接字;
- `buffer`是一个指向接收数据的缓冲区的指针;
- `need`是需要接收的数据大小;
- `0`表示接收数据时不使用任何特殊选项。
函数的返回值是接收到的数据大小,保存在变量`ret`中。
相关问题
解读这段代码 int32_t ssl_recv_section(ssl_link_t *p_link, void *p_buf) { int ret = LINK_RECV_SECTION_RET_FAIL, _ret; mem_item_t *p_item = (mem_item_t *)p_buf; memory_uint_t *p_data = p_item->mem.pdata; switch (p_link->sectionStep) { case SSL_LINK_RECV_SECTION_START: p_link->surplusLen = sizeof(record_head_t); p_data = memunit_fetch(&(p_item->mem)); p_item->mem.pdata = p_data; p_link->sectionStep = SSL_LINK_RECV_SECTION_HEAD; case SSL_LINK_RECV_SECTION_HEAD: _ret = _recv_head(p_link, p_data); if (_ret == 0) { ret = LINK_RECV_SECTION_RET_ING; break; } else if (_ret == 2) { break; } //为1时跳到数据段接收 case SSL_LINK_RECV_SECTION_DATA: _ret = _recv_data(p_link, p_data); if (_ret == 0) { ret = LINK_RECV_SECTION_RET_ING; break; } else if (_ret == 1) { ret = LINK_RECV_SECTION_RET_OK; } break; default: break; } if (ret == LINK_RECV_SECTION_RET_FAIL) { DBG("fd:%d recv sectionStep:%d surplusLen:%d datalen:%d\n",p_link->fd, p_link->sectionStep,p_link->surplusLen,mem_datalen(p_data)); p_link->sectionStep = SSL_LINK_RECV_SECTION_START; } return ret; }
这段代码是一个函数,名为ssl_recv_section,接收一个ssl_link_t类型的指针p_link和一个void类型的指针p_buf作为参数,返回一个int32_t类型的值。该函数的作用是接收SSL连接中的数据段。
函数内部首先定义了一个int类型的变量ret,并将其初始化为LINK_RECV_SECTION_RET_FAIL。同时,定义了一个mem_item_t类型的指针变量p_item,将p_buf强制转换为mem_item_t类型后赋值给p_item。再定义一个memory_uint_t类型的指针变量p_data,将p_item->mem.pdata的值赋给p_data。
接下来是一个switch语句,根据p_link->sectionStep的值进行不同的操作。如果为SSL_LINK_RECV_SECTION_START,则将surplusLen赋值为sizeof(record_head_t),从内存池中获取数据并赋值给p_data,将p_data赋值给p_item->mem.pdata,然后将sectionStep赋值为SSL_LINK_RECV_SECTION_HEAD。如果为SSL_LINK_RECV_SECTION_HEAD,则调用函数_recv_head,将p_link和p_data作为参数传递给该函数,并将返回值赋给_ret。如果_ret等于0,则将ret赋值为LINK_RECV_SECTION_RET_ING,并跳出switch语句;如果_ret等于2,则直接跳出switch语句,否则执行case的下一条语句。如果为SSL_LINK_RECV_SECTION_DATA,则调用函数_recv_data,将p_link和p_data作为参数传递给该函数,并将返回值赋给_ret。如果_ret等于0,则将ret赋值为LINK_RECV_SECTION_RET_ING,并跳出switch语句;如果_ret等于1,则将ret赋值为LINK_RECV_SECTION_RET_OK。
最后,如果ret等于LINK_RECV_SECTION_RET_FAIL,则打印出一些调试信息,并将sectionStep赋值为SSL_LINK_RECV_SECTION_START。最终返回ret。
修改client.c: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> int main() { int ret = 0; int socket_fd; char buffer[100]="你好,我是xxx!"; struct sockaddr_in server_add; socket_fd = socket(AF_INET, SOCK_STREAM, 0); if(-1 == socket_fd) { printf("socket fail ! \r\n"); return -1; } bzero(&server_add,sizeof(struct sockaddr_in)); server_add.sin_family=AF_INET; server_add.sin_addr.s_addr= inet_addr("192.168.1.2"); server_add.sin_port=htons(8888); if(-1 == connect(socket_fd,(struct sockaddr *)(&server_add), sizeof(struct sockaddr))) { printf("connect fail !\r\n"); return -1; } ret = write(socket_fd, buffer, strlen(buffer)); if(-1 == ret ) { printf("write fail!\r\n"); return -1; } close(socket_fd); return 0; }server.c: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> int main() { int ret = 0; int socket_fd, new_socket_fd; struct sockaddr_in server_add,client_add; int add_len; char recv_buff[100]={0}; char send_buff[100]="朕xxx知道了!"; socket_fd = socket(AF_INET, SOCK_STREAM, 0); if(-1 == socket_fd) { printf("socket fail ! \r\n"); return -1; } bzero(&server_add,sizeof(struct sockaddr_in)); server_add.sin_family=AF_INET; server_add.sin_addr.s_addr=htonl(INADDR_ANY); server_add.sin_port=htons(8888); if(-1 == bind(socket_fd,(struct sockaddr *)(&server_add), sizeof(struct sockaddr))) { printf("bind fail !\r\n"); return -1; } if(-1 == listen(socket_fd,5)) { printf("listen fail !\r\n"); return -1; } while(1) { new_socket_fd = accept(socket_fd, (struct sockaddr *)(&client_add), &add_len); if(-1 == new_socket_fd) { printf("accept fail !\r\n"); return -1; } ret = read(new_socket_fd, recv_buff,100); if(-1 == ret) { printf("read data fail !\r\n"); return -1; } printf("Now we get:%s from client!\n", recv_buff); ret = write(socket_fd, send_buff, strlen(send_buff)); if(-1 == ret ) { printf("write fail!\r\n"); return -1; } close(new_socket_fd); } close(socket_fd); return 0; }修改服务器和客户端,使朕知道了正常输出
修改后的client.c:
```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main() {
int ret = 0;
int socket_fd;
char buffer[100]="你好,我是xxx!";
struct sockaddr_in server_add;
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == socket_fd)
{
printf("socket fail ! \r\n");
return -1;
}
bzero(&server_add,sizeof(struct sockaddr_in));
server_add.sin_family=AF_INET;
server_add.sin_addr.s_addr= inet_addr("192.168.1.2");
server_add.sin_port=htons(8888);
if(-1 == connect(socket_fd,(struct sockaddr *)(&server_add), sizeof(struct sockaddr)))
{
printf("connect fail !\r\n");
return -1;
}
ret = write(socket_fd, buffer, strlen(buffer));
if(-1 == ret )
{
printf("write fail!\r\n");
return -1;
}
ret = read(socket_fd, buffer,100);
if(-1 == ret) {
printf("read data fail !\r\n");
return -1;
}
printf("Now we get:%s from server!\n", buffer);
close(socket_fd);
return 0;
}
```
修改后的server.c:
```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main() {
int ret = 0;
int socket_fd, new_socket_fd;
struct sockaddr_in server_add,client_add;
int add_len;
char recv_buff[100]={0};
char send_buff[100]="朕xxx知道了!";
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == socket_fd) {
printf("socket fail ! \r\n");
return -1;
}
bzero(&server_add,sizeof(struct sockaddr_in));
server_add.sin_family=AF_INET;
server_add.sin_addr.s_addr=htonl(INADDR_ANY);
server_add.sin_port=htons(8888);
if(-1 == bind(socket_fd,(struct sockaddr *)(&server_add), sizeof(struct sockaddr))) {
printf("bind fail !\r\n");
return -1;
}
if(-1 == listen(socket_fd,5)) {
printf("listen fail !\r\n");
return -1;
}
while(1) {
new_socket_fd = accept(socket_fd, (struct sockaddr *)(&client_add), &add_len);
if(-1 == new_socket_fd) {
printf("accept fail !\r\n");
return -1;
}
ret = read(new_socket_fd, recv_buff,100);
if(-1 == ret) {
printf("read data fail !\r\n");
return -1;
}
printf("Now we get:%s from client!\n", recv_buff);
ret = write(new_socket_fd, send_buff, strlen(send_buff));
if(-1 == ret ) {
printf("write fail!\r\n");
return -1;
}
close(new_socket_fd);
}
close(socket_fd);
return 0;
}
```
在客户端发送消息后,服务器会返回“朕xxx知道了!”,客户端会输出“Now we get:朕xxx知道了! from server!”。
阅读全文