客户端无法连接

时间:2011-06-21 20:02:48

标签: c windows sockets client-server

我是初学者/初学者程序员,在使用简单的客户端/服务器C代码时遇到问题。我的最终目标是将一个方位/高程数据的“流”从服务器发送到客户端,然后在收到数据时将其转换(它只是一个分区,但我真的不知道如何做到这一点或者)转换为平移/倾斜单元的位置数据,然后通过串行输出转换后的数据到平移/倾斜头。 (我可能会稍后回来询问......)

现在我只想弄清楚如何发送和接收数据。我从这个网站上抓了代码。 http://www.tenouk.com/Winsock/Winsock2example3.html。我不得不移动一些声明来获取编译代码。

我在客户端电脑上使用Windows 7和VS2010专业版。客户端和服务器之间没有路由器(它们通过以太网直接连接)。

使用调试器,我发现此时我已经挂断了。

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("12.266.66.255");
clientService.sin_port = htons(55555);

if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
    printf("Client: connect() - Failed to connect.\n");
    WSACleanup();
    return 0;
}

else
{
   printf("Client: connect() is OK.\n");
   printf("Client: Can start sending and receiving data...\n");
}

我总是收到“联系失败”的消息,我不知道为什么。我正在使用主机的正确IP地址(我在更改之前更改了它)。

如果这是一个不好的例子,我愿意从另一个例子开始。我尝试过在网上常见的几个'echo'示例,我遇到了类似的问题。我可以提供有关我的整体计划目标的更多信息,如果这会有所帮助。其余的客户端代码如下。我正在使用来自同一链接的服务器代码(带有声明的移动)。感谢。

int main()
{

int m_socket;

struct sockaddr_in clientService;

int bytesSent;

int bytesRecv = SOCKET_ERROR;
// Be careful with the array bound, provide some checking mechanism...
char sendbuf[200] = "This is a test string from client";
char recvbuf[200] = "";
// Initialize Winsock.

WSADATA wsaData;

int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
          printf("Client: Error at WSAStartup().\n");
else
          printf("Client: WSAStartup() is OK.\n");

// Create a socket
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET)
{
    printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError());
    WSACleanup();
    return 0;
}

else
   printf("Client: socket() is OK.\n");

// Connect to a server.
// Just test using the localhost, you can try other IP address
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("12.233.21.254");
clientService.sin_port = htons(55555);

if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
    printf("Client: connect() - Failed to connect.\n");
    WSACleanup();
    return 0;
}

else
{
   printf("Client: connect() is OK.\n");
   printf("Client: Can start sending and receiving data...\n");
}

// Send and receive data.
   // Receives some test string to server...

   while(bytesRecv == SOCKET_ERROR)
   {
       bytesRecv = recv(m_socket, recvbuf, 200, 0);

    if (bytesRecv == 0 || bytesRecv == WSAECONNRESET)
    {
         printf("Client: Connection Closed.\n");
        break;
    }

    if (bytesRecv < 0)
        return 0;

   else
   {
          printf("Client: recv() is OK.\n");
          printf("Client: Received data is: \"%s\"\n", recvbuf);
          printf("Client: Bytes received is: %ld.\n", bytesRecv);

   }

}

   // Sends some test data to server...
   bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0);

   if(bytesSent == SOCKET_ERROR)
          printf("Client: send() error %ld.\n", WSAGetLastError());

   else
   {
          printf("Client: send() is OK - Bytes sent: %ld\n", bytesSent);
          printf("Client: The test string sent: \"%s\"\n", sendbuf);
   }

WSACleanup();

return 0;

}

2 个答案:

答案 0 :(得分:3)

您的原始代码(在我更改IP和端口之后,很明显)连接到我的网络服务器就好了,但我做了一点调整(下面)。

虽然它可能有点多,但CSocketServer包含了大量优秀的WinSock代码,这些代码已被尝试并保持正确。

无论如何,此代码连接到我的本地Web服务器,发送了一个基​​本请求并收到了回复。

int WSATest()
{
    // Initialize Winsock.
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
    {
        printf("Client: Error at WSAStartup().\n");
        return 0;
    }

    // Create a socket
    SOCKET m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (m_socket == INVALID_SOCKET)
    {
        printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError());
        WSACleanup();
        return 0;
    }
    else {
        printf("Client: socket() is OK.\n");
    }

    struct sockaddr_in clientService;
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
    clientService.sin_port = htons(9990);

    //Connect to the remote peer:
    if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
    {
        printf("Client: connect() - Failed to connect.\n");
        WSACleanup();
        return 0;
    }
    else
    {
        printf("Client: connect() is OK.\n");
        printf("Client: Can start sending and receiving data...\n");
    }

    //Very, very basic HTTP request.
    int iSendResult = send(m_socket, "GET / \n\n", 8, 0);
    if(iSendResult == SOCKET_ERROR)
    {
        printf("Failed to send data, error %d.\n", WSAGetLastError());
        return 0;
    }

    while(true)
    {
        char sRecvBuffer[200];
        int iRecvResult = recv(m_socket, sRecvBuffer, sizeof(sRecvBuffer) - 1, 0);

        if(iRecvResult <= SOCKET_ERROR)
        {
            printf("Failed to receive data, error %d.\n", WSAGetLastError());
            break;
        }
        else if(iRecvResult == 0)
        {
            //Graceful disconnect.
            break;
        }
        else {
            //Be sure to terminate the buffer.
            sRecvBuffer[iRecvResult] = '\0';
        }

        printf("Received: [%s] for [%d] bytes.\n", sRecvBuffer, iRecvResult);
    }

    WSACleanup();

    return 0;
}

int main()
{
    WSATest();
    system("Pause");
}

答案 1 :(得分:1)

为什么不添加WSAGetLastError()来检查实际错误?