I'm using C++ as server and php as client, sending image via socket from server back to client. the main trouble that i receive image on client, but it's completely white. in html there are picture 1920*1080 without any picture. Can u suggest me where looking for a mistake ?
server part of image loading:
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d
", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d
", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return;
}
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("Bytes received: %d
", iResult);
/*send file*/
int fd;
int sockfd;
ssize_t nbytes;
char buf[LEN];
memset(buf,0, LEN+1);
int len;
FILE *in = fopen("desktop.png","rb");
while ((iResult = fread(buf,sizeof(buf),1, in)) > 0) {
send(ClientSocket,buf,sizeof(buf),0);
}
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d
", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return;
}
printf("Bytes sent: %d
", iSendResult);
}
else if (iResult == 0)
printf("Connection closing...
");
else {
printf("recv failed with error: %d
", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return;
}
} while (iResult > 0);
// shutdown the connection since we're done
//iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d
", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
so i'm packing image and send it to client.
client
function connect($host)
{
//$host = "127.0.0.1";
$port = 27001;
$message = " testing."
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server
");
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server
");
// get server response
$buf;
if (!socket_recv($socket, $buf, 4096, MSG_WAITALL)) {
echo "Socket Receive Error...
";
$error_msg = 'Unable to receive data: ' . socket_strerror(socket_last_error());
} else {
echo 'BIND_TRANSMITTER Response: ' . var_dump($buf) . "
";
}
$imgData = base64_encode($buf);
$img = "<img src= 'data:image/png;base64, $imgData' />";
print($img);
socket_close($socket);
}
When i'm looking in html i see next details:
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAgAElEQVR4nOy9edBlx1Un+DuZ97731aoq7SpJJcmyLWspy5LKsizbWLKRjTHGLHa4aXDgaKLHjYkYuiGgHTFBx4yBwTTQ000PZhimB3qgGxjongAM2NggwIu8YSzJ1m5rqdKuUi3fV/V9792beeaPc05m3vvu+7aqsmUgK756790ll5Mnz34y6dt/8iOMDZQwPYHp8lG0zQo4Drw6VBvNr4/nvMRcvkbpKe4/ynHVpjm9373LPPzC8GUebCD9ZIb8K+vm4jltnzxcPcZoYSd8vTW9IY9yp/qo74fJCTTLi4jtBGxj5fw0F++XdZVd5t73wQnn7nhiOQQuxla+otcJADN1bjLzTDvd8fbGbBUVbdAgLpXtkL7DM/eJSH86uGoMN94JV2/V5ynhFoHAlF8lALFZRpwugttJB7/6iNzvSnesuf6h+6ssiZkH1/3sOgv3vxt+cP6dcYWLZ7j3DHcqYgi8UY3hRjtB9ZaB8fRWNQHcnECcLIJDH97Fc+sqQ0DOfUz4xDaqmAgBcQAzS/v6l36DM8Fg7o2bO2NaT7H3Ka0RyldnCFz5Xnc8gMLbjeDqbUC10HmSiv9L0HC7Ap4eB8fpILzXMYCTK31w2e+TrJfm/ih+ll+IQLpCiUg=">
So picture is on page with it's width and height, and there is a rectangle with that picture, but it don't displayed, only white background. I don't understand where is my mistake, can you please suggest me something ?
*UPDATE* i'm change my server send block to.
while ((len = fread(buf,sizeof(buf),1, in)) > 0) {
send(ClientSocket,buf,len,0);
}
now i think trouble on client, cause on server it's write error : recv error failed with error 10053