标题:SELECT模型(求教)
只看楼主
devil8283
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2004-10-2
 问题点数:0 回复次数:3 
SELECT模型(求教)

如何用select()函数对套接字I/O进行管理?(我看书了,可是不会用,书上没示例)

我要具体用法,请斑竹帮帮忙,下面我给出个简单的服务器代码,

斑竹帮我用SELECT()函数对my_sockfd client_sockfd 进行管理

并给出代码 谢谢了!! /*program for send "congratulations"*/ #include <winsock2> #include <stdio> #include <iostream>

using namespace std;

void main() { int Ret,sin_size,my_port=5150; SOCKET my_sockfd,client_sockfd; WSADATA data; int ret; SOCKADDR_IN my_addr,client_addr; my_addr.sin_family=AF_INET; my_addr.sin_port=htons(my_port); my_addr.sin_addr.s_addr=htonl(INADDR_ANY); sin_size=sizeof(SOCKADDR_IN); if((Ret=WSAStartup(MAKEWORD(2,2),&data))!=0) { printf("WSAStartup failed with error %d \n",Ret); exit(1); } if((my_sockfd=socket(AF_INET,SOCK_STREAM, IPPROTO_TCP))==SOCKET_ERROR) { printf("Got socket failed with error %d \n", WSAGetLastError()); WSACleanup(); exit(1); } if((bind(my_sockfd,(SOCKADDR*)&my_addr, f sizeof(SOCKADDR)))==SOCKET_ERROR) { printf("Bind failed with error %d \n", WSAGetLastError()); closesocket(my_sockfd); WSACleanup(); exit(1); } if((listen(my_sockfd,2))==SOCKET_ERROR) { printf("Listen failed with error %d \n", WSAGetLastError()); closesocket(my_sockfd); WSACleanup(); exit(1); } if((client_sockfd=accept(my_sockfd, (SOCKADDR*)&client_addr,&sin_size)) ==SOCKET_ERROR) { printf("accept failed with error %d \n", WSAGetLastError()); closesocket(my_sockfd); WSACleanup(); exit(1); } printf("Server: got connect from %s \n", inet_ntoa(client_addr.sin_addr)); if((send(client_sockfd,"Congratulations!!", 18,0))==SOCKET_ERROR) { printf("Send failed with error %d \n", WSAGetLastError()); closesocket(client_sockfd); closesocket(my_sockfd); WSACleanup(); exit(1); } printf("send data successfully\n"); closesocket(client_sockfd); closesocket(my_sockfd); WSACleanup(); }

搜索更多相关主题的帖子: SELECT 模型 
2004-10-15 20:58
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
得分:0 
&lt;winsock2&gt;
没用过……Web服务编程吧。
2004-10-15 21:36
devil8283
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2004-10-2
得分:0 

哈哈

我自己悟出了,我会了,好高兴

不过还要谢谢斑竹的热心啊!!!


曾经我不成熟,不过——现在我也成熟不 到哪里去。在我眼中,人生就像一场脱俗的梦, 谁最先醒来,谁就会最失落。 My dream will go on. —— Goon ---------------------
2004-10-15 21:41
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
得分:0 
呵呵,好啊。孺子可教。
2004-10-15 21:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-6589-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020134 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved