zoukankan      html  css  js  c++  java
  • I/O Socket编程

    服务器端


    #include<WINSOCK2.H>

    #include<iostream>
    using namespace std;
    #include<stdlib.h>
    #define BUF_SIZE 64
    #pragma comment(lib,"WS2_32.lib")
    int main()
    {
    WSADATA wsd;
    SOCKET sServer;
    SOCKET sClient;
    int retVal;//调用Socket函数的返回值
    char buf[BUF_SIZE];    
    //初始化Socket环境
    if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
    {
    printf("WSAStartup failed! ");
    return 1;
    }
    //创建监听的Socket
    sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(INVALID_SOCKET==sServer)
    {
    printf("socket failed! ");
    WSACleanup();
    return -1;
    }
    //设置服务器Socket地址
    SOCKADDR_IN addrServ;
    addrServ.sin_family=AF_INET;
    addrServ.sin_port=htons(9990);
    addrServ.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    //绑定Sockets Server
    retVal=bind(sServer,(const struct sockaddr*)&addrServ,sizeof(SOCKADDR_IN));
    if(SOCKET_ERROR==retVal)
    {
    printf("bind failed! ");
    closesocket(sServer);
    WSACleanup();
    return -1;
    }
    //在Sockets Server上进行监听
    retVal=listen(sServer,1);
    if(SOCKET_ERROR==retVal)
    {
    printf("listen failed! ");
    closesocket(sServer);
    WSACleanup();
    return -1;
    }
    //设置Socket为非阻塞模式
    int iMode = 1;
    retVal = ioctlsocket(sServer,FIONBIO,(u_long FAR*) &iMode);
    if(retVal == SOCKET_ERROR)
    {
    printf("ioctlsocket failed! ");
    WSACleanup();
    return -1;
    }
    //接受来自客户端的请求
    printf("TCPServer start... ");
    sockaddr_in addrClient;
    int addrClientlen=sizeof(addrClient);
    while(true)
    {
    sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
    if(INVALID_SOCKET == sClient)
    {
    int err = WSAGetLastError();
    if(err == WSAEWOULDBLOCK)
    {
    Sleep(5);
    continue;
    }
    else
    {
    printf("accept failed! ");
    closesocket(sServer);
    WSACleanup();
    return -1;
    }
    }
    break;
    }
    while(true)
    {
    ZeroMemory(buf,BUF_SIZE);
    retVal = recv(sClient,buf,BUF_SIZE,0);
    if(SOCKET_ERROR == retVal)
    {
    int err = WSAGetLastError();
    if(err == WSAEWOULDBLOCK)
    {
    Sleep(5);
    continue;
    }
    else if(err == WSAETIMEDOUT || err == WSAENETDOWN)
    {
    printf("recv failed! ");
    closesocket(sServer);
    closesocket(sClient);
    WSACleanup();
    return -1;
    }
    }
    SYSTEMTIME st;
    GetLocalTime(&st);
    char sDateTime[30];
    sprintf(sDateTime,"%4d-%2d-%2d-%2d:%2d:%2d:%2d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
    //打印输出的信息
    printf("%s,Recv From Client [%s:%d]:%s ",sDateTime,inet_ntoa(addrClient.sin_addr),addrClient.sin_port,buf);
    //如果客户端发送quit字符串,则服务器退出
    if(strcmp(buf,"quit")==1)//////////////////////////////////////////
    {
    retVal=send(sClient,"quit",strlen("quit"),0);
    break;
    }
    //否则向客户端发送回显字符串
    else
    {
    char msg[BUF_SIZE];
    sprintf(msg,"Message received - %s ",buf);
    while(true)
    {
    retVal = send(sClient,msg,strlen(msg),0);
    if(SOCKET_ERROR == retVal)
    {
    int err = WSAGetLastError();
    if(err == WSAEWOULDBLOCK)
    {
    Sleep(5);
    continue;
    }
    else
    {
    printf("send failed! ");
    closesocket(sServer);
    closesocket(sClient);
    WSACleanup();
    return -1;
    }
    }
    }
    break;
    }
    }

    }







    客户端




    客户端

  • 相关阅读:
    unity的#pragma strict,#pragma downcast等指令分享
    Unity3d 添加多相机后编译警告
    Invoke计时器
    unity3d UI自动适合屏幕分辨率
    实现卷轴效果的脚本
    .unity3d格式的导出与加载
    Linux 网络编程
    姿态解算基本完成,程序编写笔记
    验证网络上四元数的正确性
    2440 模拟IIC 可以读取 L3G4200D ,ADXL345
  • 原文地址:https://www.cnblogs.com/zhangaihua/p/3718087.html
Copyright © 2011-2022 走看看