/* Compiler: BC++ 3.1, Turbo C++ 1.01 (3.01) Compile mode: Large Project: Client.c ..\lib\7186el.Lib ..\lib\tcp_dm32.lib ..\lib\FW_nnnnn.LIB Hordware: uPAC-7186EX Series [Feb 10, 2015] */ #include #include "..\lib\7186e.h" #include "..\lib\Tcpip32.h" #include "..\lib\MFW.h" void getResponse(int skt, int mode); TCP_CLIENT TcpClient={ "192.168.255.100", // char *ip, specify the IP address of the TCP server 10000, // unsigned port, specify the TCP port to use for connection -1, // int socket 0, // int tmpState -2, // int bConnected 10, // int iConnectTryCount 4000, // long lConnetTimeout, Connection timeout: 3000 ms (0: default value is 400ms) 0, // long ltmpT (void *) getResponse, NULL }; void XS_UserInit(int argc, char *argv[]) { extern int bAcceptBroadcast; extern unsigned long ACKDELAY; extern long MAXTXTOUT; void UserLoop(void); InitLib(); ACKDELAY=200; MAXTXTOUT=6000UL; bAcceptBroadcast=0; XS_AddSystemLoopFun(UserLoop); // enable the Ethernet network connection XS_AddSystemLoopFun(XS_SocketLoopFun); XS_StartSocket(); // enable the TCP client connection and connect to remote server XS_AddClient(&TcpClient); EnableWDT(); } void XS_UserEnd(void) { XS_StopSocket(); XS_RemoveSystemLoopFun(XS_SocketLoopFun); XS_RemoveSystemLoopFun(UserLoop); DisableWDT(); } char sendMessage[]="hello world"; void UserLoop(void) { int iRet; RefreshWDT(); if(Kbhit()) { switch(Getch()) { case 27: // press ESC key to exit QuitMain=1; break; case 'c': // press 'c' key to attempt to reconnect to the TCP server after a loss of connection if(TcpClient.bConnected==-1) { TcpClient.bConnected=-2; } break; default: // press any key to send data over an existing connection if(TcpClient.bConnected==1) { iRet=XS_WriteSocket(TcpClient.socket, sendMessage, strlen(sendMessage)); if(iRet<0) XS_CloseSocket(TcpClient.socket); } else { Print("Connection is not ready for use\n"); switch(TcpClient.bConnected) { case 0: Print("Waiting for the response from the server\n"); break; case -1: Print("Socket is idle and ready for connection\n"); break; case -2: Print("Create a socket and attempt to connect to the TCP server\n"); break; } } break; } } } void getResponse(int skt, int mode) { char recDataBuf[128]; int iRet, iLength; if(!mode) { Print("A connection is established with the TCP server using socket #%d\n", skt); } else { iLength=XS_ReadSocket(skt, recDataBuf, sizeof(recDataBuf)-1); if(iLength>0) { recDataBuf[iLength]=0x0; Print("%s\n", recDataBuf); iRet=XS_WriteSocket(skt, recDataBuf, iLength); if(iRet<0) XS_CloseSocket(skt); } else { // an error occurred or connection was terminated by TCP server XS_CloseSocket(skt); Print("The connection was terminated\n"); } } } void main(int argc, char *argv[]) { XS_main(argc, argv); // call the XS library main function }