includes { #include "IPCommon.can" } variables { char Local_IP[15]; // char Server_IP[15]="192.168.178.49"; } void SetupIp() { int adapterIndex = 1; 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(); } 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); strncpy(Local_IP, text,16); sysSetVariableString(sysvar::TCPIP::TcpClientIp, text); writelineex(0, 1, info); // SysSetVariableString(sysvar::TCPIP::TcpClientIp, 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 preStart { setStartdelay(1000); } on start { SetupIp(); ConnectTcp(); } on key 'd' { SendTcpData(); } void ConnectTcp() { char buffer[64]; dword serverIp; long fehler; SysGetVariableString(sysvar::TCPIP::TcpServerIp, buffer, elcount(buffer)); // strncpy(buffer, Server_IP,16); serverIp = IpGetAddressAsNumber(buffer); if (INVALID_IP == serverIp) { writelineex(0, 1, "Error: invalid server Ip address!"); return; } // gTcpPort = @sysvar::TCPIP::TcpClientPort; gTcpPort = 12345; gTcpDataSocket = TcpOpen(gIpAddress, gTcpPort); if ( INVALID_SOCKET == gTcpDataSocket) { writelineex(0, 1, "Error: could not open Tcp socket!"); return; } else { writelineex(0, 1, "Tcp socket opened."); } if (0 == TcpConnect(gTcpDataSocket, serverIp, @sysvar::TCPIP::TcpServerPort)) { writelineex(0, 1, "Successfully connected to server %s:%d", buffer, @sysvar::TCPIP::TcpServerPort); TcpRecv( gTcpDataSocket); } else { fehler=IpGetLastSocketError(gTcpDataSocket); write ("No Port-Connection %d", fehler); } } void SendTcpData() { char buffer[512]; strncpy (buffer, "TestData2", 12); // SysGetVariableString(sysvar::TCPIP::TcpClientData, buffer, elcount(buffer)); if (INVALID_SOCKET == gTcpDataSocket) { writelineex( 0, 2, "Tcp socket is invalid!"); return; } write ("Sende Länge %d", strlen(buffer)); if (0 != TcpSend( gTcpDataSocket, buffer, strlen(buffer))) { gIpLastErr = IpGetLastSocketError( gTcpDataSocket); if ( WSA_IO_PENDING != gIpLastErr) { IpGetLastSocketErrorAsString( gTcpDataSocket, gIpLastErrStr, elcount( gIpLastErrStr)); writelineex( 0, 2, "Tcp send error (%d): %s", gIpLastErr, gIpLastErrStr); } } else { writelineex( 0, 1, "Tcp data sent successfully!"); } } void OnTcpConnect( dword socket, long result) { if ( gTcpDataSocket != socket) { writelineex(0, 2, "OnTcpConnect called for unknown socket 0x%X", socket); return; } if (0 != result) { IpGetLastSocketErrorAsString( socket, gIpLastErrStr, elcount( gIpLastErrStr)); writelineex( 0, 2, "OnTcpConnect error (%d): %s", IpGetLastSocketError( socket), gIpLastErrStr); return; } else { writelineex(0, 1, "Successfully connected to server via Tcp"); TcpRecv( socket); } }