我缺少什么来创建套接字
What am i missing to create socket?
我正试图用C创建一个客户端/服务器,这是我第一次学习C编程,我只想通过本地主机连接,而不是通过互联网连接,我缺少什么。我该如何修复它,让我的服务器启动并运行,以及我应该键入什么来让我的客户端连接到我的服务器。
我需要创建一个可以与客户端进行发送和接收的服务器。对不起,我是C编程的新手,只是在学习阶段,请原谅我缺乏经验,我很想向这里的专家学习。
下面是我在主中的代码
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
using namespace std;
int main()
{
int serverFd;
int clientFd;
struct sockaddr_un serverAddress;
cout << "" << endl;
cout << "Running server program 'css' ...... " << endl;
cout << "" << endl;
// SOCKET CREATION PART - SERVER
serverFd = socket (AF_LOCAL, SOCK_STREAM, 0);
/* Set domain type */
serverAddress.sun_family = AF_LOCAL;
/* Set name */
strcpy (serverAddress.sun_path, "Server");
/* Remove file if it already exists */
unlink ("Server");
/* Create file */
bind (serverFd, serverSockAddrPtr, serverLen);
// SOCKET CREATION END - SERVER
return 0;
}
我得到的错误:
CountryServer.c:39:17: error: ‘serverSockAddrPtr’ was not declared in this scope
CountryServer.c:39:36: error: ‘serverLen’ was not declared in this scope
正如错误所说,您使用的名称(serverSockAddrPtr
和serverLen
)尚未声明。您想要的是serverAddress
结构的地址和大小:
bind (serverFd,
reinterpret_cast<sockaddr const *>(&serverAddress),
sizeof serverAddress);
您缺少一些标头。
根据本手册页,struct sockaddr_un
在sys/un.h
中。
相关文章:
- 如何在CPP中创建应该在Windows和Linux上运行的套接字?
- C++套接字客户端到 Python 服务器未创建连接
- 在C ++ ASIO中,如何为HTTP或HTTPS创建相同的套接字对象(接口/抽象问题?
- 如何在C++中创建特定大小的消息以通过UDP套接字发送?
- 在线程C++中创建一个套接字
- 从 PHP 调用已编译的C++可执行文件来创建套接字
- C++ 套接字编程:创建多个流
- 在新计算机上,ZMQ上下文套接字创建要慢得多
- C 创建多个套接字客户端
- 根据C++代码在 PHP 中创建字节数据并将其传递给套接字
- 正在创建多个TCP套接字连接
- 连接到Boost.Asio创建的域套接字时权限被拒绝
- 我缺少什么来创建套接字
- 当服务器重新启动并且客户端收到WSAECONNRESET错误代码时,我应该重新创建整个套接字吗
- 在具有 DNS 名称时创建套接字
- 如何在同一程序中创建多个套接字
- 在Qt中创建套接字
- s3eSocket Marmalade无法创建套接字
- Linux 系统不允许创建套接字,因为"Too many open files"*即使在重新启动后*
- 运行io_service后创建套接字