CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2016
    Posts
    3

    Socket Programming -- Sending image and received notice.

    Hi there,

    I am trying to do socket programming between client and server.
    Client Side will send an image written in python and server side will received this image and store it. Then send ACK for the client.

    I run in one error but because I am not programmer, I doubt my code structure. Also, I am using opencv on both sides.

    This is my server code c++

    #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[1024];
    int n;

    int serverSock=socket(AF_INET, SOCK_STREAM, 0);

    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 */
    bind(serverSock, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr));


    // wait for a client
    /* listen (this socket, request queue length) */
    listen(serverSock,1);

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

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

    while (1 == 1) {
    bzero(buffer, 1024);

    //char *buff = (char*)malloc(sizeof(char) * (240*360));
    FILE *output;
    output = fopen("test.jpg", "wb");
    unsigned int readBytes = 0;
    while(true)
    {
    int ret = recv(clientSock, buffer+readBytes, (240*360)-readBytes, 0);
    if (ret <= 0)
    {
    break;
    }
    readBytes += ret;
    }
    fwrite(buffer, sizeof(char), readBytes, output);
    fclose( output );

    //Mat img_2 = imread( "test.jpg");

    //receive a message from a client
    n = read(clientSock, buffer, 500);
    cout << "Confirmation code " << n << endl;
    cout << "Server received: " << buffer << endl;

    strcpy(buffer, "test");
    n = write(clientSock, buffer, strlen(buffer));
    cout << "Confirmation code " << n << endl;
    }
    return 0;
    close(serverSock);
    }

    This is my client code python

    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)

    client_socket.connect((HOST, PORT))


    fpath ='/Users/salemalqahtani/Desktop/NetworkingPart/sss.png'
    pic = open(fpath, 'rb')
    chunk = pic.read(1024)
    client_socket.send(str.encode("STORE " + filePath))
    t = time()
    while chunk:
    print("Sending Picture")
    client_socket.send(chunk)
    chunk = pic.read(1024)
    pic.close()
    print("Done sending")
    print("Elapsed time = " + str(time() - t) + 's')
    client_socket.close()
    return "Done sending"

    Please help me out.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Socket Programming -- Sending image and received notice.

    Quote Originally Posted by salem alqahtani View Post
    I run in one error but because I am not programmer, I doubt my code structure. Also, I am using opencv on both sides.
    And what is that error?
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2016
    Posts
    3

    Re: Socket Programming -- Sending image and received notice.

    This is the error in the python side

    client_socket.send(chunk)
    ^
    IndentationError: unindent does not match any outer indentation level

  4. #4
    Join Date
    Jan 2016
    Posts
    3

    Re: Socket Programming -- Sending image and received notice.

    Quote Originally Posted by salem alqahtani View Post
    This is the error in the python side

    client_socket.send(chunk)
    ^
    IndentationError: unindent does not match any outer indentation level
    Now I solved this issue but When I run the code on both client and server nothing happened and it said
    client_socket.connect((HOST, PORT))
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
    socket.error: [Errno 60] Operation timed out

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Socket Programming -- Sending image and received notice.

    I assume your server is not listening on port 50007, since you #define'd SERVER_PORT htons(50007).
    You can verify this by using TcpView (or netstat) and check what port your server is listening on. Try omitting the 'htons' and see if this changes the behavior.

  6. #6
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Socket Programming -- Sending image and received notice.

    Hmmm well if you have never done socket programming before, I suggest starting simple. Don't try to run before you crawl.

    Here is a good book on the subject:
    http://www.amazon.com/Pocket-Socket-.../dp/1558606866

    FYI You could FTP files from your client program to your server. This solution will probably be more robust if your trying to use it in a real business solution.
    Last edited by ahoodin; January 26th, 2016 at 08:27 AM.
    ahoodin
    To keep the plot moving, that's why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured