Socket Programming Send and receive images

Hi there,

This codes below is for server c++ and client python. I am sending an image form client to server and server. When I send an image for server I found an image with 0 bytes. also, server gave this error ERROR reading from socket: Undefined error: 0

Please Help me out.

This is server code:

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

#define SERVER_PORT htons(50007)

int main() {

char buffer[677633];
int n;

/* First call to socket() function */
int serverSock=socket(AF_INET, SOCK_STREAM, 0);

if (serverSock < 0)
{
perror("ERROR opening socket");
exit(1);
}

sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = SERVER_PORT;
serverAddr.sin_addr.s_addr = INADDR_ANY;

/* bind (this socket, local address, address length)
bind server socket (serverSock) to server address (serverAddr).
Necessary so that server can use a specific port */

/* Now bind the host address using bind() call.*/
if (bind(serverSock, (struct sockaddr *)&serverAddr, sizeof(sockaddr)) < 0)
{
perror("ERROR on binding");
exit(1);
}



/* Now start listening for the clients, here process will
* go in sleep mode and will wait for the incoming connection
*/
// wait for a client
/* listen (this socket, request queue length) */
listen(serverSock,5);

sockaddr_in clientAddr;
socklen_t sin_size=sizeof(struct sockaddr_in);

int clientSock=accept(serverSock,(struct sockaddr*)&clientAddr, &sin_size);

if (clientSock < 0)
{
perror("ERROR on accept");
exit(1);
}

/* If connection is established then start communicating */

// while (1 == 1) {
// bzero(buffer, 677633);
//char *buff = (char*)malloc(sizeof(char) * (240*360));
FILE *output;
output = fopen("test.png", "wb");
unsigned int readBytes = 0;
while(true)
{
char filename[1024];
int ret = recv(clientSock, filename+readBytes, sizeof(filename), 0);
if(filename[0] == '.'){
break;
}
if (ret <= 0)
{
perror("ERROR reading from socket");
exit(1);
}
readBytes += ret;
}
fwrite(buffer, sizeof(char), readBytes, output);
fclose( output );

// }
return 0;
close(serverSock);
}




This is a client code:

import socket
import sys,os
import cv2
import numpy as np
import time

HOST, PORT = "localhost", 50007

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create Socket

client_socket.connect((HOST, PORT)) # Connection

fpath ='/Users/salemalqahtani/Desktop/NetworkingPart/ss.png'

size = os.path.getsize(fpath)
print size

client_socket.send(str.encode("STORE " + fpath))

t = time.time()

pic = open(fpath,'rb')
while True:
strng = pic.readline(512)
if not strng:
break
client_socket.send(strng)
pic.close()

print("Done sending")
print("Elapsed time = " + str(time.time() - t) + 's')
client_socket.close()


Sincerely
Salem
Are you interested in learning how to do it yourself, or do you just want something that works?
Topic archived. No new replies allowed.