277 lines
5.7 KiB
Text
277 lines
5.7 KiB
Text
|
includes
|
||
|
{
|
||
|
#include "IPCommon.can"
|
||
|
}
|
||
|
|
||
|
variables
|
||
|
{
|
||
|
int adapterIndex = 1;
|
||
|
char Server_IP[16]="";
|
||
|
}
|
||
|
|
||
|
void SetupIp()
|
||
|
{
|
||
|
|
||
|
char text[512] = "";
|
||
|
char info[512] = "";
|
||
|
int size = 512;
|
||
|
long result = 0;
|
||
|
dword addresses[1];
|
||
|
|
||
|
writeClear(0);
|
||
|
|
||
|
if (1 > IpGetAdapterCount())
|
||
|
{
|
||
|
writelineex(0, 3, "Error: There is no network interface available!");
|
||
|
|
||
|
stop();
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// while (adapterIndex<=IpGetAdapterCount())
|
||
|
// {
|
||
|
// IpGetAdapterDescription(adapterIndex, text, size);
|
||
|
// write("Info: Interface %d von %d: %s", adapterIndex, IpGetAdapterCount(), text);
|
||
|
// adapterIndex++;
|
||
|
// };
|
||
|
//
|
||
|
|
||
|
if (0 != IpGetAdapterAddress(adapterIndex, addresses, 1))
|
||
|
{
|
||
|
writelineex(0, 3, "Error: Could not retrieve Ip address!");
|
||
|
|
||
|
stop();
|
||
|
}
|
||
|
|
||
|
gIpAddress = addresses[0]; // the interface used
|
||
|
|
||
|
if (INVALID_IP == gIpAddress)
|
||
|
{
|
||
|
writelineex(0, 3, "Error: Ip address to be used is invalid!");
|
||
|
|
||
|
stop();
|
||
|
}
|
||
|
|
||
|
IpGetAdapterDescription(adapterIndex, text, size);
|
||
|
snprintf(info, size, "Interface: %s", text);
|
||
|
writelineex(0, 1, info);
|
||
|
|
||
|
IpGetAdapterAddressAsString(adapterIndex, text, size);
|
||
|
snprintf(info, size, "Ip address: %s", text);
|
||
|
writelineex(0, 1, info);
|
||
|
|
||
|
strncpy(Server_IP, text,16);
|
||
|
sysSetVariableString(sysvar::TCPIP::TcpServerIp, text);
|
||
|
|
||
|
IpGetAdapterMaskAsString(adapterIndex, text, size);
|
||
|
snprintf(info, size, "Subnet mask: %s", text);
|
||
|
writelineex(0, 1, info);
|
||
|
|
||
|
IpGetAdapterGatewayAsString(adapterIndex, text, size);
|
||
|
snprintf(info, size, "Gateway address: %s", text);
|
||
|
writelineex(0, 1, info);
|
||
|
|
||
|
gStatus = gkSTATUS_INITIALISED;
|
||
|
}
|
||
|
|
||
|
on start
|
||
|
{
|
||
|
SetupIp();
|
||
|
StartListenTcp();
|
||
|
}
|
||
|
|
||
|
on stopMeasurement
|
||
|
{
|
||
|
ResetIp();
|
||
|
}
|
||
|
|
||
|
void OnTcpListen( dword socket, long result)
|
||
|
{
|
||
|
if (gTcpSocket != socket)
|
||
|
{
|
||
|
writelineex( 0, 2, "OnTcpListen called for unexpected socket (%d).", socket);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if ( 0 != result)
|
||
|
{
|
||
|
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||
|
|
||
|
writelineex( 0, 2, "OnTcpListen error (%d, %s).",
|
||
|
IpGetLastSocketError( socket), gIpLastErrStr);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (gTcpDataSocket != INVALID_SOCKET)
|
||
|
{
|
||
|
TcpClose(gTcpDataSocket);
|
||
|
}
|
||
|
|
||
|
gTcpDataSocket = TcpAccept( socket);
|
||
|
|
||
|
if ( INVALID_SOCKET == gTcpDataSocket)
|
||
|
{
|
||
|
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||
|
|
||
|
writelineex( 0, 2, "Error: TcpAccept (%d): %s",
|
||
|
IpGetLastSocketError( socket), gIpLastErrStr);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
TcpRecv( gTcpDataSocket);
|
||
|
|
||
|
writelineex( 0, 1, "Status: Tcp connection established.");
|
||
|
}
|
||
|
|
||
|
long OnTcpReceive( dword socket, long result, dword address, dword port, char buffer[], dword size)
|
||
|
{
|
||
|
char addressString[64] = "";
|
||
|
|
||
|
if ( gTcpDataSocket != socket)
|
||
|
{
|
||
|
writelineex(0, 2, "OnTcpReceive called for unknown socket 0x%X", socket);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
if (0 != result)
|
||
|
{
|
||
|
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||
|
|
||
|
writelineex( 0, 2, "OnTcpReceive error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
IpGetAddressAsString(address, addressString, elcount(addressString));
|
||
|
|
||
|
strncat (buffer, " Received", elcount(buffer)+10);
|
||
|
SysSetVariableString(sysvar::TCPIP::TcpData, buffer);
|
||
|
SysSetVariableString(sysvar::TCPIP::Connect_IP, addressString);
|
||
|
|
||
|
@sysvar::TCPIP::Connect_Port = port;
|
||
|
SendTcpData();
|
||
|
TcpRecv( socket);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void OnTcpSend( dword socket, long result, char buffer[], dword size)
|
||
|
{
|
||
|
if ( gTcpDataSocket != socket)
|
||
|
{
|
||
|
writelineex(0, 2, "OnTcpSend called for unknown socket 0x%X", socket);
|
||
|
}
|
||
|
|
||
|
if (0 != result)
|
||
|
{
|
||
|
IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||
|
|
||
|
writelineex( 0, 2, "OnTcpSend error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
writelineex( 0, 1, "Tcp data sent successfully!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void StartListenTcp()
|
||
|
{
|
||
|
gTcpPort = @sysvar::TCPIP::TcpServerPort;
|
||
|
|
||
|
gTcpSocket = TcpOpen(gIpAddress, gTcpPort);
|
||
|
|
||
|
if ( INVALID_SOCKET == gTcpSocket)
|
||
|
{
|
||
|
writelineex(0, 1, "Error: could not create Tcp socket!");
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
writelineex(0, 1, "Successfully created Tcp socket.");
|
||
|
|
||
|
TcpListen(gTcpSocket);
|
||
|
|
||
|
writelineex( 0, 1, "Listening for incoming Tcp connections on port %d", gTcpPort);
|
||
|
}
|
||
|
|
||
|
void StopListenTcp()
|
||
|
{
|
||
|
if (INVALID_SOCKET != gTcpDataSocket)
|
||
|
{
|
||
|
TcpClose(gTcpDataSocket);
|
||
|
|
||
|
gTcpDataSocket = INVALID_SOCKET;
|
||
|
}
|
||
|
|
||
|
if (INVALID_SOCKET != gTcpSocket)
|
||
|
{
|
||
|
TcpClose(gTcpSocket);
|
||
|
|
||
|
gTcpSocket = INVALID_SOCKET;
|
||
|
}
|
||
|
|
||
|
writelineex(0, 1, "Tcp socket is closed.");
|
||
|
}
|
||
|
|
||
|
void SendTcpData()
|
||
|
{
|
||
|
char buffer[560];
|
||
|
|
||
|
SysGetVariableString(sysvar::TCPIP::TcpData, buffer, elcount(buffer));
|
||
|
|
||
|
if (INVALID_SOCKET != gTcpDataSocket)
|
||
|
|
||
|
if (0 != TcpSend( gTcpDataSocket, buffer, strlen(buffer)))
|
||
|
{
|
||
|
gIpLastErr = IpGetLastSocketError( gTcpDataSocket);
|
||
|
|
||
|
if ( WSA_IO_PENDING != gIpLastErr)
|
||
|
{
|
||
|
IpGetLastSocketErrorAsString( gTcpDataSocket, gIpLastErrStr, elcount( gIpLastErrStr));
|
||
|
|
||
|
writelineex( 0, 2, "TcpSend error (%d): %s", gIpLastErr, gIpLastErrStr);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
writelineex( 0, 1, "Tcp data sent successfully!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ResetIp()
|
||
|
{
|
||
|
if (INVALID_SOCKET != gTcpDataSocket)
|
||
|
{
|
||
|
TcpClose(gTcpDataSocket);
|
||
|
|
||
|
gTcpDataSocket = INVALID_SOCKET;
|
||
|
}
|
||
|
|
||
|
if (INVALID_SOCKET != gTcpSocket)
|
||
|
{
|
||
|
TcpClose(gTcpSocket);
|
||
|
|
||
|
gTcpSocket = INVALID_SOCKET;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
on key 'a'
|
||
|
{
|
||
|
SendTcpData();
|
||
|
}
|
||
|
|
||
|
on key 's'
|
||
|
{
|
||
|
StartListenTcp();
|
||
|
}
|
||
|
|
||
|
|
||
|
on key 'S'
|
||
|
{
|
||
|
StopListenTcp();
|
||
|
}
|