// This is My Client TCP program in C++
// Program Name client.cpp

#include <winsock2.h>
#include <iostream.h>

#include <windows.h>
#include <iomanip>


char szServerIPAddr[ 20 ] = "192.168.5.251" ; // IP address of my Server
int nServerPort = 5000 ; // The server port that will be used by clients for communication

int var1;
bool InitWinSock2_0( ) ;
int main( )

{

unsigned char str[12] = 
{0xAA,0xFF,0xC0,0x00,0x64,0x07,0xB7,0x98,0x2F,0x4D ,0x08,0x02}; 

// Querry Request to the Server

char tst[12] ={0}; 
char szBuffer[12] = {0};
char qzBuffer[12] = {0};

int q = 0;

while (q<12)
{
szBuffer[q] = str[q];
q = q++;
}

q=0;

printf("\n \n Actual Data Bytes To Be Send \n \n ");

while(q<12)
{
printf(" %c \n", szBuffer[q]);
q = q++;
}

Sleep(1000);


if ( ! InitWinSock2_0( ) )
{
cout << "Unable to Initialize Windows Socket environment" << WSAGetLastError( ) << endl ;
return -1 ;
}

SOCKET hClientSocket ;
hClientSocket = socket( AF_INET, // The address family. AF_INET specifies TCP/IP
SOCK_STREAM, // Protocol type. SOCK_STREM specified TCP
0 // Protoco Name. Should be 0 for AF_INET address family
) ;

if ( hClientSocket == INVALID_SOCKET )
{
cout << "Unable to create Server socket please verify the proper connection" << endl ;
// Cleanup the environment initialized by WSAStartup()
WSACleanup( ) ;
return -1 ;
}

//************************************************** *******************

// Create the structure describing various Server parameters


struct sockaddr_in serverAddr ;
serverAddr . sin_family = AF_INET ; // The address family. MUST be AF_INET
serverAddr . sin_addr . s_addr = inet_addr( szServerIPAddr ) ;
serverAddr . sin_port = htons( nServerPort ) ;


//************************************************** ********************
// For Testing 

Sleep(5000); // this command will cause the Delay in Milliseconds

//************************************************** ********************

// Connect to the server

if ( connect( hClientSocket, ( struct sockaddr * ) &serverAddr, sizeof( serverAddr ) ) < 0 )
{
cout << "\n \n Unable to connect to " << szServerIPAddr << " on port " << nServerPort << endl ;
closesocket( hClientSocket ) ;
WSACleanup( ) ;
return -1 ;
} 

int a = 1; 

/*do
{
cout << "\n \n Enter '1' to Start '0' to Wait: ";
cin >> a;

}
while(a==0);
*/

while (a==1)
{
int nLength = sizeof( szBuffer ) ;

cout << "\n \n Actual Buffer Length : " << nLength << "\n \n " ;


int nCntSend = 0 ;

// char *pBuffer = szBuffer ;

Sleep(5000);

while ( ( nCntSend = send( hClientSocket, szBuffer, nLength, 0 ) != nLength ) )
{

if ( nCntSend == -1 )
{
cout << "\n Error sending the data to server" << endl ;
break ;
}
if ( nCntSend == nLength )
break ;

//pBuffer += nCntSend ;
cout << "\n Command Sent to Mitsubishi FX2N is :: " << szBuffer << " " << endl ;
nLength -= nCntSend ;

}

Sleep(1000); // Request Time Out Settings for FX2N

nLength = recv( hClientSocket, qzBuffer, sizeof( qzBuffer ), 0 ) ;

if ( nLength > 0 )
{
qzBuffer[ nLength ] = '\0' ;

cout << " Bytes Received from PLC :: " << nLength << endl ;

cout << "\n \n Response From Mitsubishi FX2N is :: "<< endl ;

q =0;

while (q<nLength)
{
char b_char = qzBuffer[q];
tst[q] = b_char;
q = q++;
}

q=0;
printf("\n \n Actual Data Bytes Received \n \n ");


while(q<nLength)
{
unsigned char c_char = tst[q];
short int p = c_char;
printf(" %d \n", p);
q = q++;
}
Sleep(5000);

}

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

bool InitWinSock2_0( )
{
WSADATA wsaData ;
WORD wVersion = MAKEWORD( 2, 0 ) ;
if ( ! WSAStartup( wVersion, &wsaData ) )
return true ;
return false ;
}



// And this is my C program which will use the reply received form the server which is stored in qzBuffer in above clinet.cpp

// This is C program.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <MyHeader.h>

#ifdef VMS
#pragma nostandard
noshare 
#pragma standard
unsigned short int D[300 + 1];



int user_server_update(unsigned char * tzBuffer) 

{
int i, n,m;

m = n;
while(m == n)
{

for (i=0;i< 12; i++)
{
D[i]++;
}
n=1;
m=2;
}

D[20]++;

return (0);
}



int main()

{
unsigned char T[30] = {0};
FILE *fp;
int m;


m = user_server_update(T);



if((fp = fopen("c:\\server_values.txt", "wb"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}

// write the entire array in one step 
if(fwrite(T, sizeof T, 1, fp) != 1) {
printf("Write error.\n");
exit(1);
}

fclose(fp);
return;
}

// Also i wud be happy to understand any option of DLL usage for sharing the data between C & C++ Function also i want both function capable of editing the same variable / array (variable )

Is there a question hidden in your posting?

// Also i wud be happy to ...

And we would be happy if you'd use CODE tags and format your code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.