| |

VerySource

 Forgot password?
 Register
Search
View: 677|Reply: 1

About the network!

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-12 11:00:01
| Show all posts |Read mode
Here is why the connect function of this program has not been successful?
I suspect that the parameter struct sockaddr_in sa is passed incorrectly, which brother can help me a little. It's pressed tightly.
BOOL IP100ComSendToNet (const unsigned char * IpAddr, const unsigned char * DataBuf)
{
    struct sockaddr_in sa;
   unsigned int ret;
   SOCKET socketId;
    int status;
Ranch
    IP100ComIpAdrStringToData (IpAddr,&ret);
Ranch
    sa.sin_family = AF_INET;
    sa.sin_port = htons (9000);

    sa.sin_addr.S_un.S_addr = htons (ret);
Ranch
socketId = socket (AF_INET, SOCK_DGRAM, 0);


if (socketId == ERROR)
        printf ("socket fail:% d\n", ERROR);

if (ret! = ERROR)
{
        if (connect (socketId, (struct sockaddr_in *)&sa, sizeof (sa)) == ERROR)
// if (connect (socketId, (const struct sockaddr FAR *)&sa, sizeof (sa)) == ERROR)
            printf ("send fail:% d\n", ERROR);
else
{
            printf ("send success:% d\n", OK);
            status = send (socketId, DataBuf, strlen (DataBuf), 0); // MSG_DONTWAIT);
}
}
Ranch
close (socketId);

    return OK;
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-8 17:00:02
| Show all posts
sa.sin_addr.S_un.S_addr = htons (ret); there is a problem
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list