Unix服务器无法从Windows客户端接收数据

时间:2013-10-11 11:48:32

标签: c++ c sockets client-server

我正在开发一个简单的客户端 - 服务器应用程序。客户端进程是在Visual Studio 2012上运行的Windows应用程序(C ++)。服务器进程是通过Cygwin运行的Unix应用程序(C)。运行服务器后,我运行客户端。但是,服务器永远不会获取输入的数据客户端。具体来说,在服务器端,甚至没有打印“等待......”

客户端:

#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.
    char szPath[128] = "";
    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 serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv_addr.sin_port = htons(27010);

    // Connect to server.
    iResult = connect(ConnectSocket, (SOCKADDR *) &serv_addr, sizeof (serv_addr));
    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, 1000);

    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;
}

服务器:

#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>

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

    char Buffer[1025];

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

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

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

    listen(listenfd, 10); 

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

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

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

Cygwin已经安装了最小的基础包,加上gcc(GNU编译器集合),gcc核心(GNU编译器集合(C,OpenMP)),gdb(GNU调试器),make('make'的GNU版本)效用)。还有其他需要,还是有代码主题?

0 个答案:

没有答案