-1

サーバーの UNIX プロセスとクライアントの Windows プロセスを通信するために他に必要なものはありますか? 両方をコンパイルした後、サーバーを実行してからクライアントを実行します。ただし、クライアントは connect() でエラー: 10061 で失敗します。

クライアント (Windows アプリケーション):

#ifndef UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN

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

// Need to link with Ws2_32.lib.
#pragma comment(lib, "ws2_32.lib")

int wmain()
{

    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR) {
        printf("WSAStartup() failed with error: %d\n", iResult);
        return 1;
    }

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

    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.
    sockaddr_in Service;
    memset(&Service, 0, sizeof(Service));
    Service.sin_family = AF_INET;
    Service.sin_addr.s_addr = inet_addr("127.0.0.1");
    Service.sin_port = htons(27015);

    // Connect to server.
    iResult = connect(ConnectSocket, (SOCKADDR *) &Service, sizeof (Service));
    if (iResult == SOCKET_ERROR) {
        printf("connect() failed with error: %ld\n", WSAGetLastError());
        iResult = closesocket(ConnectSocket);
        if (iResult == SOCKET_ERROR)
            printf("closesocket() failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // Message that has to be sent.
    char message[1000];

    printf("\nEnter message: ");
    gets_s(message);

    printf("Message you wrote is: %s\n", message);

    // Send a message.
    if (send(ConnectSocket, message, sizeof(message), 0) == SOCKET_ERROR)
    {
        printf("send() failed with error code: %d\n", WSAGetLastError());
    }
    printf("Message successfully sent to server.");

closesocket(ConnectSocket);
WSACleanup();

while(1);

return 0;
}

サーバー (UNIX アプリケーション):

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <gnu/stubs-64.h>

int main(int argc, char *argv[])
{   
    int n;
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr; 

    char sendBuff[1025];

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(27015); 

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

    listen(listenfd, 10); 

    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 

        n = read(connfd,sendBuff,255);
        printf("Here is the message: %s\n",sendBuff);

        close(connfd);
        sleep(1);
     }
}

クライアントは Windows 7 で実行され、サーバーは Fedora 19 (VMware) で実行されます。クライアントサーバーWindowsアプリケーションを実行すると、ポートは問題ありませんでした。また、ウイルス対策SWも導入していません。どんな助けでも大歓迎です。

4

1 に答える 1

1

クライアントがサーバー マシンではなくクライアント マシンに接続しているため、接続拒否エラーが発生しています。

クライアント コードで、アドレス 127.0.0.1 をサーバーのアドレスに置き換えます。

于 2013-10-06T20:57:08.990 に答える