English 中文(简体)
• 向欧洲复兴开发银行提供自动交换机
原标题:::socket returns 0 and ::connect sets errno to EBADF
  • 时间:2012-05-21 11:15:12
  •  标签:
  • c++
  • sockets

I m 采用BSD的袖珍,我想使用:link,与example.com连接80。 <代码>: 告诉我,它有一份有效的卷宗说明,有一份是<代码>-1。

auto fd = ::socket(AF_INET, SOCK_STREAM, 0);

struct ::sockaddr_in addr;
::bzero(&addr, sizeof(addr));
addr.sin_family = family_;

struct ::hostent* hostent = ::gethostbyname(host.c_str());
::bcopy(hostent->h_addr, &addr.sin_addr.s_addr, hostent->h_length);
addr.sin_port = port;

auto err = ::connect(fd,
                     reinterpret_cast<struct ::sockaddr*>(&addr),
                     sizeof(addr));

fd = 0, so :socket 继承(否则将回到<代码>-1)。 但是,er ==-1errno的编号为EBADF,表示fd为坏账人。

在此可以做些什么? 为什么<代码>:连接 我告诉我,我给它一个坏的描述者,而我显然没有这样做?

最佳回答

您应当确认,<代码>socket 是 actual re 0? 除非你关闭标准档案记录员,否则情况非常罕见。 确保你在接到呼吁后立即检查,如果它受到其他电话的腐蚀。

问题回答

• 将第3条论点转交 明确称为IPPROTO_TCP。

auto fd = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

The 0 value is IPPROTO_IP, which is not what you need.

另一gues:尝试

addr.sin_addr = htons(port);

你可能与无法得到的东西连接(港口不是80个,正如你所期望的那样,而是0x5000=20480)。

第三场审判。 你们正在使用BSD/MacOS、LC/或其他POSIX系统? 还是WinSock? 如果是窗户,则对电离层信号进行检查。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签