0

tcp/ip アプリケーション プロジェクトがあり、ソース コードで winsock2.lib を使用しています。サーバーとクライアントの機能は次のとおりです。

サーバ:

#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include<string>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")
using namespace::std;



int server()
{
    string SERVER_ADDRESS= "172.17.11.138";
    int PORT_NUMBER = 27015;

    sockaddr_in server;
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT_NUMBER);
    server.sin_addr.S_un.S_addr=inet_addr(&SERVER_ADDRESS[0]);


    WSADATA wsaData;
        int iResult;
    SOCKET ListenSocket = INVALID_SOCKET;
        SOCKET ClientSocket = INVALID_SOCKET;

   // Initialize Winsock
       iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
       if (iResult != 0) {
       printf("WSAStartup failed with error: %d\n", iResult);
       return 1;
    }
    int iSendResult;
char recvbuf[256];
int recvbuflen = 256;

// Create a SOCKET for connecting to server
ListenSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
    printf("socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
}

// Setup the TCP listening socket
iResult = bind( ListenSocket, (struct sockaddr*)&server,sizeof(server));
if (iResult == SOCKET_ERROR) {
    printf("bind failed with error: %d\n", WSAGetLastError());
    closesocket(ListenSocket);
    WSACleanup();
    return 1;
}

iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
    printf("listen failed with error: %d\n", WSAGetLastError());
    closesocket(ListenSocket);
    WSACleanup();
    return 1;
}

// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
    printf("accept failed with error: %d\n", WSAGetLastError());
    closesocket(ListenSocket);
    WSACleanup();
    return 1;
}

// No longer need server socket
closesocket(ListenSocket);

// Echo the buffer back to the sender
iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
if (iSendResult == SOCKET_ERROR) {
    printf("send failed with error: %d\n", WSAGetLastError());
    closesocket(ClientSocket);
    WSACleanup();
    return 1;
}
}

クライアント:

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>


#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")


int client()
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
string SERVER_ADDRESS= "172.17.11.138";

char *sendbuf = "this is a test";
char recvbuf[256];
int PORT_NUMBER = 27015;
int iResult;

sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(PORT_NUMBER);
server.sin_addr.S_un.S_addr=inet_addr(&SERVER_ADDRESS[0]);
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
    printf("WSAStartup failed with error: %d\n", iResult);
    return 1;
}

// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
    printf("socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
}

iResult = connect( ConnectSocket,(struct sockaddr*)&server,sizeof(server));
if (iResult == SOCKET_ERROR) {
    closesocket(ConnectSocket);
    printf("socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
}
if (ConnectSocket == INVALID_SOCKET) {
    printf("Unable to connect to server!\n");
    WSACleanup();
    return 1;
    }

 iResult = recv(ConnectSocket, recvbuf, 256, 0);
    if ( iResult > 0 )
        printf("Bytes received: %d\n", iResult);
    else if ( iResult == 0 )
        printf("Connection closed\n");
    else
        printf("recv failed with error: %d\n", WSAGetLastError());
}

上記のコードをコンパイルすると、サーバーとクライアントは相互に接続できます (tcpview アプリケーションで確認) が、サーバーからクライアントにデータを送信したい場合、winsock2 はエラー 10057 を返します (リッスン ソケットを閉じると、エラーは 10038 になります) ) また、send 関数は -1 を返しました。

4

1 に答える 1