如何解决C++ TCP 发送错误:无法解析不存在的文件'/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/send.c'
我一直在尝试用 C++ 制作服务器。代码编译并运行,但函数“send”不起作用并给出如标题所示的错误。完整错误如下所示,来自 VSCode。
无法打开'send.c':无法读取文件'/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/send.c'(错误:无法解析不存在的文件'/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/send.c').
#include <iostream>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
const int PORT = 5000;
const int MAX_CONNECTIONS = 8;
int main(){
const char* openingMessage = "Welcome!\n";
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
int socketS2G = socket(AF_INET,SOCK_STREAM,0); //AF_INET is IPv4,SOCK_STREAM is TCP and Protocol value for Internet Protocol(IP),which is 0.
if(socketS2G == 0){
std::cout << "Something went wrong!\n";
} else {
//bind address to socket objekt
if(bind(socketS2G,(struct sockaddr *)&address,sizeof(address)) < 0){
std::cout << "Could not bind address and port\n";
} else {
std::cout << "Port Now open\n";
//listen to socket
if (listen(socketS2G,3) < 0){
std::cout << "Can't lisent to socket\n";
} else {
std::cout << "Listening on socket...\n";
//accept requests from clients
socklen_t addresslen = sizeof(address);
while(true){
std::cout << "Ready to accept connection\n";
accept(socketS2G,&addresslen);
//THE LINE BELOW IS WHERE THINGS BEGIN TO GO WRONG
send(socketS2G,openingMessage,strlen(openingMessage),0);
}
}
}
}
return 0;
}
该错误仅在我尝试在端口 5000 连接到 127.0.0.1 时发生。我使用 telnet
命令连接到我的应用程序。
我感觉我可能缺少一个库,或者我调用了错误的函数。因此,我更新并升级了我的 Ubuntu 机器。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。