问题描述:

Please, this is the first time that I'm trying to program using sockets. I work with C++ MFC and I've tried to make an application to communicate with a video server controller by its TCP/IP address.

I make all the first steps without error. I create a socket, I call the bind() function to associate the address, but when I try to connect it doesn't work.

I don't know if I am doing something wrong, so I put part of my code bellow.

 struct sockaddr_in my_addr;

int lenAddrs;

b1 = 192;

b2 = 168;

b3 = 11;

b4 = 2;

remoteAddress.Format("%u.%u.%u.%u", b1, b2, b3, b4);

port = (u_short)(portNumber);

// my address

memset(&my_addr, 0, sizeof(my_addr));

my_addr.sin_family = AF_INET;

my_addr.sin_port = htons(port);

lenAddrs = sizeof(my_addr);

// remote address

memset(&remote_addr, 0, sizeof(remote_addr));

remote_addr.sin_family = AF_INET;

remote_addr.sin_port = port;

inet_pton(AF_INET, remoteAddress, (&remote_addr.sin_addr));

// socket

sckSloMo = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (sckSloMo == INVALID_SOCKET)

{

AfxMessageBox("sckSloMo is invalid!!");

return;

}

if (sckSloMo != SOCKET_ERROR)

{

ret = bind(sckSloMo, (const sockaddr*) &my_addr, sizeof(my_addr));

if (ret == 0)

{

ret = connect(sckSloMo, (const sockaddr*) &remote_addr, sizeof(remote_addr));

if (ret == 0)

{

AfxMessageBox("connect!");

StartSendingMessages();

}

else

{

AfxMessageBox("connect error!!!");

return;

}

}

}

If anybody could help me, I would be glad.

网友答案:

try this:

struct sockaddr_in remote_addr;
int lenAddrs;

b1 = 192;
b2 = 168;
b3 = 11;
b4 = 2;

remoteAddress.Format("%u.%u.%u.%u", b1, b2, b3, b4);

port = (u_short)(portNumber);

// remote address
memset(&remote_addr, 0, sizeof(remote_addr));
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(port);
inet_pton(AF_INET, remoteAddress, (&remote_addr.sin_addr)); 

// socket 
sckSloMo = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (sckSloMo == INVALID_SOCKET)
{
    AfxMessageBox("sckSloMo is invalid!!");
    return;
}

if (sckSloMo != SOCKET_ERROR)
{
    ret = connect(sckSloMo, (const sockaddr*) &remote_addr, sizeof(remote_addr));
    if (ret == 0)
    {
        AfxMessageBox("connect!");
        StartSendingMessages();
    }
    else
    {
        AfxMessageBox("connect error!!!");
        return;
    }
}

As someone else already, rightly pointed out, the value of the port is not in the right byte order. Also, you do not need to use bind() on client-side sockets. The MSDN docuemntation for connect() has an example that is quite straight forward and easy to follow.

网友答案:

You are mixing things up. You need bind() for incoming sockets, not for outgoing sockets. You need connect for outgoing sockets, not for incoming sockets. So please only use connect, not bind().

You are not initializing remote_addr.sin_addr. This won't work. If you do not explicitly initialize it, then it is still 0.0.0.0 from your memset(,0,). While 0.0.0.0 is perfect for a bind (meaning bind to all network cards), it is not a valid destination address for connect.

Sample code for setting remote_addr.sin_addr:

remote_addr.sin_addr = inet_addr("192.168.11.2");

Also, when putting the port into sockaddr_in, please don't forget to put it in the right byte order. Depending on the value of port you might need to change your code to:

remote_addr.sin_port = htons(port);
相关阅读:
Top