Port description code

Source: Internet
Author: User

Int main ()
{
Wsadata wsdata;
Socket sock = invalid_socket;
Struct sockaddr_in ADDR;

Char IP [16] = {0 };
U_short prot = 16999;
Char data [2048] = {0 };

Int NREL = 0;
Int Index = 0;

U_long mode = 1;
Struct timeval timeout;

Timeout. TV _sec = 0;
Timeout. TV _usec = 10000*6;

Fd_set W;
Fd_set R;

NREL = wsastartup (makeword (2, 2), & wsdata );
If (0! = NREL)
{
Log ("wsastartup failed! \ N ");
Getchar ();
Return 0;
}
Else
{
Log ("wsastartup successful! \ N ");
}

For (Index = 0; index <255; index ++)
{
Sprintf (IP, "192.168.1.% d", index );

Sock = socket (af_inet, sock_stream, ipproto_tcp );
If (sock! = Invalid_socket)
{
Outputdebugstring ("socket create successful! \ N ");
}

NREL = ioctlsocket (sock, fionbio, & mode );
If (NREL = socket_error)
{
Log ("ioctlsocket failed! \ N ");
}

Fd_zero (& W );
Fd_set (sock, & W );
Fd_zero (& R );
Fd_set (sock, & R );

ADDR. sin_family = af_inet;
ADDR. sin_port = htons (prot );
ADDR. sin_addr.s_addr = inet_addr (IP );

NREL = connect (sock, (sockaddr *) (& ADDR), sizeof (ADDR ));

NREL = select (0, 0, & W, 0, & timeout );
If (NREL = socket_error) | (NREL = 0 ))
{
Printf ("Connect Host: % s: % d failed! \ N ", IP, Prot );
Closesocket (sock );
}
Else
{
Printf ("------------------------------------ Connect Host: % s: % d successful! \ N ", IP, Prot );
Select (0, & R, 0, 0, & timeout );
NREL = Recv (sock, Data, sizeof (data), 0 );
If (socket_error = NREL)
{
Printf ("Data Recv failed! \ N ");
}
Else
{
Printf ("------------------------------------ Recv data: % s \ n", data );
Closesocket (sock );
}
}
}

Closesocket (sock );
Wsacleanup ();

Getchar ();
Return 0;
}

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.