无法在 Windows 上创建套接字 [英] Can't create socket on Windows

查看:61
本文介绍了无法在 Windows 上创建套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很尴尬的问题.以下代码不会在 Windows 上创建套接字;它失败并显示错误消息.谁能简要解释一下为什么会这样?我非常困惑和沮丧,如此简单的事情失败了.谢谢.

I have quite an embarrassing problem. The following code simply will not create a socket on Windows; it fails and displays the error message. Could anyone briefly explain why this might be? I'm incredibly confused and frustrated that something so simple is failing. Thank you.

int sock;
if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
    printf("error opening socket");
}

另外,打印出 strerror(errno) 只是显示没有错误".

Also, printing out strerror(errno) simply shows "No error".

推荐答案

您需要调用 WSAStartup() 在任何其他套接字函数在 Windows 上工作之前(并且您应该调用 WSACleanup() 完成后.

You need to call WSAStartup() before any other socket functions will work on Windows (and you're supposed to call WSACleanup() when you're done).

这篇关于无法在 Windows 上创建套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆