现在写winsock更倾向于用API自己写,控制上更自由一些。<br><br>给你一个小Demo(C)看是否你想要的呢:<br><br>delphi中,你自己uses Winsock,就可以改写过来了<br>--------------------------------------<br>编译环境: C++ Builder6.0<br><br>//---------------------------------------------------------------------------------------------<br>//公共单元<br>//common.h<br>#ifndef commonH<br>#define commonH<br>#endif<br><br>#define SERVER_PORT 21<br>#define SERVER_IP "127.0.0.1"<br>#define MAX_SOCKFD 8<br><br>void InitWinSock();<br>void UnInitWinSock();<br><br>//common.cpp<br>#pragma hdrstop<br><br>#include <common.h><br>#include <winsock.h><br>#include <windows.h><br><br>#pragma package(smart_init)<br><br><br>void InitWinSock()<br>{<br> WSADATA wsaData;<br><br> WSAStartup(MAKEWORD(2, 2), &wsaData);<br>}<br><br>void UnInitWinSock()<br>{<br> WSACleanup;<br>}<br><br>//---------------------------------------------------------------------------------------------<br>//服务端<br>//tcp_server.cpp<br>#pragma hdrstop<br>#include <stdio.h><br>#include <inetsdk.h><br>#include <wininet.h><br>#include <winsock.h><br>#include <iostream><br>#include <windows.h><br>#include <io.h><br>#include <sys/types.h><br>#include <common.h> <br><br>#pragma argsused<br><br><br>int main(int argc, char* argv[])<br>{<br> int fd_listen,fd_accept,client[MAX_SOCKFD],fd,reclen,i,n;<br> int maxfd,maxi,nready;<br> int optlen=sizeof('1');<br> char optval='1';<br> int addr_len=sizeof(struct sockaddr_in);<br> char buff[256];<br> char msg[]="welcome to server!";<br> char msg2[]="too many clients!/n";<br> struct sockaddr_in sock_server,sock_client;<br> fd_set readfds,ifds;<br><br> InitWinSock();<br> fd_listen=socket(AF_INET,SOCK_STREAM,0);<br> if (fd_listen<0) {<br> printf("socket error"
;<br> exit(1);<br> }<br> /*填写 sockaddr_in 结构*/<br> memset(&sock_server,0,addr_len);<br> sock_server.sin_family =AF_INET;<br> sock_server.sin_port =htons(SERVER_PORT);<br> sock_server.sin_addr.S_un.S_addr =htonl(INADDR_ANY);<br><br> /* 如果服务器终止后,服务器可以第二次快速启动而不用等待一段时间 */<br> setsockopt(fd_listen,SOL_SOCKET,SO_REUSEADDR,&optval,optlen);<br><br> /*使用bind()把上面的socket和定义的IP地址和端口绑定*/<br> if (bind(fd_listen,(struct sockaddr*)(&sock_server),addr_len)<0) {<br> printf("socket bind error!/n"
;<br> exit(1);<br> }<br><br> /*使用listen()来使socket接受进入的连接请求,<br> *第二个参数为socket可维护的最大连接数(包括已连接的和请求连接的数量)。<br> */<br> if (listen(fd_listen,MAX_SOCKFD)<0) {<br> printf("socket listen error!/n"
;<br> exit(1);<br> }<br><br> /* initialize */<br> maxfd = fd_listen;<br> maxi=-1;<br> fd_accept=0;<br> for (fd=0;fd<MAX_SOCKFD;fd++)<br> client[fd]=-1; /* -1 indicates available entry */<br> FD_ZERO(&readfds);<br> FD_SET(fd_listen,&readfds);<br><br> while (1) {<br> //--------------------------------------------------------------------------<br> ifds=readfds;<br> nready=select(maxfd + 1, &ifds, NULL, NULL, NULL);<br><br> if (FD_ISSET(fd_listen, &ifds)) { /* new client connection */<br> //新的连接<br> fd_accept=accept(fd_listen,(struct sockaddr*)(&sock_client),&addr_len);<br><br> if (fd_accept>0) {<br> sendto(fd_accept,msg,sizeof(msg),0,(struct sockaddr*)(&sock_client),addr_len);<br> printf("connect from %s/n",inet_ntoa(sock_client.sin_addr));<br> }<br><br> for (i=0;i<MAX_SOCKFD;i++)<br> if (client
<0){<br> client =fd_accept;<br> break;<br> } //for i<br><br> //是否超出最大连接数<br> if (i==MAX_SOCKFD) {<br> sendto(fd_accept,msg2,sizeof(msg2),0,(struct sockaddr*)(&sock_client),addr_len);<br> //write(fd_accept, msg2, sizeof(msg2));<br> close(fd_accept);<br> continue;<br> }<br><br> FD_SET(fd_accept, &readfds); /* add new descriptor to set */<br><br> if (fd_accept>maxfd) maxfd=fd_accept;<br><br> if (i>maxi) maxi=i;<br><br> if (--nready<=0) continue; /* no more readable descriptors */<br> }//if<br><br> //==============================================<br> for (i = 0; i <= maxi; i++) { /* check all clients for data */<br> if ((fd = client) < 0) continue;<br><br> if (FD_ISSET(fd, &ifds)) {<br> memset(buff,0,sizeof(buff));<br> reclen=recvfrom(fd,buff,sizeof(buff),0,(struct sockaddr*)(&sock_server),&addr_len);<br> if (reclen<=0){<br> /*连接中断,清除连接状态旗标*/<br> printf("connection close./n"<br> client = -1;<br> FD_CLR(fd, &readfds); /* turn off the bit for sockfd in allset */<br> close(fd);<br> }<br> else {<br> printf("receive message from client: %s/n",buff);<br> //返回一条确认信息<br> sendto(fd,buff,sizeof(buff),0,(struct sockaddr*)(&sock_server),addr_len);<br> }<br> <br> if (--nready <= 0) break; /* no more readable descriptors */<br> } //if 1<br><br> } //end for i<br> //==============================================<br><br> //--------------------------------------------------------------------------<br> } /*while*/<br><br> close(fd_listen);<br> UnInitWinSock();<br> return 0;<br>}<br><br>//---------------------------------------------------------------------------------------------------<br>//客户端<br>//tcp_client.cpp<br>#pragma hdrstop<br><br>#include <stdio.h><br>#include <inetsdk.h><br>#include <wininet.h><br>#include <winsock.h><br>#include <iostream><br>#include <io.h><br>#include <windows.h><br>#include <sys/types.h><br>#include <common.h><br><br><br><br>#pragma argsused<br>int main(int argc, char* argv[])<br>{<br> int sockfd;<br> int addr_len=sizeof(struct sockaddr_in);<br> int reclen;<br> char buff[256];<br> struct sockaddr_in sock_client;<br><br> InitWinSock();<br> sockfd=socket(AF_INET,SOCK_STREAM,0);<br> if (sockfd<0)<br> {<br> printf("socket error"<br> exit(1);<br> }<br><br> /*填写 sockaddr_in 结构*/<br> memset(&sock_client,0,addr_len);<br> sock_client.sin_family =AF_INET;<br> sock_client.sin_port =htons(SERVER_PORT);<br> sock_client.sin_addr.S_un.S_addr =inet_addr(SERVER_IP);<br><br> /*连接到server*/<br> if (connect(sockfd,(struct sockaddr*)(&sock_client),addr_len)<0)<br> {<br> printf("socket connect error!/n"<br> exit(1);<br> }<br><br> /*接收由server端传来的信息*/<br> recv(sockfd,buff,sizeof(buff),0);<br> printf("receive message from server: %s/n",buff);<br><br> while(1) {<br> memset(buff,0,sizeof(buff));<br> /*从标准输入设备取得字符串*/<br> printf("please input one string to send : "<br> scanf("%s",buff); <br><br> if (send(sockfd,buff,sizeof(buff),0)<0) {<br> printf("socket send error!/n"<br> exit(1);<br> }<br><br> memset(buff,0,sizeof(buff));<br> reclen=recv(sockfd,buff,sizeof(buff),0);<br> if (reclen>0)<br> printf("receive message from server: %s/n/n",buff);<br><br> }//while<br><br> close(sockfd);<br><br> UnInitWinSock();<br> return 0;<br>}<br><br>//-----------------------------------------------------------------------------