TCP socket technology-server side

Source: Internet
Author: User

XX: the drifting 100 ~ _~ {2014/10/02 16:12}

Streaming socket (sock_stream): provides connection-oriented, reliable data transmission services, data error-free, non-repeated transmission, and is accepted in the sending order. Program Design Ideas

Server:

Process: wsastartup ()-> socket ()-> BIND ()-> listen ()-> Accept ()-> send ()

(1) create a socket

(2) bind the socket to a local address and port

(3) set the socket to the listening mode and prepare to accept client requests.

(4) Wait for the client request to arrive. When the request arrives, the system accepts the connection request and returns a socket corresponding to the connection.

(5) use the returned socket to communicate with the client

(6) Return and wait for the connection request from another client

(7) Disable socket

// Sockettcp. CPP: defines the entry point for the console application. // # include "stdafx. H "# include" tchar. H "# pragma comment (Lib," wsock32.lib ") # include <stdio. h >#include <winsock2.h> # define max_ser 10 # define host_path 256 # define hostname_size host_pathchar hostname [max_path] = {0}; unsigned short maxservice; unsigned short port; void Service (lpvoid LPV); int loopcontrol (socket llistenfd, int ismultitaski NG); void initial (); int initsockets (void); // initialize windows socketint main (INT argc, char * argv []) {socket listenfd; struct sockaddr_in serveraddr; int err; initial (); initsockets (); // create socketlistenfd = socket (pf_inet, sock_stream, 0); If (listenfd = invalid_socket) {printf ("error: out of socket resource \ n "); return 1;} // bind the local port serveraddr. sin_family = pf_inet; // The protocol type is inetserveraddr. sin_addr.s_un.s_addr = htonl (inaddr _ Any); // The local ipserveraddr. sin_port = htons (port); // The binding port is 5054err = BIND (listenfd, (const struct sockaddr *) & serveraddr, sizeof (serveraddr); If (ERR = invalid_socket) {printf ("error: Unable to bind socket \ n"); return 1 ;}// listen listener port err = listen (listenfd, 3); If (ERR = invalid_socket) {printf ("error: Listen socket failed \ n"); return 1;} printf ("Listen ...... "); loopcontrol (listenfd, 1); printf (" server is down \ n "); wsaclean Up (); Return 0;} void initial () {maxservice = 3; Port = 5054;}/** Winsock service initialization */INT initsockets (void) {wsadata; word sockversion; // typedef unsigned short word (16) int err; sockversion = makeword (2, 2); err = wsastartup (sockversion, & wsadata); If (Err! = 0) {printf ("error % d: Winsock not avaliable \ n", err);} printf ("Environemnt invaild success ..... \ n "); Return 0;}/*** wait for client connection */INT loopcontrol (socket listenfd, int ismultitasking) {socket acceptfd; struct sockaddr_in clientaddr; // client address information int err; int servernum = 0; handle handles [max_ser]; int nsize; nsize = sizeof (clientaddr); int myid; // fixed number of times [The number of times of service is less than the maximum number of times] While (servernum <maxservice) {acceptfd = accept (listenfd, (St Ruct sockaddr *) & clientaddr, & nsize); // receives the client connection preparation if (acceptfd = invalid_socket) {printf ("error: accept failed \ n "); return 1;} printf ("accepted connection from client at % s \ n", inet_ntoa (clientaddr. sin_addr); If (ismultitasking) {handles [servernum] = createthread (null, 1000, (lpthread_start_routine) service, (lpvoid) acceptfd, 0, (unsigned long *) & myid);} else {Service (lpvoid) acceptfd);} servernum ++;} If (ismultitasking) {Err = waitformultipleobjects (maxservice, handles, true, infinite); printf ("last thread to finish was thread # % d \ n", err);} return 0 ;} /** receive client data and send data to the client */void Service (lpvoid LPV) {socket acceptfd = (socket) LPV; const char * MSG = "message have recived"; char response [1, 4096]; memset (response, 0, sizeof (response); While (acceptfd! = Socket_error) {int result = Recv (acceptfd, response, sizeof (response), 0); // accept client data if (result> 0) {printf ("from client: % s ", response); send (acceptfd, MSG, strlen (MSG) + 1, 0) ;}} closesocket (acceptfd );}

Enter local IP: 5054 in the browser to view the running effect. Or use netstat-a to view the port status.


TCP socket technology-server side

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.