Unix服务器--Windows客户端,连接失败

时间:2013-10-06 20:50:22

标签: c++ c sockets client-server winsock

为了传达服务器unix进程和客户端窗口进程,还需要什么吗?编译后,我运行服务器,然后运行客户端。但是,客户端在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;
}

server(unix application):

#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应用程序时,端口是可以的。另外,我没有安装防病毒软件。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您收到连接拒绝错误,因为客户端连接到客户端计算机,而不是连接到服务器计算机。

在客户端代码中,将地址127.0.0.1替换为服务器的地址。