Simple C ++ socket programming-based on TCP/IP protocol)

Source: Internet
Author: User
Server: # include <winsock2.h> # include <stdio. h> # pragma comment (Lib, "ws2_32.lib") void main () {// create a socket: Word myversionrequest; wsadata; myversionrequest = makeword (); int err; err = wsastartup (myversionrequest, & wsadata); If (! Err) {printf ("opened socket \ n");} else {// further bind the socket printf ("the nested word is not opened! "); Return;} socket sersocket = socket (af_inet, sock_stream, 0); // creates an identifiable socket // The parameter sockaddr_in ADDR to be bound; ADDR. sin_family = af_inet; ADDR. sin_addr.s_un.s_addr = htonl (inaddr_any); // ip address ADDR. sin_port = htons (6000); // bind port (sersocket, (sockaddr *) & ADDR, sizeof (sockaddr); // The listen (sersocket, 5) is bound ); // The second parameter indicates the maximum number of connections that can be received /////////////////////////// //////////////////////////////////////// //// // start listening /////// //////////////////////////////////////// /// // Sockaddr_in clientsocket; int Len = sizeof (sockaddr); While (1) {socket serconn = accept (sersocket, (sockaddr *) & clientsocket, & Len ); // if it is not accept but conection .. It will continuously listen to Char sendbuf [100]; sprintf (sendbuf, "Welcome % s to Bejing", inet_ntoa (clientsocket. sin_addr); // find the corresponding IP address and print this line to send (serconn, sendbuf, strlen (sendbuf) + 100); char receivebuf []; // receive Recv (serconn, receivebuf, strlen (receivebuf) +); printf ("% s \ n", receivebuf); closesocket (serconn ); // close wsacleanup (); // actions to release resources} client: # include <winsock2.h> # include <stdio. h> # pragma comment (Lib, "ws2_32.lib") VO Id main () {int err; Word versionrequired; wsadata; versionrequired = makeword (1, 1); err = wsastartup (versionrequired, & wsadata ); // If (! Err) {printf ("the nested character of the client has been opened! \ N ");} else {printf (" An error occurred while opening the nested words on the client! \ N "); return; // end} socket clientsocket = socket (af_inet, sock_stream, 0); sockaddr_in clientsock_in; success = inet_addr (" 127.0.0.1 "); success = af_inet; clientsock_in.sin_port = htons (6000); // BIND (clientsocket, (sockaddr *) & clientsock_in, strlen (sockaddr); // pay attention to the third parameter // listen (clientsocket, 5); Connect (clientsocket, (sockaddr *) & clientsock_in, sizeof (sockaddr); // start to connect char receivebuf [100]; Recv (clientsocket, receivebuf ); printf ("% s \ n", receivebuf); send (clientsocket, "Hello, this is client", strlen ("Hello, this is client") + 1, 0 ); closesocket (clientsocket); wsacleanup ();}

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.