# define _WIN32_WINNT 0 x0601
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
# define PORT 7
# define a TIMEOUT 15/* (seconds) */
/*
*========the main========
*/
Int main (int arg c, char * argv [])
{
The SOCKET s.
Unsigned short port=port;
Char * name=argv [0].
Fd_set readfds;
Struct timeval timeout.
Time_t ts;
Int nr.
Int status=EXIT_SUCCESS;
The int value.
Struct addrinfo hints.
Struct addrinfo * results=NULL;
Char portString [10].
The FILE * fq=NULL;
The FILE * fd=NULL;
Int rd;
Int wd;
Int rc;
Int sc;
The timeout. Tv_sec=timeout;
The timeout. Tv_usec=0;
Struct stat st.
Char * pbufs=NULL;
Char * rbuf=NULL;
//int len=0;
If (arg c & lt; 2)
{
Fprintf (stderr, "the Usage: % s [IPv4 or IPv6 addr] [port] \ n", name);
The status=EXIT_FAILURE;
Goto the QUIT;
}
If (arg c & gt; 2)
{
Port=(unsigned short) strtoul (argv [2], NULL, 0).
}
/* the convert the port into a string format in order to pass it to getaddrinfo */
Sprintf (portString, "% d", the port).
Printf (" \ nTesting UDP echo server at % s: % d \ n ", argv [1], the port).
/* the initialize sockets environment */
//socketsStartup ();
WSADATA WSADATA;
WSAStartup (MAKEWORD (1, 1), & amp; WsaData);
Memset (& amp; Hints, 0, sizeof (hints));
Hints. Ai_family=AF_UNSPEC;
Hints. Ai_socktype=SOCK_DGRAM;
Hints. Ai_protocol=IPPROTO_UDP;
/*
* getaddrinfo () fills in the results struct for us appropriately
* depending on been the IP address is the v4 or v6
*
* argv [1]=IPv4 or IPv6 address passed in from the command line
* argv [2]=port number passed in from the command line
*/
Value=https://bbs.csdn.net/topics/getaddrinfo (argv [1], portString, & hints, & results);
If (the value!=0)
{
Fprintf (stderr, "getaddrinfo failed: % d \ n", value);
If (value=https://bbs.csdn.net/topics/=- 2 | | value==11004)
{
Fprintf (stderr, "unrecognized IP address \ n");
}
The status=EXIT_FAILURE;
Goto the QUIT;
}
Printf (" succeed to get the info of the host \ n ");
/* create a socket. The ai_family determined for us via getaddrinfo () call */
If ((s=socket (results - & gt; Ai_family results - & gt; Ai_socktype, 0))==INVALID_SOCKET)
{
Fprintf (stderr, "% s: failed socket \ n", name);
Printf (" Could not create a socket: % d ", WSAGetLastError ());
The status=EXIT_FAILURE;
Goto the QUIT;
}
Printf (" succeed to create the socket \ n ");
Int opt=1;
The setsockopt (s, SOL_SOCKET, SO_REUSEADDR, & amp; Opt, sizeof (opt));
//send bufffer
Int nSendBuf=8 * 1024;//set the as 32 k
The setsockopt (s, SOL_SOCKET, SO_SNDBUF, (const char *) & amp; NSendBuf, sizeof (int));
//the receive buffer
Int nRecvBuf=8 * 1024;//set the as 32 k
The setsockopt (s, SOL_SOCKET, SO_RCVBUF, (const char *) & amp; NRecvBuf, sizeof (int));
//set the timeout of sending and identifiers
The setsockopt (s, SOL_SOCKET, SO_SNDTIMEO, & amp; The timeout, sizeof (timeout));
The setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, & amp; The timeout, sizeof (timeout));
If ((fq=fopen (" C:/Users/Administrator/Desktop/zyc2. JPG ", "rb"))==NULL)
{
Perror (" file error ");
The status=EXIT_FAILURE;
Goto the QUIT;
}
Printf (" succeed to open the file \ n ");
//stat (" C:/Users/Administrator/Desktop/zyc2. JPG ", & amp; St);/* attain the size of the file */
//len=st. st_size;
Fseek (fq, 0, SEEK_END);
Int lSize;
LSize=ftell (fq);
Printf (" the size of the file is % d \ n ", lSize);
The rewind (fq);
//the send and receive buffer
if (! (pbufs=(char *) malloc (sizeof (char) * lSize)))
{
Perror (" failed temp buffer allocation \ n ");
The status=EXIT_FAILURE;
Goto the QUIT;
}
if (! (rbuf=(char *) malloc (sizeof (char) * lSize)))
{
Perror (" failed temp buffer allocation \ n ");
The status=EXIT_FAILURE;
Goto the QUIT;
}
Rd=fread (pbufs, 1, lSize, fq);
//long temin1=ftell (fq);
//printf (" the terminate file: % d \ n ", temin1);
If (rd!=lSize)
{
Perror (" read error "\ n");
Return: printf (" % d ", rd);
The status=EXIT_FAILURE;
Goto the QUIT;
}
Printf (" % d bytes read \ n ", rd);
Printf (" sending \ n ");
Int i_group;
Int i_pakge=512;
I_group=lSize/i_pakge;
Int i_leftpk=lSize - i_pakge * i_group;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull