/* Compiler: BC++ 3.1, Turbo C++ 1.01 (3.01) Compile mode: large Project: main.c tcpConn.C ..\..\..\lib\upac5000.lib ..\lib\tcp_dm32.lib For this client to work you need to have a TCP Server connected to the same address as specified by the server, port combination. [Jan 28, 2015] */ #include #include "..\..\..\lib\upac5000.h" #include "..\..\lib\tcpip32.h" #include "tcpConn.h" extern tcpclient TCPClient; void Ethernet_Receive(int socket, int mode) { static char recDataBuf[128]; int iRet, iLength; if(!mode) { Print("A connection is established with the TCP server using socket #%d\n", socket); } else { iLength=recv(socket, recDataBuf, sizeof(recDataBuf)-1, 0); if(iLength>0) { recDataBuf[iLength]=0x0; Print("%s\n", recDataBuf); iRet=send(socket, recDataBuf, iLength, 0); if(iRet<0) CloseSocket(socket); } else { // an error occurred or connection was terminated by TCP server CloseSocket(socket); Print("The connection was terminated\n"); } } } void main(void) { char sendMessage[]="hello world"; int iRet, Quit=0; InitLib(); if((iRet=NetStart())!=NoError) { Print("Failed to initialize the network settings [%d].\n", iRet); return; } TcpClient_Connect("192.168.255.100", 10000, 3000, 5000); /* IP address of TCP server: "192.168.255.100" Connection port of TCP server: 10000 Connection timeout: 3000 ms Reconnect interval: 5000 ms Note: Reconnect interval is the amount of time between attempts by the module to reconnect with the TCP server. If reconnectInterval is greater than 2000 ms, TCPClient always attempting to reconnect to the TCP server after a loss of connection. */ while(!Quit) { EthernetLoop(); if(Kbhit()) { switch(Getch()) { case 27: // press ESC key to exit Quit=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=send(TCPClient.socket, sendMessage, strlen(sendMessage), 0); if(iRet<0) 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; } } } CloseSocket(TCPClient.socket); Nterm(); }