Today, we finally achieved the simplest Network C/S programming.

Source: Internet
Author: User
Tags htons

Server

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

Int main (INT argc, char * argv [])
{
Wsadata;
Word sockversion = makeword (2, 0 );
Wsastartup (sockversion, & wsadata );

Socket S = socket (af_inet, sock_stream, ipproto_tcp );
If (S = invalid_socket)
{
Printf ("failed socket ()/n ");
Wsacleanup ();
Return 0;
}

Sockaddr_in sin;
Sin. sin_family = af_inet;
Sin. sin_port = htons (8888 );
Sin. sin_addr.s_un.s_addr = inaddr_any;

If (BIND (S, (lpsockaddr) & sin, sizeof (SIN) = socket_error)
{
Printf ("failed BIND ()/n ");
Wsacleanup ();
Return 0;
}

If (Listen (S, 2) = socket_error)
{
Printf ("failed listen ()/n ");
Wsacleanup ();
Return 0;
}

Sockaddr_in remoteaddr;
Int naddrlen = sizeof (remoteaddr );
Socket Client;
Char sztext [] = "Hello world! ";

While (true)
{
Client = accept (S, (sockaddr *) & remoteaddr, & naddrlen );
If (client = invalid_socket)
{
Printf ("failed accept ()/n ");
Continue;
}
Printf ("received a connection: % s/n", inet_ntoa (remoteaddr. sin_addr ));
Send (client, sztext, strlen (sztext), 0 );
Closesocket (client );
}

Closesocket (s );
Wsacleanup ();
Return 0;

}

Client

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

Int main (INT argc, char * argv [])
{
Wsadata;
Word sockversion = makeword (2, 0 );
Wsastartup (sockversion, & wsadata );

Socket S = socket (af_inet, sock_stream, ipproto_tcp );
If (S = invalid_socket)
{
Printf ("failed socket ()/n ");
Wsacleanup ();
Return 0;
}

Sockaddr_in servaddr;
Servaddr. sin_family = af_inet;
Servaddr. sin_port = htons (8888 );
Servaddr. sin_addr.s_un.s_addr = inet_addr ("127.0.0.1 ");

If (connect (S, (sockaddr *) & servaddr, sizeof (servaddr) =-1)
{
Printf ("failed connect ()/n ");
Wsacleanup ();
Return 0;
}

Char buffer [256];
Int nrecv = Recv (S, buffer, 256, 0 );
If (nrecv> 0)
{
Buffer [nrecv] = '/0 ';
Printf ("received data: % s/n", buffer );
}
Closesocket (s );
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.