#define WIN32_LEAN_AND_MEAN #include #include #include //#define CONNECTED_UDP //if commented out, use unconnected semantic; if uncommented, use connected semantics #define DEFAULT_PORT 5432 #define DEFAULT_PROTO SOCK_STREAM // TCP void Usage(char *progname) { fprintf(stderr,"Usage\n%s -p [protocol] -n [server] -e [endpoint] -l [iterations]\n", progname); fprintf(stderr,"Where:\n\tprotocol is one of TCP or UDP\n"); fprintf(stderr,"\tserver is the IP address or name of server\n"); fprintf(stderr,"\tendpoint is the port to listen on\n"); fprintf(stderr,"\titerations is the number of loops to execute\n"); fprintf(stderr,"\t(-l by itself makes client run in an infinite loop,"); fprintf(stderr," Hit Ctrl-C to terminate it)\n"); fprintf(stderr,"Defaults are TCP , localhost and 5001\n"); WSACleanup(); exit(1); } void DisplayError (LPTSTR title, DWORD error) { printf ("%s, Error# %d\n\n", title, error); } int main(int argc, char **argv) { unsigned char Buffer[128]; char *server_name= "localhost"; unsigned short port = DEFAULT_PORT; int retval, loopflag=0; int i, loopcount,maxloop=-1; unsigned int addr; int socket_type = DEFAULT_PROTO; struct sockaddr_in server; struct hostent *hp; WSADATA wsaData; SOCKET conn_socket; int j; if (argc >1) { for(i=1;i h_addr,hp->h_length); server.sin_family = hp->h_addrtype; server.sin_port = htons(port); conn_socket = socket(AF_INET,socket_type,0); /* Open a socket */ if (conn_socket <0 ) { fprintf(stderr,"Client: Error Opening socket: Error %d\n", WSAGetLastError()); WSACleanup(); return -1; } if (socket_type == SOCK_STREAM) { printf("Client connecting to: %s\n",hp->h_name); if (connect(conn_socket,(struct sockaddr*)&server,sizeof(server)) == SOCKET_ERROR) { fprintf(stderr,"connect() failed: %d\n",WSAGetLastError()); WSACleanup(); return -1; } } Sleep (200); //Wait a little while to allow PATH, RESV exchange // cook up a string to send // loopcount =0; while(1) { loopcount++; Buffer[0] = 102; Buffer[1] = 0; retval = send(conn_socket,(char*)Buffer,1,0); if (retval == SOCKET_ERROR) { fprintf(stderr,"send() failed: error %d\n",WSAGetLastError()); WSACleanup(); return -1; } Sleep (20); retval = recv(conn_socket,(char*)Buffer,sizeof (Buffer),0 ); if (retval == SOCKET_ERROR) { fprintf(stderr,"recv() failed: error %d\n",WSAGetLastError()); closesocket(conn_socket); continue; } printf("Received %4d bytes, from server. Data:",retval); for(j=0;j= maxloop) && (maxloop >0) ) { break; } } } closesocket(conn_socket); WSACleanup(); return(0); }