之前光看理论是不行滴,一定要实践,实践啊,不然永远都是门外汉!!

嗯嗯,把找到的一段源码贴上先,稍微修改了一下:

#include <winsock.h>
#include <stdio.h> #include <string> #define PORT 5010
#define MSGSIZE 1024
int BytesSum = ; #pragma comment(lib, "ws2_32.lib") DWORD WINAPI WorkerThread(LPVOID lpParameter); void InitWinSocket()
{
WSADATA wsaData;
WSAStartup(0x0202, &wsaData);
} #define ListenQ 10 int main()
{
InitWinSocket(); int opt = TRUE;
int master_socket , addrlen , new_socket , client_socket[] , max_clients = , activity, i , valread , sd;
int max_sd;
struct sockaddr_in address; char buffer[]; //data buffer of 1K //set of socket descriptors
fd_set readfds; //a message
char *message = "ECHO Daemon v1.0 \r\n"; //initialise all client_socket[] to 0 so not checked
for (i = ; i < max_clients; i++)
{
client_socket[i] = ;
} //create a master socket
if( (master_socket = socket(AF_INET , SOCK_STREAM , )) == )
{
perror("socket failed");
exit(EXIT_FAILURE);
} //set master socket to allow multiple connections , this is just a good habit, it will work without this
if( setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < )
{
perror("setsockopt");
exit(EXIT_FAILURE);
} //type of socket created
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT ); //bind the socket to localhost port 8888
if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("Listener on port %d \n", PORT); //try to specify maximum of 3 pending connections for the master socket
if (listen(master_socket, ) < )
{
perror("listen");
exit(EXIT_FAILURE);
} //accept the incoming connection
addrlen = sizeof(address);
puts("Waiting for connections ..."); while(TRUE)
{
//clear the socket set
FD_ZERO(&readfds); //add master socket to set
FD_SET(master_socket, &readfds);
max_sd = master_socket; //add child sockets to set
for ( i = ; i < max_clients ; i++)
{
//socket descriptor
sd = client_socket[i]; //if valid socket descriptor then add to read list
if(sd > )
FD_SET( sd , &readfds); //highest file descriptor number, need it for the select function
if(sd > max_sd)
max_sd = sd;
} //wait for an activity on one of the sockets , timeout is NULL , so wait indefinitely
activity = select( max_sd + , &readfds , NULL , NULL , NULL); if (activity < )
{
printf("select error");
} //If something happened on the master socket , then its an incoming connection
if (FD_ISSET(master_socket, &readfds))
{
if ((new_socket = accept(master_socket, (struct sockaddr *)&address,&addrlen))<)
{
perror("accept");
exit(EXIT_FAILURE);
} //inform user of socket number - used in send and receive commands
printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port)); //send new connection greeting message
if( send(new_socket, message, strlen(message), ) != strlen(message) )
{
perror("send");
} puts("Welcome message sent successfully"); //add new socket to array of sockets
for (i = ; i < max_clients; i++)
{
//if position is empty
if( client_socket[i] == )
{
client_socket[i] = new_socket;
printf("Adding to list of sockets as %d\n" , i); break;
}
}
} //else its some IO operation on some other socket :)
for (i = ; i < max_clients; i++)
{
sd = client_socket[i]; if (FD_ISSET( sd , &readfds))
{
//Check if it was for closing , and also read the incoming message
if ((valread = recv( sd , buffer, ,)) == )
{
//Somebody disconnected , get his details and print
getpeername(sd , (struct sockaddr*)&address , &addrlen);
printf("Host disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port)); //Close the socket and mark as 0 in list for reuse
closesocket( sd );
client_socket[i] = ;
} //Echo back the message that came in
else
{
//set the string terminating NULL byte on the end of the data read
buffer[valread] = '\0';
send(sd , buffer , strlen(buffer) , );
}
}
}
} return ;
}

ok,回家慢慢学习!

win32 socket之select的更多相关文章

  1. windows socket编程select模型使用

    int select(         int nfds,            //忽略         fd_ser* readfds,    //指向一个套接字集合,用来检测其可读性       ...

  2. 非阻塞式socket的select()用法

    Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只 是习惯写诸如 connect.accept.recv或recvfrom这样的阻塞程 ...

  3. socket中select的使用源码

    下面的代码来自IBM学习网站,是学习socket通信和select使用的一个很好的源码. server.c  服务器端 #include <stdio.h> #include <st ...

  4. socket之 select模型

    前段时间一直想学习网络编程的select模型,看了<windows网络编程>的介绍,参考了别人的博客. 这里的资料主要来自http://www.cnblogs.com/RascallySn ...

  5. Python之基于socket和select模块实现IO多路复用

    '''IO指的是输入输出,一部分指的是文件操作,还有一部分网络传输操作,例如soekct就是其中之一:多路复用指的是利用一种机制,同时使用多个IO,例如同时监听多个文件句柄(socket对象一旦传送或 ...

  6. 转 linux socket的select函数例子

    使用select函数可以以非阻塞的方式和多个socket通信.程序只是演示select函数的使用,功能非常简单,即使某个连接关闭以后也不会修改当前连接数,连接数达到最大值后会终止程序. 1. 程序使用 ...

  7. python中的网络通信,socket、select、selectors、socketserver

    楔子 网络通信用于获取一个算法在本地运行所需的数据,还可以共享信息实现分布式处理,另外可以用来管理云服务. python的标准库提供了一些模块来创建网络服务以及访问现有服务ipaddress模块提供了 ...

  8. win32 socket 编程(六)——UDP

    基于 UDP 无连接的 socket 编程 一.服务器端操作流程 1.1加载套接字库(WSAStartup) 1.2 创建套接字  (socket) 1.3将创建的套接字绑定到一个本地地址和端口上   ...

  9. win32 socket编程(五)——客户端实例(TCP)

    一.客户端操作流程 1.1 加载套接字库(WSAStartup()) 1.2创建套接字(socket()). 1.3向服务器发出连接请求(connect()). 对于客户端来说,它不需要绑定,可以直接 ...

随机推荐

  1. 大数据开发实战:Hive表DDL和DML

    1.Hive 表 DDL 1.1.创建表 Hive中创建表的完整语法如下: CREATE [EXTERNAL] TABLE [IF NOT EXISTS]  table_name [ (col_nam ...

  2. Struts2环境搭建及实例解析

    与struts1框架的使用一样,struts2的环境搭建及使用同样分为以下几个步骤: 1.      引入jar包(这里以struts2.2.3为例),所需jar包有以下几个: a)        s ...

  3. TCP Socket Programming in Node.js

    TCP Socket Programming in Node.js Posted on October 26th, 2011 under Node.jsTags: Client, node.js, S ...

  4. 转:Raft一致性选举算法的ppt与视频

    http://xiaorui.cc/2016/07/08/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB-%E3%80%8A%E5%88%86%E5%B8%83%E5%BC% ...

  5. how-to-get-a-job-in-deep-learning

    http://blog.deepgram.com/how-to-get-a-job-in-deep-learning/ How to Get a Job In Deep Learning 22 SEP ...

  6. Using Timers in MFC Applications

    Timer Events in MFC Applications Event timers are always handy to have around and useful in nearly e ...

  7. Mongoose Connection best practice

    There is often quite a lot of confusion about how best to set up a database connection with Mongoose ...

  8. WDCP,LNMP安装PHP缓存加速扩展eAccelerator,xcache和memcached

    VPS主机性能配置如果太差,则在运行Wordpress博客和Discuz! 论坛等高消耗程序时能够明显感觉出VPS有些吃力.另外,即使VPS主机的CPU.内存.硬盘I/O等性能足够好,但是在面对大流量 ...

  9. JavaScript 上下文环境和作用域,以及 call、apply 和 bind【转载+翻译+整理】

    --看到这篇文章,翻译国外的,虽说写得有点矫情,但总体来看,还是相当不错的- 本文内容 我在哪儿?你又是谁 ? this? 用 apply 和 call 掌控上下文环境 bind 之美 本文将说明上下 ...

  10. Android Context完全解析,你所不知道的Context的各种细节

    Context相信所有的Android开发人员基本上每天都在接触,因为它太常见了.但是这并不代表Context没有什么东西好讲的,实际上Context有太多小的细节并不被大家所关注,那么今天我们就来学 ...