Winsock Client - Connecting to a Network Camera via TCP/IP4

Hi,
first of: this is my first post on Cplusplus (please let me if this is the wrong sub forum, sorry) and please excuse my writing since english is not my native language.

So, im trying to control a highspeed camera with a windows application using winsock (client-server chat). The camera is connected via ethernet and it does accept TCP connections to a given port. A control stream is implemented as TCP stream socket.

Control Stream Syntax: "All communication over the control streams is done in text (ASCII) format. The commands are formatted so that it is reasonable for a human to type commands and interpret the results.
The control stream command interpreter reads command lines and produces re-
sponse lines. For each command line there is exactly one reponse line. Receiving
a newline causes the command interpreter to attempt to execute all input since the last newline as a command. After the response (or error) is generated, the command interpreter returns to it’s base state.
Command lines are limited to a total length of 65536 bytes (including the newline). All response lines are guaranteed not to exceed the same length."

I already was able to establish a telnet connection to the camera and send /receive messages.
BUT: my c++ client application wont receive or send any messages. It says the connection is established successfully though.

Im using Windows 10 and Visual Studio 2015; my Code:

///////////////////////////////////////////////////////////////////////////////

// TCP Client
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#define DEFAULT_BUFLEN 512

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <cstdio>
#include <iostream>

// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")

using namespace std;

int main()
{
int recvbuflen = DEFAULT_BUFLEN;
char sendbuf[512];
char recvbuf[DEFAULT_BUFLEN];
long rc;

WSADATA wsaData;
SOCKET sConnect = INVALID_SOCKET;
sockaddr_in serv_addr;

// ws2_32.dll activate
rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (rc == 0)
cout << "WSAStartup()\t\t successful" << endl;
else
cout << "error WSAStartup(): " << WSAGetLastError() << endl;

// socket configuration
sConnect = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //AF_INET for IP4
if (sConnect != INVALID_SOCKET)
cout << "socket() \t\t successful" << endl;
else
cout << "error socket(): " << WSAGetLastError() << endl;

// connection info
serv_addr.sin_addr.s_addr = inet_addr("x.x.x.x"); //Camera IP
serv_addr.sin_family = AF_INET; //IP4
serv_addr.sin_port = htons(7200); //given Port
int conparlen = sizeof(serv_addr);


// connecting to server
rc = connect(sConnect, (struct sockaddr*)&serv_addr, conparlen);
if (rc != SOCKET_ERROR)
cout << "connect() \t\t successful" << endl;
else
cout << "not connected(): " << WSAGetLastError() << endl;

while (1)
{
//memset(&sendbuf, 0, sizeof(sendbuf)); Do I need this??
//memset(&recvbuf, 0, sizeof(recvbuf));

do {
rc = recv(sConnect, recvbuf, recvbuflen, 0);
if (rc > 0)
printf("Bytes received: %d\n", rc);
else if (rc == 0)
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while (rc > 0);

cout << "send: ";
cin.getline(sendbuf, 512);
//sendbuf[rc] = '\0';

rc = send(sConnect, sendbuf, (int)strlen(sendbuf), 0);
if (rc == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(sConnect);
WSACleanup();
return 1;
}

printf("Bytes Sent: %ld\n", rc);

}

closesocket(sConnect);
WSACleanup();
return 0;
}

///////////////////////////////////////////////////////////////////////////////

I dont get why its not working, am I missing something here? I appreciate any suggestions, thank you!

Cheers Finn
Topic archived. No new replies allowed.