C++ socket stuck sending data

1.9k views Asked by At

I'm trying to develop a streaming system sending multiple images to a receiving socket, which displays them. However, for some reason I can't understand, my code gets blocked at the send function after sending about 3 images. This is a snippet of my code so far:

portno = atoi(argv[2]);

sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) 
    error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, 
     (char *)&serv_addr.sin_addr.s_addr,
     server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
    error("ERROR connecting");
bzero(buffer,256);

for(int i=10;i<51;i++){  
    std::stringstream sstm;
    sstm << filename << i << ext;
    result = sstm.str();
    cout << result << endl;
    Mat image = imread(result, CV_LOAD_IMAGE_COLOR);
    image = (image.reshape(0,1));
    int imgSize = image.total()*image.elemSize();
    n = send(sockfd, image.data, imgSize, 0);
}

Tried to debug it, and, as I said, it gets blocket at the last line, the send function. I wonder if there's a limit on how much information can you transmit through a socket. If I move the for sentence back before the socket creation, it works like a charm, but i'm not going to create a bizillion sockets. Any help?

1

There are 1 answers

0
selbie On

The send() call will block if the TCP window size goes to zero. This is almost always a result of the other side of the connection not consuming the data from a recv() call.

It's also entirely possible that imgSize is extremely huge and it just simply takes that long for the receiver to consume the stream.

You didn't share any code from the receiver side, so it's difficult to say.

I would suggest more debug spew (print statements) showing the value of imgSize and the return value of the send call. Ditto for the recv side.

Do note - that just because you sent imgSize bytes in one send call, the remote receiver may not receiver all those bytes within a single call to recv().