Hi Guys,
I have been asked to create a module that receives images over a network and some saple code that works, my problem is the sample code is C++ and i have tried to understand it but with not much success.
The code is only 91 lines long with line spaces (see below):
I know the IP Address of the Server 172.31.16.17 and the port should be 7096 that it receives the data on. But looking at the C++ code i cant see where it opens a TCP connection with these attributes, i can see the port number, but can not get my head around this.
I have tried creating some small VB apps using TCPCLient and Network Streams but the different ways i have tried all come wont connect.
Is there an easy way to understand how the C++ program is connecting when no IP Has been passed??
Any help is very much appreciated.
Regards
Stu
I have been asked to create a module that receives images over a network and some saple code that works, my problem is the sample code is C++ and i have tried to understand it but with not much success.
The code is only 91 lines long with line spaces (see below):
VB.NET:
#include "stdafx.h"
#include <winsock2.h>
struct ImageRxHeader
{
char m_token[6];
unsigned short m_headerCrc; // CRC of rest of header
unsigned short m_headerVersion;
unsigned long m_headerLength; // octets
unsigned long m_dataLength; // octets
unsigned long m_timeSecs;
unsigned long m_timeNanoSecs;
unsigned short m_diffTime; // msecs
unsigned long m_maxNum; // packets
unsigned long m_actNum; // packets
unsigned long m_streamLength; // octets
unsigned char m_device;
unsigned char m_channel;
unsigned long m_deviceIp;
unsigned long m_devicePort;
char m_datatype[16];
char m_status[80];
};
DWORD WINAPI clientThread(void *context)
{
SOCKET s = (SOCKET)context;
ImageRxHeader vlh;
int err = recv(s,(char *)&vlh,68,0);
char *pBuf = new char [vlh.m_dataLength];
char *pData = pBuf;
unsigned int bytesLeft = vlh.m_dataLength;
unsigned int count = 0;
while ((err = recv(s,pData,bytesLeft,0)) > 0)
{
count += err;
pData += err;
bytesLeft -= err;
}
bool fileWritten = false;
int index = 0;
do
{
char fileName[80];
sprintf(fileName,"c:\\test%04d.png",index++);
unsigned long attrib = GetFileAttributes(fileName);
if (attrib == INVALID_FILE_ATTRIBUTES)
{
FILE *pf = fopen(fileName,"wb");
fwrite(pBuf,1,vlh.m_dataLength,pf);
fclose(pf);
fileWritten = true;
}
} while (!fileWritten);
delete pBuf;
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsd;
int err = WSAStartup(MAKEWORD(2,2),&wsd);
SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = 0;
sa.sin_port = htons(7096);
bind(s,(const sockaddr *)&sa,sizeof(sa));
listen(s,5);
bool keepGoing = true;
while (keepGoing)
{
sockaddr_in clientAddr;
int cliLen = sizeof(clientAddr);
SOCKET clientSocket = accept(s,(sockaddr *)&clientAddr,&cliLen);
DWORD threadId;
CreateThread(0,0,clientThread,(void *)clientSocket,0,&threadId);
}
return 0;
}
I know the IP Address of the Server 172.31.16.17 and the port should be 7096 that it receives the data on. But looking at the C++ code i cant see where it opens a TCP connection with these attributes, i can see the port number, but can not get my head around this.
I have tried creating some small VB apps using TCPCLient and Network Streams but the different ways i have tried all come wont connect.
Is there an easy way to understand how the C++ program is connecting when no IP Has been passed??
Any help is very much appreciated.
Regards
Stu