Winsock C TCP 套接字

发布于 2024-12-22 00:45:15 字数 285 浏览 0 评论 0原文

我之前在 Python 中使用过 TCP 套接字。它在 C 中看起来非常相似,但我无法让任何东西工作。 socket(AF_INET, SOCK_STREAM, 0); 返回 -1,这当然表示错误。我怎么会这么快就出错了呢? 如果你能帮助我解决这个问题,那就太好了,但如果你能为我提供一些简单的、简单的源代码,那将会非常有帮助。它甚至不需要做任何事情,也不需要处理错误。我只需要了解如何正确创建服务器套接字、绑定它、侦听它并接受客户端,以及如何创建和连接客户端套接字。我可以自己弄清楚所有的花里胡哨的事情。

谢谢!

I've worked with TCP sockets before in Python. It looks pretty similar in C but I can't get anything to work. socket(AF_INET, SOCK_STREAM, 0); returns -1, which of course indicates an error. How could I go so wrong so fast?
If you could help me with this problem, that would be nice, but it would be incredibly helpful if you could provide me with some simple, bare bones source code. It doesn't need to even do anything really, and it doesn't need to handle errors. I just need to see how to properly create a server socket, bind it, listen on it, and accept clients and how to create and connect a client socket. I can figure out all the bells and whistles on my own.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绾颜 2024-12-29 00:45:15

Have you called WSAStartup before making any other winsock calls?

千笙结 2024-12-29 00:45:15

您需要使用 WSAStartup< 初始化 WinSock /code>函数之后才能使用套接字。 Windows 上的 Python 套接字实现可能会自动调用此函数,因此您无需担心它,但是,当直接使用 WinSock 时,在任何其他 WinSock 调用之前以及当您的程序运行时,调用 WSAStartup 非常重要。完成套接字后,您需要调用WSACleanup

WSAData data;

if (WSAStartup(MAKEWORD(2, 2), &data) != 0)
{
    // unable to initialise WinSock, time to quit
}

// WinSock has been successfully initialised, time to make sockets!
int s = socket(...);

// After all WinSock stuff is done, balance out your WSAStartup with a cleanup:
WSACleanup();

You need to initialise WinSock with the WSAStartup function before you can use sockets. Python's implementation of sockets on Windows likely calls this automatically so you don't need to worry about it, however when using WinSock directly it is important to call WSAStartup before any other WinSock calls, and when your program is done with sockets, you need to call WSACleanup.

WSAData data;

if (WSAStartup(MAKEWORD(2, 2), &data) != 0)
{
    // unable to initialise WinSock, time to quit
}

// WinSock has been successfully initialised, time to make sockets!
int s = socket(...);

// After all WinSock stuff is done, balance out your WSAStartup with a cleanup:
WSACleanup();
烙印 2024-12-29 00:45:15

可能只是该进程没有创建套接字的权限(errno == EACCES)。

也许您的 python 解释器正在获取不同的安全上下文,请检查一下。

不管怎样,安全总比后悔好,所以写上类似这样的内容:

sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
        perror("myapp");
        exit(1);
}

如果 Winsock 实际上设置了 errno,则 IDK,但它应该...

Probably it's just that the process has no permission to create sockets (errno == EACCES).

Perhaps your python interpreter is getting a different security context, check that.

Anyway, better safe than sorry, so put there something like:

sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
        perror("myapp");
        exit(1);
}

IDK if Winsock actually sets errno, but it should...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文