#include /*for struct sockaddr_in and INADDR_ANY*/ #include /*for gethostbyname*/ #include #include /*for sockets*/ #include /*for timeval struct*/ #include /*for sockets and time*/ #include /*read and time*/ #define MAX_BUFFER 16384 int main(int argc, char *argv[]) { char chrClientReadBuffer[MAX_BUFFER]; char *chrRemoteHostIP; char chrRemoteHostReadBuffer[MAX_BUFFER]; fd_set FileDescriptorSet; int intBlock; int intClientReadSize; int intClientSocket; int intHighestFD; int intRemoteHostReadSize; int intRemoteHostSocket; int intReuseOptval; int intServerSocket; struct hostent *RemoteHostNameAddress; struct sockaddr_in ClientSockAddress; struct sockaddr_in RemoteHostSockAddress; struct sockaddr_in ServerSockAddress; struct timeval timeSelectTimeout; unsigned int intClientAddressLength; unsigned short shrRemoteHostPort; unsigned short shrServerPort; if(argc < 3) { printf("Usage: %s [Local Port]\n",argv[0]); exit(0); } if (argc == 4) shrServerPort=atoi(argv[3]); else shrServerPort=5555; while(1) { if ((intServerSocket = socket(PF_INET, SOCK_STREAM, 0)) < 0) { printf("Creating server socket failed\n"); exit(0); } memset(&ServerSockAddress,'\0',sizeof(ServerSockAddress)); ServerSockAddress.sin_family=AF_INET; ServerSockAddress.sin_addr.s_addr=htonl(INADDR_ANY); ServerSockAddress.sin_port=htons(shrServerPort); intReuseOptval=1; if ((setsockopt(intServerSocket, SOL_SOCKET, SO_REUSEADDR, &intReuseOptval, sizeof(intServerSocket))) < 0) { printf("setsockopt failed\n"); exit(0); } if (bind(intServerSocket, (struct sockaddr *) &ServerSockAddress, sizeof(ServerSockAddress)) < 0) { printf("bind failed\n"); exit(0); } if (listen(intServerSocket, 5) < 0) { printf("listen failed\n"); exit(0); } intClientAddressLength=sizeof(ClientSockAddress); if ((intClientSocket=accept(intServerSocket, (struct sockaddr *) &ClientSockAddress, &intClientAddressLength)) < 0) { printf("accept failed\n"); exit(0); } printf("Client %s connected\n",inet_ntoa(ClientSockAddress.sin_addr)); if ((intRemoteHostSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { printf("Creating remote host socket failed\n"); exit(0); } shrRemoteHostPort=atoi(argv[2]); chrRemoteHostIP=argv[1]; if ((inet_aton(chrRemoteHostIP, &RemoteHostSockAddress.sin_addr)) == 0) { if ((RemoteHostNameAddress=gethostbyname(chrRemoteHostIP)) == NULL) { printf("gethostbyname failed\n"); exit(0); } chrRemoteHostIP=inet_ntoa(*((struct in_addr *)RemoteHostNameAddress->h_addr)); } memset(&RemoteHostSockAddress,'\0',sizeof(RemoteHostSockAddress)); RemoteHostSockAddress.sin_family=AF_INET; RemoteHostSockAddress.sin_addr.s_addr=inet_addr(chrRemoteHostIP); RemoteHostSockAddress.sin_port=htons(shrRemoteHostPort); if (connect(intRemoteHostSocket, (struct sockaddr *) &RemoteHostSockAddress, sizeof(RemoteHostSockAddress)) < 0) { printf("connect failed\n"); exit(0); } printf("Connected to %s on port %i\n",inet_ntoa(RemoteHostSockAddress.sin_addr),shrRemoteHostPort); memset(chrClientReadBuffer,'\0',sizeof(chrClientReadBuffer)); memset(chrRemoteHostReadBuffer,'\0',sizeof(chrRemoteHostReadBuffer)); intBlock=1; while(intBlock){ timeSelectTimeout.tv_sec=0; timeSelectTimeout.tv_usec=1000; FD_ZERO(&FileDescriptorSet); FD_SET(intClientSocket, &FileDescriptorSet); FD_SET(intRemoteHostSocket, &FileDescriptorSet); intHighestFD = intClientSocket > intRemoteHostSocket ? intClientSocket + 1 : intRemoteHostSocket + 1; select(intHighestFD+1, &FileDescriptorSet, NULL, NULL, &timeSelectTimeout); if FD_ISSET(intClientSocket, &FileDescriptorSet) { if ((intClientReadSize=read(intClientSocket, chrClientReadBuffer, MAX_BUFFER)) > 0) { if ((write(intRemoteHostSocket, chrClientReadBuffer, intClientReadSize)) < 0) { printf("write to Remote Host Socket failed.\n"); exit(0); } printf("Client Read: %s", chrClientReadBuffer); intClientReadSize = 0; memset(chrClientReadBuffer,'\0',sizeof(chrClientReadBuffer)); } } if FD_ISSET(intRemoteHostSocket, &FileDescriptorSet) { if ((intRemoteHostReadSize=read(intRemoteHostSocket, chrRemoteHostReadBuffer, MAX_BUFFER)) >= 0) { if ((write(intClientSocket, chrRemoteHostReadBuffer, intRemoteHostReadSize)) < 0) { printf("write to Client Socket failed.\n"); exit(0); } if (intRemoteHostReadSize == 0) { intBlock = 0; close(intClientSocket); close(intRemoteHostSocket); close(intServerSocket); } printf("Remote Host Read: %s", chrRemoteHostReadBuffer); //intRemoteHostReadSize = 0; memset(chrRemoteHostReadBuffer,'\0',sizeof(chrRemoteHostReadBuffer)); } } } } }