Socket的select模型
2011-07-04 22:40 Clingingboy 阅读(15112) 评论(2) 编辑 收藏 举报
注意点:发送的时候字节数不要发送…我就悲剧的测试数据发错了,以为哪出问题了
思路:
- 初始化一个socket
- 建立一个socket列表用于管理socket
- 将初步连接的socket放入列表中
- 用select判断列表中未处理的socket
Win API版本
1.
USHORT nPort = 4567; // 此服务器监听的端口号
// 创建监听套节字
SOCKET sListen = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(nPort);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
// 绑定套节字到本地机器
if(::bind(sListen, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf(" Failed bind() \n");
return -1;
}
// 进入监听模式
::listen(sListen, 5);
2.
// select模型处理过程
// 1)初始化一个套节字集合fdSocket,添加监听套节字句柄到这个集合
fd_set fdSocket; // 所有可用套节字集合
FD_ZERO(&fdSocket);
FD_SET(sListen, &fdSocket);
3.
while(TRUE)
{
// 2)将fdSocket集合的一个拷贝fdRead传递给select函数,
// 当有事件发生时,select函数移除fdRead集合中没有未决I/O操作的套节字句柄,然后返回。
fd_set fdRead = fdSocket;
int nRet = ::select(0, &fdRead, NULL, NULL, NULL);
if(nRet > 0)
{
// 3)通过将原来fdSocket集合与select处理过的fdRead集合比较,
// 确定都有哪些套节字有未决I/O,并进一步处理这些I/O。
for(int i=0; i<(int)fdSocket.fd_count; i++)
{
if(FD_ISSET(fdSocket.fd_array[i], &fdRead))
{
if(fdSocket.fd_array[i] == sListen) // (1)监听套节字接收到新连接
{
if(fdSocket.fd_count < FD_SETSIZE)
{
sockaddr_in addrRemote;
int nAddrLen = sizeof(addrRemote);
SOCKET sNew = ::accept(sListen, (SOCKADDR*)&addrRemote, &nAddrLen);
FD_SET(sNew, &fdSocket);
printf("接收到连接(%s)\n", ::inet_ntoa(addrRemote.sin_addr));
}
else
{
printf(" Too much connections! \n");
continue;
}
}
else
{
char szText[256];
int nRecv = ::recv(fdSocket.fd_array[i], szText, strlen(szText), 0);
if(nRecv > 0) // (2)可读
{
szText[nRecv] = '\0';
printf("接收到数据:%s \n", szText);
}
else // (3)连接关闭、重启或者中断
{
::closesocket(fdSocket.fd_array[i]);
printf("关闭\n");
FD_CLR(fdSocket.fd_array[i], &fdSocket);
}
}
}
}
}
else
{
printf(" Failed select() \n");
break;
}
}
二.自己写了一个c#版本的socket的select方法使用
class Threadtcpserver
{
private Socket server;
public Threadtcpserver()
{
IPAddress local = IPAddress.Parse("127.0.0.1");
IPEndPoint iep = new IPEndPoint(local, 4567);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 将套接字与本地终结点绑定
server.Bind(iep);
//在本地13000端口号上进行监听
server.Listen(20);
Console.WriteLine("等待客户机进行连接......");
List<Socket> socketList = new List<Socket>();
socketList.Add(server);
while (true)
{
List<Socket> temp = socketList.ToList();
Socket.Select(temp, null, null, 1000);
int count = temp.Count;
for (int i = 0; i < count; i++)
{
if (temp[i].Equals(server))
{
Socket client = socketList[i].Accept();
socketList.Add(client);
}
else
{
byte[] bytes = new byte[1024];
int len;
if ((len = temp[i].Receive(bytes)) > 0)
{
Console.WriteLine("收到数据:" + System.Text.Encoding.UTF8.GetString(bytes, 0, len));
}
else
{
temp[i].Close();
socketList.Remove(temp[i]);
Console.WriteLine("关闭");
}
}
}
}
}
}
测试:可以多次调用下面函数测试
public static void ClientTest()
{
Socket client;
byte[] buf = new byte[1024];
string input;
IPAddress local = IPAddress.Parse("127.0.0.1");
IPEndPoint iep = new IPEndPoint(local, 4567);
try
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(iep);
}
catch (SocketException)
{
Console.WriteLine("无法连接到服务器!");
return;
}
input = "test";
client.Send(Encoding.ASCII.GetBytes(input));
Console.WriteLine("断开与服务器的连接......");
client.Close();
}