Send more than 1500 data packets

Source: Internet
Author: User
Tags htons
In XP, sendto is used to send more than 1500 data packets (the return value of sendto is normal), but recvfrom is not accepted. sniffer is used to find that the data packet has been successfully sent. So_rcvbuf, so_sndbuf, and so_max_msg_size of the monitoring program are default normal values.

If you send packets smaller than 1500 on XP, everything works!

The same program (data packets larger than 1500 or smaller than 1500) can be normally sent and accepted on win2000.

The source code is as follows:

Server:
# Include <stdlib. h>
# Include <iostream. h>
# Include <stdio. h>
# Include <conio. h>
# Include <winsock2.h>
# Include <windows. h>

# Pragma comment (Lib, "ws2_32.lib ")
Main ()
{
Socket S;
Sockaddr_in addrto;
Wsadata wsdata;
Bool bsocket;
Char * smsg = "this is a test ";
// Start the socket library, version 2.0
Wsastartup (0x0202, & wsdata );

S = wsasocket (af_inet, sock_dgram, 0, null, 0, wsa_flag_overlapped );
Bsocket = true;
// Assign a value to the address to receive messages from the broadcast address on the network;
Addrto. sin_family = af_inet;
Addrto. sin_addr.s_addr = inaddr_broadcast;
Addrto. sin_port = htons (5050 );
Bool opt = true;
// Set the socket to the broadcast type,
Setsockopt (S, sol_socket, so_broadcast, (char far *) & OPT, sizeof (OPT ));
Int nlen = sizeof (addrto );
While (1)
{
Sleep (1000 );
// Send a message from the broadcast address
Int ret = sendto (S, smsg, 1500,0, (sockaddr *) & addrto, nlen );
If (ret = socket_error)
Cout <wsagetlasterror () <Endl;
Else
{
Printf ("OK ");
}
}
}

Client:

# Include <stdlib. h>
# Include <iostream. h>
# Include <conio. h>
# Include <stdio. h>
# Include <winsock2.h>
# Include <windows. h>
# Pragma comment (Lib, "ws2_32.lib ")

Void main (void)
{
Socket S;
Sockaddr_in from,;
Wsadata wsdata;
Bool optval;
// Start the socket library, version 2.0
Wsastartup (0x0202, & wsdata );
Optval = true;
// Then assign two addresses, one for binding a socket and the other for receiving messages from the broadcast address on the network;
A. sin_family = af_inet;
A. sin_addr.s_addr = 0;
A. sin_port = htons (5050 );

From. sin_family = af_inet;
From. sin_addr.s_addr = inaddr_broadcast;
From. sin_port = htons (5050 );

Int fromlength = sizeof (sockaddr );
// Use UDP to initialize the socket
S = socket (af_inet, sock_dgram, 0 );
// Set the socket to the broadcast type,
Setsockopt (S, sol_socket, so_broadcast, (char far *) & optval, sizeof (optval ));
BIND (S, (sockaddr *) & A, sizeof (sockaddr_in ));
Char Buf [1600];
While (1)
{// Receives messages from the broadcast address. Note that the address used for binding is different from the address used for receiving messages.
Recvfrom (S, Buf, 1600,0, (struct sockaddr far *) & from, (INT far *) & fromlength );
Sleep (2000 );
Printf ("% s/n", Buf );
Zeromemory (BUF, 1600 );
}
}

The test code is normal in 2000, but not in xp. If you change the sendto packet to 1400, everything is normal!

Thank you for your advice ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.