Summary of Select () function usage and question point __ function

Source: Internet
Author: User
1. function prototype and header file-Windows Bottom file is
#include <WinSock2.h>


-Linux bottom file for #include <unistd.h>
#include <sys/time.h>


-prototype int Select (int nfds,fd_set *readsets,fd_set *writesets,fd_set *exceptsets,const struct timeval)

Return value: Number of Ready descriptor, timeout return 0, error return-1


2, the measured code should pay attention to the point is: each time the select end of the descriptor value will change, so each cycle start to clean the Read_fds and Write_fds, and then the Socketid Fdset again.
The server-side code is as follows

#include <stdio.h> #include <string> #include <vector> #ifdef WINDOWS #include <WinSock2.h> # pragma comment (lib, "Ws2_32.lib") #endif #ifdef LINUX #include <unistd.h> #include <stdio.h> #include < stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/ in.h> #endif #pragma warning (disable:4996) #define Default_port 48000 #define MaxLen 4096 void prepareids (int listen_f d,std::vector<int> accept_fd,int &max_fd,fd_set &read_fds,fd_set &write_fds) {FD_ZERO (&read_
	FDS);
	Fd_zero (&write_fds);
	Fd_set (LISTEN_FD, &read_fds);
	MAX_FD = LISTEN_FD;
		for (Auto Itor = Accept_fd.begin (); Itor!= accept_fd.end (); itor++) {Fd_set (*itor, &read_fds);
		Fd_set (*itor, &write_fds);
	MAX_FD = Max (MAX_FD, *itor);
} MAX_FD = 1;
	} void doreadactions (int fd) {char buff[4096];
	memset (buff, 0, sizeof (buff));
	int len = recv (fd, Buff, maxlen, 0); printf ("Receive buffer[%s]\n ", buff);
	} void dowriteactions (int fd) {Char *buff = ' This is the ' message sending from HOST. '
	Send (FD, buff, strlen (buff) +1, 0);
printf ("Host Send Message ok\n");
	int main () {int listen_fd;
	struct sockaddr_in servaddr;
	Fd_set Readfds, Writefds;
	Std::vector<int> Accept_fds;
	int max_fd;
	Timeval timeout;
	Timeout.tv_sec = 3;
	Timeout.tv_usec = 500000;
	Fd_zero (&readfds);
	Fd_zero (&writefds);

	Wsadata InitData;
		if (WSAStartup (Makeword (2, 2), &initdata)) {printf ("Can not init socket");
	return-1; if (LISTEN_FD = socket (af_inet, sock_stream, ipproto_tcp) = =-1) {printf ("Create Socket Error:%s (errno:%d) \ n", s
		Trerror (errno), errno);
	Exit (0);
	} memset (&servaddr, 0, sizeof (SERVADDR));
	servaddr.sin_family = af_inet;
	SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
	Servaddr.sin_port = htons (Default_port); if (Bind (LISTEN_FD, (struct sockaddr*) &servaddr,sizeof (servaddr)) = = 1) {printf (Bind socket Error:%s (errno:%d) \ n ", strError (errno), errno);
		Closesocket (LISTEN_FD);
	Exit (0);
		} if (listen (listen_fd) = = 1) {printf ("Listen socket Error:%s (errno:%d) \ n", Strerror (errno), errno);
		Closesocket (LISTEN_FD);
	Exit (0);
	} accept_fds.clear ();
		while (1) {prepareids (listen_fd, Accept_fds, MAX_FD, Readfds, Writefds);
		printf ("r1:readfds.fd_count[%d],writefds.fd_count[%d]\n", Readfds.fd_count, Writefds.fd_count);
		int RTN = SELECT (MAX_FD, &readfds, &writefds, 0, &timeout);
		printf ("r2:readfds.fd_count[%d],writefds.fd_count[%d]\n", Readfds.fd_count, Writefds.fd_count); if (Fd_isset (LISTEN_FD, &readfds))//If LISTEN_FD has a value, represents a new link, stored in the new Socketid {int new_socket = accept (LISTEN_FD, Stru
			CT sockaddr*) null,null);
			printf ("Receive client accept\n");
				if (new_socket<0) {printf ("Accept socket Error:%s (errno:%d)", Strerror (errno), errno);
			Continue
		} accept_fds.push_back (New_socket); for (Auto Itor = Accept_fds.begin (); Itor!= accept_fds.end (); itor++) {if (Fd_isset (*itor, &readfds)) doreadactions (*itor);			
		if (Fd_isset (*itor, &writefds)) dowriteactions (*itor);
} return 0; }
The client code is as follows:
#include <Winsock2.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib") 
#pragma message ("Error!!!")
#define DEFAULTPORT 48000

void Main ()
{
	WORD wversionrequested;
	Wsadata Wsadata;
	int err;
	
	wversionrequested = Makeword (2, 2);
	
	Err = WSAStartup (wversionrequested, &wsadata);
	if (Err!= 0) {return
		;
	}
	
	SOCKET Sockclient=socket (af_inet,sock_stream,0);

	Sockaddr_in addrsrv;
	Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
	addrsrv.sin_family=af_inet;
	Addrsrv.sin_port=htons (defaultport);
	Connect (sockclient, (sockaddr*) &addrsrv,sizeof (sockaddr));


	Send (Sockclient, "This are Test", strlen ("This is Test") +1,0);
	Char recvbuf[100];
	memset (recvbuf,0,sizeof (RECVBUF));
	while (recv (sockclient,recvbuf,100,0) >0)
	{
		printf ("%s\n", recvbuf);
	}
	Recv (sockclient,recvbuf,100,0);
	printf ("%s\n", recvbuf);
	Closesocket (sockclient);
	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.