WinSock 2: client that uses WinSock to implement socket communication.

Source: Internet
Author: User

Compiling environment: VC ++ 6.0

/*
* File: client. cpp
*
*/

# Include <winsock2.h>
# Include <stdio. h>
# Include <stdlib. h>

# Define default_port 5150.
# Define default_buffer 4096.
# Define default_message "login"
# Define default_count 20
Int iport = default_port;

Char szserver [128];
Char szmessage [1024];
DWORD dwcount = default_count;
Bool bsendonly = false;
// Function: Usage
// Description:
// Print usage information and exit
Void usage ()
{
Printf ("Usage: Client [-P: X] [-I: IP] [-N: X] [-O]/n ");
Printf ("-P: X remote port to send to/N ");
Printf ("-I: Str server's address or host name/N ");
Printf ("-N: X number of times to send message/N ");
Printf ("-o send messages only; don't recevie/N ");
Printf ("Camel 2004-8-29 ");
Exitprocess (1 );
}

// Function:
Void validateargs (INT argc, char ** argv)
{
Int I;
For (I = 1; I <argc; I ++)
{
If (argv [I] [0] = '-') | (argv [1] [0] = '/'))
{
Switch (tolower (argv [I] [1])
{
Case 'p ':
Iport = atoi (& argv [I] [3]);
Break;
Case 'I ':

If (strlen (argv [I])> 3)
Strcpy (szserver, & argv [I] [3]);
Break;
Case 'N ':
If (strlen (argv [I])> 3)
Dwcount = atoi (& argv [I] [3]);
Case 'O ':
Bsendonly = true;
Break;
Default:
Usage ();
Break;
}
}
}

}

Int main (INT argc, char ** argv)
{
Wsadata WSD;
Socket sclient;
Char szbuffer [default_buffer];
Int ret, I;

Struct sockaddr_in server;
Struct hostent * Host = NULL;
Printf ("===== client =====/N ");
Validateargs (argc, argv );
If (wsastartup (makeword (2, 2), & WSD )! = 0)
{
Printf ("failed to load Winsock! /N ");
Return 1;
}

Strcpy (szmessage, default_message );

//
// Create the socket, and attempt to connect to the server
//
Sclient = socket (af_inet, sock_stream, ipproto_ip );
If (sclient = socket_error)
{
Printf ("socket () failed: % d/N", wsagetlasterror ());
Return 1;
}
// Select the local interface, and bind to it
Server. sin_family = af_inet;
Server. sin_port = htons (iport );
Server. sin_addr.s_addr = inet_addr (szserver );

//
// If the supplied server address wasn't in the form
// "AAA. BBB. ccc. DDD" it's a host name, so try to resolver it
//
If (server. sin_addr.s_addr = inaddr_none)
{
Host = gethostbyname (szserver );
If (host = NULL)
Printf ("unable to resolve server: % s/n", szserver );
Return 1;
}
Printf ("Debug: copymemory ");
// Copymemory (& server. sin_addr, host-> h_addr_list [0], host-> h_length );
Printf ("Debug: copymemory over ");
 
If (connect (sclient, (struct sockaddr *) & server, sizeof (server) = socket_error)
{
Printf ("Connect () failed: % d] n", wsagetlasterror ());
Return 1;
}
//
// Send and receive data
//
For (I = 0; I <dwcount; I ++)
{
Ret = Send (sclient, szmessage, strlen (szmessage), 0 );
If (ret = 0)
Break;
Else if (ret = socket_error)
{
Printf ("Send () failed: % d/N", wsagetlasterror );
Break;
}
Printf ("Send % d Bytes/N", RET );
If (! Bsendonly)
{
Ret = Recv (sclient, szbuffer, default_buffer, 0 );
If (ret = 0)
Break;
Else if (ret = socket_error)
{
Printf ("Recv () failed: % d/N", wsagetlasterror ());
Break;
}
Szbuffer [RET] = '/0 ';
Printf ("Recv [% d bytes]: '% s'/N", RET, szbuffer );
}
}
Closesocket (sclient );
Wsacleanup ();
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.