simple select echo server example

//    echo_server.cc
#include <fcntl.h>			//	fcntl
#include <stdlib.h>			//	exit
#include <sys/select.h>		//	select
#include <sys/socket.h>		//	socket
#include <netinet/in.h>		//	sockaddr_in
#include <strings.h>		//	bzero
#include <arpa/inet.h>		//	inet_addr
#include <errno.h>			//	errno
#include <unistd.h>			//	read
#include <stdio.h>			//	perror
#include <string.h>			//	memset
#include <iostream>

int main()
{
	std::cout << "start select echo server..." << std::endl;

	static const int max_back_log = 10;
	int cur_conn_nun = 0;
	int fd_conn_arr[max_back_log];

	int listenfd = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == listenfd)
	{
		std::cout << "error at socket" << std::endl;
		exit(1);
	}

	int option_name = 1;
	if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &option_name, sizeof(int)) == -1)
	{
        perror("setsockopt");
        exit(1);
    }

	struct sockaddr_in serveraddr;
	bzero(&serveraddr,sizeof(sockaddr_in));
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(9877);
	inet_aton("127.0.0.1",&(serveraddr.sin_addr));
	bind(listenfd,(sockaddr*)&serveraddr,sizeof(sockaddr_in));

	listen(listenfd,max_back_log);

	fd_set fd_flag;
	int connectfd = 0;
	struct sockaddr_in clientaddr;
	struct timeval tv;
	socklen_t	client_addr_len;
	int read_size = 0;
	int send_size = 0;
	int read_data = 0;
	int fd_max = listenfd;
	memset(fd_conn_arr,0,sizeof(int)*max_back_log);
	while(1)
	{
		FD_ZERO(&fd_flag);
		FD_SET(listenfd,&fd_flag);
		
		tv.tv_sec = 15;
		tv.tv_usec = 0;
		
		for(int i = 0; i < max_back_log; ++i)
		{
			if(fd_conn_arr[i] != 0)
			{	
				FD_SET(fd_conn_arr[i],&fd_flag);
			}
		}
		int ret = select(fd_max + 1,&fd_flag,NULL,NULL,&tv);
		if(-1 > ret)
		{
			perror("error at select");
			exit(1);
		}
		else if(0 == ret)
		{
			perror("select time out");
			continue;
		}

		for(int i = 0; i < cur_conn_nun; ++i)
		{
			if(FD_ISSET(fd_conn_arr[i],&fd_flag))
			{
				if((read_size = read(fd_conn_arr[i],(void*)&read_data,sizeof(int))) < 0 )
				{
					std::cout << "read error" <<std::endl;
				}
				else
				{
					std::cout << read_size << " bytes data read: " << read_data << std::endl;
				}
				if((send_size = write(fd_conn_arr[i],(void*)&read_data,sizeof(int))) < 0 )
				{
					std::cout << "read error" <<std::endl;
				}
				else
				{
					std::cout << send_size << " bytes data send: " << read_data << std::endl;
				}
			}
		}
		if(FD_ISSET(listenfd,&fd_flag))
		{
			connectfd = accept(listenfd,(sockaddr*)&clientaddr,&client_addr_len);
			if(-1 == connectfd)
			{
				perror("error at accept");
				continue;
			}	
			if(cur_conn_nun < max_back_log)
			{
				fd_conn_arr[cur_conn_nun++] = connectfd;
				if(fd_max < connectfd)
				{
					fd_max = connectfd;
				}
			}

			std::cout << "new connection coming at " << inet_ntoa(clientaddr.sin_addr) << ": " << ntohs(clientaddr.sin_port) << std::endl;
		}
	}
	return 0;
}


 

//    echo_client

#include <stdlib.h>			//	exit
#include <netinet/in.h>		//	sockaddr_in
#include <strings.h>		//	bzero
#include <arpa/inet.h>		//	inet_addr
#include <sys/socket.h>
#include <iostream>

int main()
{
	int sock = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sock)
	{
		std::cout << "error at socket"<<std::endl;
		exit(1);
	}
	struct sockaddr_in clientaddr;
	clientaddr.sin_family = AF_INET;
	clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	clientaddr.sin_port = htons(9877);
	int res = connect(sock,(sockaddr*)&clientaddr,sizeof(sockaddr_in));
	if(-1 == res)
	{
		std::cout << "error at connect"<<std::endl;
		exit(1);
	}
	static int send_data = 1;
	for(;;)
	{
		int send_bytes = send(sock,(void*)&send_data,sizeof(int),0);
		if(-1 != send_bytes)
		{
			std::cout << send_bytes << " bytes data send: " << send_data++ << std::endl;
		}
		int recv_data = 0;
		int recv_bytes = recv(sock,(void*)&recv_data,sizeof(int),0);
		if(-1 != recv_bytes)
		{
			std::cout << recv_bytes << " bytes data recv: " << recv_data << std::endl;
		}
		sleep(1);
	}
}


與WINDOWS版本不同的是 select 的第一個參數

nfds

linux 版本

The nfds argument specifies the range of descriptors to be tested. The firstnfds descriptors shall be checked in each set; that is, the descriptors from zero throughnfds-1 in the descriptor sets shall be examined.

windows 版本

nfds

Ignored. The nfds parameter is included only for compatibility with Berkeley sockets.

其次遇到的問題是 Address already in use,使用 if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &option_name, sizeof(int)) == -1) 解決。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章