开发者

result too large c++ sockets

I am getting an error an bind() with the value 34 (Result too large), can anyone help?

 void Connect(string address, unsigned short port){         
            memset(&server2, 0, sizeof(server2));
            server2.sin_family = AF_INET;  
            server2.sin_addr.s_addr = inet_addr(address.c_str());  
            server2.sin_port = htons(port);  

            desc开发者_开发百科2 = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);            
            if(desc2 == -1) {            
                cout << "Error in Socket()" << endl;    
            }

            if((::bind(desc2,(sockaddr*)&server2, sizeof(server2))) == -1) {                        
                cout << "Error in Bind() " <<  errno << endl;
            }

            if((::connect(desc2, (sockaddr*)&server2, sizeof(server2))) > 0) {                      
                cout << "Error in Connect()" << endl;    
            }
            cout << "YOU ARE CONNECTED TO " << address << " ON PORT " << port << endl;              
}

PS: I got this error 1 year ago too,the problem was simple i had write something bad when initializing the socket address, where to connect, but now again i have no clue where I made a mistake.


I don't know what caused your problem exactly to return that type of result.

You said you were a proxy server, so you are listening for incoming connections. Try this:

server2.sin_addr.s_addr = 0;
server2.sin_family = AF_INET;
server2.sin_port = htons(port);

desc2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

// Puts the socket in listening mode, allowing 10 connection requests to queue.
bind(desc2, (sockaddr*)&server2, sizeof(server2));
listen(desc2, 10);

// Accepts the first connection request.
SOCKET accepted_socket = accept(desc2, NULL, NULL);
std::cout << "connection accepted!\n";

You will probably want to learn how to program asynchronously.


I don't know why you're getting 'name too long' -- without seeing how server2 was declared and defined, it's impossible to know for sure.

But I do know that calling bind() and then connect() on the same socket with the same address will fail -- bind() is assigning the local address to the socket, and connect() connects to the remote address. Giving the same address for both ends of the socket can only end badly.

Almost no protocols require bind() before connect(). (The exceptions would involve the "ports lower than 1024 can only be opened by root, so we can trust this connection" style of authentication, which hasn't been used in ages. Think rlogin.)

bind() makes most sense immediately before a listen() call. Clients will attempt to contact a server on a 'well-known port', and bind() is the mechanism you use to assign that name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜