English 中文(简体)
套: C++的Bad filescriptor
原标题:setsockopt: Bad file descriptor in C++

我有著名的错误:“address”已经在使用,因为我没有检查约束职能。

我的守则是:

memset(&(this->serv_addr), 0, sizeof(this->serv_addr));
this->serv_addr.sin_family = AF_INET;
this->serv_addr.sin_port = htons(port);
this->serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int yes = 1;
if (setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
    perror("setsockopt");
    exit(1);
iii

But running code I got this: setsockopt: Bad file descriptor

The code is right, from the guide Beejnet. But why I got the error? Maybe the position of the code is wrong?

The first the sock_fd is at the function w_socket:

int retv;
retv = socket(AF_INET, SOCK_STREAM, 0);
if(retv == -1)
{
    std::string err_msg(strerror(errno));
    err_msg = "[socket] " + err_msg;
    throw err_msg;
iii
else
{
    int reuse_opt = 1;

    setsockopt(this->sock_fd, SOL_SOCKET, SO_REUSEADDR, &reuse_opt, sizeof(int));
    return retv;
iii

iii

By default there s the sesockopt but no check. I ve tried but it doesn t work.

最佳回答

页: 1 呼吁:

sock_fd = socket(PF_INET, SOCK_STREAM, 0);

(详情见man 2 socket)

只有到那时,才能打电话到<条码>。 电话socket之前,sock_fd 变数将包含随机价值(或0)而不是记票记录。

www.un.org/Depts/DGACM/index_spanish.htm Edit after updated question:

您对<代码>setsockopt的号召需要使用retv而不是this->sock_fd,截至当时,this->sock_fdps>,尚未载有您对socket/code>的呼吁的结果。

问题回答

暂无回答




相关问题
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?