Simple TCP/IP server for VC ++ socket programming

Source: Internet
Author: User

Simple TCP/IP server for VC ++ socket programming

# Include <windows. h>
# Include <iostream. h>
# Include <Winsock. h>

# Define no_flags_set 0

# Define Port (u_short) 44965
# Define maxbuflen 256

Int main (void)
{
Wsadata data;
Sockaddr_in serversockaddr;
Sockaddr_in clientsockaddr;
Socket serversocket;
Socket clientsocket;
Int addrlen = sizeof (sockaddr_in );
Int status;
Int numrcv;
Char buffer [maxbuflen];

/* Initialize the Windows Socket DLL */
Status = wsastartup (makeword (1, 1), & data );
If (status! = 0)
Cerr <"error: wsastartup unsuccessful"
<Endl;

/* Zero the sockaddr_in structure */
Memset (& serversockaddr, 0,
Sizeof (serversockaddr ));
/* Specify the port portion of the address */
Serversockaddr. sin_port = htons (port );
/* Specify the address family as Internet */
Serversockaddr. sin_family = af_inet;
/* Specify that the address does not matter */
Serversockaddr. sin_addr.s_addr = htonl (inaddr_any );

/* Create a socket */serversocket = socket (af_inet, sock_stream, 0 );
If (serversocket = invalid_socket)
Cerr <"error: Socket unsuccessful" <Endl;

/* Associate the socket with the address */
Status = BIND (serversocket,
(Lpsockaddr) & serversockaddr,
Sizeof (serversockaddr ));
If (status = socket_error)
Cerr <"error: bind unsuccessful" <Endl;

/* Allow the socket to take connections */
Status = listen (serversocket, 1 );
If (status = socket_error)
Cerr <"error: Listen unsuccessful" <Endl;

/* Accept the connection request when one
Is already ed */
Clientsocket = accept (serversocket,
(Lpsockaddr) & clientsockaddr,
& Addrlen );

Cout <"got the connection..." <Endl;

While (1)
{
Numrcv = Recv (clientsocket, buffer,
Maxbuflen, no_flags_set );
If (numrcv = 0) | (numrcv = socket_error ))
{
Cout <"Connection terminated." <Endl;
Status = closesocket (clientsocket );
If (status = socket_error)
Cerr <"error: closesocket unsuccessful"
<Endl;
Status = wsacleanup ();
If (status = socket_error)
Cerr <"error: wsacleanup unsuccessful"
<Endl;
Return (1 );
}
Cout <buffer <Endl;
}/* While */

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.