我正在开发一个
client-socket
项目,通过 Linux socket
编程将自定义数据类型 (myMail) 从客户端发送到服务器。这是我的客户代码:
struct mail pmail;
connect(clientSocket, (struct sockaddr*)&sa, sizeof(sa));
send(clientSocket, &pmail, sizeof(pmail), 0);
对于我的服务器来说,这是有问题的部分:
int new_socket = .....;
char buffer[1024] = { 0 };
valread = read(new_socket, buffer, 1024 - 1);
strut mail pmail;
pmail = &buffer;
问题是编译后显示
operand types are ‘mail’ and ‘char (*)[1024]’
。所以,我想知道是否有任何方法可以让我将变量 pmail 的内容分配给缓冲区?或者我必须尝试其他方法?
将
buffer
投射到 struct mail
的指针:
int new_socket = .....;
char buffer[1024] = { 0 };
valread = read(new_socket, buffer, 1024 - 1);
if (valread)
struct mail* pmail = (struct mail*)buffer;
或者简单地说:
int new_socket = .....;
struct mail pmail;
valread = read(new_socket, &pmail, sizeof(struct mail));