/* Compiler: BC++ 3.1, Turbo C++ 1.01 (3.01) Compile mode: Large Project: Client.c ..\lib\upac5000.Lib ..\lib\tcp_dm32.lib ..\lib\FW_nnnnn.LIB Hordware: uPAC-5000 Series [May 20, 2015] */ #include #include "..\lib\upac5000.h" #include "..\lib\Tcpip32.h" #include "..\lib\MFW.h" void getResponse(int skt, int mode); TCP_CLIENT TcpClient={ "192.168.255.100", // char *ip 10000, // unsigned port -1, // int socket 0, // int tmpState -1, // 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); // specify the IP address of DNS server to default AddDomainNameServer("10.0.0.3"); EnableWDT(); } void XS_UserEnd(void) { XS_StopSocket(); XS_RemoveSystemLoopFun(XS_SocketLoopFun); XS_RemoveSystemLoopFun(UserLoop); DisableWDT(); } char ipaddress[16]; int DN_to_IP(char *domainname, char *ip) { unsigned char *tmp; struct hostent *hptr; if((hptr=gethostbyname(domainname))!=NULL) { tmp=*(hptr->h_addr_list); sprintf(ip, "%d.%d.%d.%d", tmp[0], tmp[1], tmp[2], tmp[3]); return 0; } return -1; } 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 connect to the TCP server if(TcpClient.bConnected==-1) { if(!stricmp(TcpClient.ip, "192.168.255.100")) { if(!DN_to_IP("www.wydomain.com", ipaddress)) { XS_RemoveClient(&TcpClient); // enable the TCP client connection and connect to remote server TcpClient.ip=ipaddress; TcpClient.port=10000; TcpClient.bConnected=-2; XS_AddClient(&TcpClient); } else Print("cannot receive a response from the DNS server\n"); } else 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 }