2

I have to following problem. Maybe someone could help me with that.

What I want do achieve is that a service inside a virtual machine (using virtualbox/WinXP) should send data to the host via socket (client)(1). The host (socketserver) then takes a snapshot of the current system(2), sends an ack(3) into the vm (again via socket), that every action was taken and that the service can continue(4).

Service                                               Host
=========                                               =======

Service sends specific data (1)

        ---------------------------------------->       
                                                        Invoke Snapshot (2)
| On restore socket gets destroyed  (X) |
| No ACK can be accepted -- Endless Loop|               Send ACK (3)

        <----------------------------------------

Accept ACK and continue (4)

The Problem occurs, when I restore the vm to a state, which was taken earlier. The software waits for the ack to continue. The vm takes some time to restore its network (3-5 seconds until "your network cable is now plugged in..." in the tray) and this crashes the sockets (X).

I don't have a workaround for that. The Service is written in C. Host is a python script. Sleep is the worst solution in my opinion. Due to high load, the time until something happens is not predictable.

I cannot come up with a great idea on how to solve that problem. Would be great if you could help me.

Thanks in advance!

EDIT:

@alk: I made the assumption, because the client is not connected to server anymore (different states due to restore, and losing the connection for a while I guess)

Here is the C Code from the Service. I hooked certain syscalls, and the code gets executed when a syscall is called

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

#pragma comment(lib,"ws2_32.lib") //Winsock Library

WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char buffer[1024];
char ack[1024];
int recv_size;

int mpex_send(const char *str, ...)
{
    // Build String
    va_list va;
    va_start(va, str);
    vsnprintf(buffer, sizeof(buffer), str, va);
    va_end(va);

    // Init
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");

    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");


    server.sin_addr.s_addr = inet_addr("192.168.56.1");
    server.sin_family = AF_INET;
    server.sin_port = htons( 42000 );

    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected");
     //Send some data
    if( send(s , buffer , strlen(buffer) , 0) < 0)
    {
        puts("Send failed");
        return 1;
    }
    puts("Data Send\n");

    //Receive a reply from the server
    while(1)
    {
        if((recv_size = recv(s , ack , 2000 , 0)) == SOCKET_ERROR)
        {
            puts("recv failed");
        }

        puts("Reply received\n");
        ack[recv_size] = '\0';
        puts(ack);
        // Important, put \n after ack
        if (strcmp("ack\n", ack) == 0)
        {
            puts("Got it");
            break;
        }
    }
    closesocket(s);
    WSACleanup();
    return 0;
}
4

1 回答 1

2

我设法解决了我的问题。也许有一天会遇到同样的事情,所以希望这会有所帮助。

发送来自 C 代码的第一条消息后,它将进入循环状态,尝试创建和打开套接字(参见上面的源代码)。如果他们成功了,他们就会崩溃;循环。之后,我循环一个 socket.recv 以从主机获取 ack。

在这种状态下,主机收到消息并进入 ack flooding 状态,向所有连接的客户端发送 ack。所以消息被发送到新的连接以及断开的连接。

C 代码收到 ack 后,向主机发送 ack_ack 以停止泛洪。

这个页面对 python 服务器部分非常有帮助。 http://www.binarytides.com/code-chat-application-server-client-sockets-python/

希望这可以帮助

于 2013-09-08T16:47:53.447 回答