网络编程实验2_(4)基于流式套接字的服务器程序设计

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <winsock2.h>

using namespace std;

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

#define PORT 7210
#define IP_ADDRESS "192.168.0.103"
#define MAX_LINE 10000

DWORD WINAPI ClientThread(LPVOID lpParameter)
{
    struct sockaddr_in ClientAddr;
    int AddrLen = sizeof(ClientAddr);
    SOCKET CientSocket = (SOCKET)lpParameter;
    int iResult = 0;
    char RecvBuffer[MAX_LINE];
    while (true)
    {
        memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
        iResult = recv(CientSocket, RecvBuffer, MAX_LINE, 0);
        getpeername(CientSocket, (struct sockaddr*) & ClientAddr, &AddrLen);
        if (iResult == 0 || iResult == SOCKET_ERROR)
        {
            cout << "客户" << inet_ntoa(ClientAddr.sin_addr) << ":" << ClientAddr.sin_port << "退出。" << endl;
            //客户端退出
            break;
        }
        //输出服务端收到的信息
        cout << "客户" << inet_ntoa(ClientAddr.sin_addr) << ":" << ClientAddr.sin_port << "说:" << RecvBuffer << endl;
        iResult = send(CientSocket, RecvBuffer, (int)strlen(RecvBuffer), 0);
        //将接收到的字符串回送给客户端
    }

    return 0;
}

int main(int argc, char* argv[])
{
    WSADATA  wsaData;
    SOCKET ServerSocket, ClientSocket;
    struct sockaddr_in LocalAddr, ClientAddr;
    int iReuslt = 0;
    int AddrLen = 0;
    HANDLE hThread = NULL;

    //Init Windows Socket
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        cout << "Init Windows Socket Failed::" << WSAGetLastError() << endl;
        WSACleanup();
        return -1;
    }

    //Create Socket
    ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ServerSocket == INVALID_SOCKET)
    {
        cout << "创建Socket失败::" << WSAGetLastError() << endl;
        closesocket(ServerSocket);
        WSACleanup();
        return -1;
    }

    LocalAddr.sin_family = AF_INET;
    LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
    LocalAddr.sin_port = htons(PORT);
    memset(LocalAddr.sin_zero, 0x00, 8);

    //Bind Socket
    iReuslt = bind(ServerSocket, (struct sockaddr*) & LocalAddr, sizeof(LocalAddr));
    if (iReuslt != 0)
    {
        cout << "Bind Socket Failed::" << WSAGetLastError() << endl;
        WSACleanup();
        return -1;
    }
    //listen
    iReuslt = listen(ServerSocket, 10);
    if (iReuslt != 0)
    {
        cout << "listen Socket Failed::" << WSAGetLastError() << endl;
        return -1;
    }

    cout << "服务端已经启动" << endl;

    while (true)
    {
        AddrLen = sizeof(ClientAddr);
        ClientSocket = accept(ServerSocket, (struct sockaddr*) & ClientAddr, &AddrLen);
        if (ClientSocket == INVALID_SOCKET)
        {
            cout << "Accept Failed::" << WSAGetLastError() << endl;
            break;
        }

        cout << "客户端连接::" << inet_ntoa(ClientAddr.sin_addr) << ":" << ClientAddr.sin_port << endl;

        hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)ClientSocket, 0, NULL);
        if (hThread == NULL)
        {
            cout << "Create Thread Failed!" << endl;
            break;
        }

        CloseHandle(hThread);
    }

    closesocket(ServerSocket);
    closesocket(ClientSocket);
    WSACleanup();

    return 0;
}

 

posted @ 2020-03-23 20:46  chuyaoxin  阅读(636)  评论(0编辑  收藏  举报