0

So after I got the problems here done, I encountered a new one :(

What I am trying to do:

I want to stream a camera frame to a certain IP/URL and be able to open it through a browser in the same network - using send() to get the images to the connected socket.

Problem:

The streaming itself works when I enter the IP in my browser without any problems, however when the IP is entered from another device in the same network the camera images itself get transmitted super slowly (the program stutters itself -> i'm showing images of the camera itself when someone connects and it starts getting slow once it's from another device).

Once the other device cuts the connection (aka closes the tab where the IP has been entered) the camera/application works just fine again.

Can the data transmission be slowing down the programm itself so much as well? Or what might be the reason for that?

Here's the server part I'm using (got most of it from a tutorial):

// initialize winsock
rc=WSAStartup(MAKEWORD(2,0),&wsa);

if(rc!=0)
{
    printf("Error: startWinsock, Errorcode: %d\n",rc);
    return 1;
}
else
{
    printf("Winsock initialized!\n");
}

// Create socket
acceptSocket=socket(AF_INET,SOCK_STREAM, IPPROTO_IP);

if(acceptSocket==INVALID_SOCKET)
{
    printf("Error: Socket-Creation failed, Errorcode: %d\n",WSAGetLastError());
    return 1;
}
else
{
    printf("Socket succesfully created!\n");
}

memset(&addr,0,sizeof(SOCKADDR_IN));
addr.sin_family=AF_INET;
addr.sin_port=htons(1234);
addr.sin_addr.s_addr=ADDR_ANY;
rc=bind(acceptSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));

if(rc==SOCKET_ERROR)
{
    printf("Error: bind, Errorcode: %d\n",WSAGetLastError());
    return 1;
}
else
{
    printf("Socket bound to port 1234\n");
}
rc=listen(acceptSocket,10);

if(rc==SOCKET_ERROR)
{
    printf("Error: listen, Errorcode: %d\n",WSAGetLastError());
    return 1;
}
else
{
    printf("acceptSocket is now in listen mode...\n");
}

for(i=0; i<MAX_CLIENTS; i++)
{
    clients[i]=INVALID_SOCKET;
}

SOCKET connectedSocket=accept(acceptSocket,NULL,NULL);

if(connectedSocket==INVALID_SOCKET)
{
    printf("Error, Errorcode: %d\n",WSAGetLastError());
    return 1;
}
else
{
    printf("New connection accepted!\n");
}
//socket stuff end

I'm also trying to get a multi-client access working and currently have no idea how to implement it in the code I have. I tried following some tutorials (e.g. http://www.binarytides.com/code-tcp-socket-server-winsock/) but it didn't work for me so far.

Community
  • 1
  • 1
Appuru
  • 417
  • 1
  • 6
  • 16
  • 1
    i solved a [similar problem](https://github.com/berak/opencv_smallfry/blob/master/mjpg_serve.cpp) using select() – berak Sep 19 '14 at 10:08
  • Really helpful code you got there. Used your class in my code now and everything seems to be working quite fine. Even the "lag" seems to be gone for now. Thanks a lot! – Appuru Sep 19 '14 at 11:02
  • I guess you could post that comment as an answer as well so I can tick it :) my questions are pretty much solved that way. – Appuru Sep 19 '14 at 11:52
  • select() solves the blocking problem, but the code still has a lot of warts, so , hmmm... – berak Sep 19 '14 at 11:55
  • yeah well I tried to use select() before as well, my code looked pretty similar to yours before in general. My select function differed in not using the highest socked value (maxfd) but just 0 which I read should work as well. Else I don't see too much of a difference (except for a better readability as you used a separate class for the whole mjpeg streaming). I stumbled upon one more problem though: right now it seems to only work with firefox/nightly but not with chrome/IE. Any idea on how to fix that? – Appuru Sep 19 '14 at 12:41
  • not really. that is also the weak part of my code. one probably should write a proper html page, containing an image first, then reload that image instead of writing images only.. – berak Sep 19 '14 at 12:44
  • That might be the right approach. I'll try reading up some more on it... I already read about someone having a similar problem (http://www.cplusplus.com/forum/unices/31142/), gotta check it out myself. – Appuru Sep 19 '14 at 13:04

0 Answers0