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