select模型的示例代码

服务端的代码

#include <cstdlib>
#include <windows.h>
#include <iostream>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")

#define MAX 512

using namespace std;

volatile int g_iTotalConn=0;
SOCKET g_CliSocketArr[FD_SETSIZE];

DWORD PROCESS(LPVOID param)
{
    int i;
    fd_set fdread;
    int ret;
    struct timeval tv={2,0};
    char rzMessage[MAX];
    char szMessage[]="Server has got your message";
    for(;;){
        printf("This is a loop***********\n");
        FD_ZERO(&fdread);
        for(i=0 ; i<g_iTotalConn; i++){
            FD_SET(g_CliSocketArr[i],&fdread);
        }
        ret=select(0,&fdread,NULL,NULL,&tv);
        if(ret==0)
            continue;
        for(i=0 ; i<g_iTotalConn ; i++){
            if(FD_ISSET(g_CliSocketArr[i],&fdread)){
                ret=recv(g_CliSocketArr[i],rzMessage,MAX,0);
                if(ret==0 || (ret==SOCKET_ERROR && WSAGetLastError()==WSAECONNRESET)){
                    printf("Client socket %d closed.\n",g_CliSocketArr[i]);
                    closesocket(g_CliSocketArr[i]);
                    if(i<g_iTotalConn-1)
                        g_CliSocketArr[i--]=g_CliSocketArr[--g_iTotalConn];
                    else
                        g_iTotalConn--;
                }
                else{
                    szMessage[ret]='\0';
                    printf("A new Client:%s:%d connections\n",rzMessage,g_iTotalConn);
                    send(g_CliSocketArr[i],szMessage,strlen(szMessage)+1,0);
                }
            }
        }
    }
    return 0;
}

int main(int argc,char *argv[])
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2,2),&wsaData);
    int err_no;
    fd_set fdsocket;
    char sendbuf[]="Server has get your message!\n";
    char buf[MAX];
//    fd_set swrite;
//    fd_set ex;

    SOCKET sSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    
    if(sSocket==INVALID_SOCKET){
        cout<<"Invalid socket"<<endl;
    }
    
    sockaddr_in myaddr_in,youraddr_in;
    myaddr_in.sin_addr.s_addr=inet_addr("114.212.81.144");
    myaddr_in.sin_port=htons(8888);
    myaddr_in.sin_family=AF_INET;
    int addr_size=sizeof(sockaddr_in);
    err_no=bind(sSocket,(sockaddr*)(&myaddr_in),addr_size);
    if(err_no==SOCKET_ERROR){
        printf("bind failed,Erro:%d\n",WSAGetLastError());
        goto err;
    }    
    err_no=listen(sSocket,20);
    if(err_no==SOCKET_ERROR){
        printf("Error listening on socket,Erro:%d\n",WSAGetLastError());
        goto err;
    }
    DWORD tid;
    CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PROCESS,NULL,0,&tid);
    
    
    printf("Now start......\n");    
    for(;;){
        SOCKET cSocket=accept(sSocket,(sockaddr*)&youraddr_in,&addr_size);
        printf("Accepted client:%s:%d\n",inet_ntoa(youraddr_in.sin_addr),ntohs(youraddr_in.sin_port));
        g_CliSocketArr[g_iTotalConn++]=cSocket;
    }

/*
    SOCKET cSocket;
    cSocket=accept(sSocket,(sockaddr*)&youraddr_in,&addr_size);
    if(cSocket==INVALID_SOCKET){
        printf("accept failed!%d\n",WSAGetLastError());
        goto err;
    }else{
        printf("Accepted client:%s:%d\n",inet_ntoa(youraddr_in.sin_addr),ntohs(youraddr_in.sin_port));
    }
*/
//    DWORD tid;
//    CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PROCESS,NULL,0,&tid);
err:
    WSACleanup();
    system("pause");
    return 0;
}

客户端的代码

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <set>
#include <queue>
#include <deque>
#include <assert.h>
#include <ctime>
#include <algorithm>
#include <math.h>
#include <winsock.h>
#include <windows.h>
/*weakness*/

using namespace std;

#ifndef BYTE
#define BYTE char
#endif

#define myMain main

#define MAX 512

int myMain(int argc , char *argv[])
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2,2),&wsaData);
    int err_no;
    char buf[MAX];
    
    srand(unsigned(time(NULL)));
    int itemp=rand()%1000;
    char sendbuf[20];
    itoa(itemp,sendbuf,10);
    SOCKET cSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    
    if(cSocket==INVALID_SOCKET){
        cout<<"Invalid socket"<<endl;
    }
    
    sockaddr_in myaddr_in;
    myaddr_in.sin_addr.s_addr=inet_addr("114.212.81.144");
    myaddr_in.sin_port=htons(8888);
    myaddr_in.sin_family=AF_INET;
    
    err_no=connect(cSocket,(sockaddr*)&myaddr_in,sizeof(myaddr_in));
    if(err_no==SOCKET_ERROR){
        printf("connecting error,Erro:%d\n",WSAGetLastError());
        goto err;
    }
    
    for(;;){
        send(cSocket,sendbuf,strlen(sendbuf),0);        

        int result=recv(cSocket,buf,MAX,0);
        if(result>0){
            printf("%dBytes received:%s\n",result,buf);
        }
        else if(result==0)
            printf("Connection closed\n");
        else{
            printf("recv failed:%d\n",WSAGetLastError());
            goto err;
        }
        Sleep(3000);        
    }

err:    
    WSACleanup();
    system("pause");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值