can any one help tell me if this code will work or if im barking up the wrong tree
the client is like thisCode:#include <stdio.h> /* for printf() and fprintf() */ #include <sys/socket.h> /* for socket(), bind(), and connect() */ #include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ #include <stdlib.h> /* for atoi() and exit() */ #include <string.h> /* for memset() */ #include <unistd.h> /* for close() */ #include <sys/wait.h> /* for waitpid() */ #include <sys/time.h> /* for struct timeval {} */ #include <fcntl.h> /* for fcntl() */ int main(int argc, char *argv[]) { int *servSock; /* Socket descriptors for server */ int maxDescriptor; /* Maximum socket descriptor value */ fd_set sockSet; /* Set of socket descriptors for select() */ long timeout; /* Timeout value given on command-line */ struct timeval selTimeout; /* Timeout for select() */ int running = 1; /* 1 if server should be running; 0 otherwise */ int noPorts = 11; /* Number of port specified on command-line */ int port; /* Looping variable for ports */ unsigned short portNo; /* Actual port number */ char clientname[i][3] /* to give names to clients*/ char command[6] /* for the users command*/ char client_choice[6] /* for the users choice of finger*/ if (argc < 3) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s <Timeout (secs.)> <Port 1> ...\n", argv[0]); exit(1); } timeout = atol(argv[1]); /* First arg: Timeout */ noPorts = argc - 2; /* Number of ports is argument count minus 2 */ /* Allocate list of sockets for incoming connections */ servSock = (int *) malloc(noPorts * sizeof(int)); /* Initialize maxDescriptor for use by select() */ maxDescriptor = -1; /* Create list of ports and sockets to handle ports */ for (port = 0; port < noPorts; port++) { /* Add port to port list */ portNo = atoi(argv[port + 2]); /* Skip first two arguments */ /* Create port socket */ servSock[port] = CreateTCPServerSocket (portNo); // // if ((servSock = socket(PF_INET,/*use ip protocol*/ SOCK_STREAM, IPPROTO_TCP/* using tcp*/)) < 0) // DieWithError("socket() failed");*/ // // /* Construct local address structure */ // memset(&echoServAddr, 0, sizeof(serveraddress)); /* Zero out structure */ // echoServAddr.sin_family = AF_INET; /* Internet address family */ // echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ // echoServAddr.sin_port = htons(serverport); /* Local port */ // // /* Bind to the local address */ // if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) // DieWithError("bind() failed"); // // /* Mark the socket so it will listen for incoming connections */ // if (listen(servSock, MAXPENDING) < 0) // DieWithError("listen() failed"); // // for (;;) /* Run forever */ // { // /* Set the size of the in-out parameter */ // // //clntLen = sizeof(echoClntAddr); // // /* Wait for a client to connect */ // if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, // &clntLen)) < 0) // DieWithError("accept() failed"); /* clntSock is connected to a client! */ /* Determine if new descriptor is the largest */ if (servSock[port] > maxDescriptor) maxDescriptor = servSock[port]; } printf("Starting server: Hit return to shutdown\n"); while (running) { /* Zero socket descriptor vector and set for server sockets */ /* This must be reset every time select() is called */ FD_ZERO(&sockSet); /* Add keyboard to descriptor vector */ FD_SET(STDIN_FILENO, &sockSet); for (port = 0; port < noPorts; port++) FD_SET(servSock[port], &sockSet); /* Timeout specification */ /* This must be reset every time select() is called */ selTimeout.tv_sec = timeout; /* timeout (secs.) */ selTimeout.tv_usec = 0; /* 0 microseconds */ /* Suspend program until descriptor is ready or timeout */ if (select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout) == 0) printf("No echo requests for %ld secs...Server still alive\n", timeout); else { if (FD_ISSET(0, &sockSet)) /* Check keyboard */ { printf("Shutting down server\n"); getchar(); running = 0; } for (port = 0; port < noPorts; port++) if (FD_ISSET(servSock[port], &sockSet)) { printf("Request on port %d: ", port); HandleTCPClient(AcceptTCPConnection(servSock[port])); //ip address need to be changed if (echoClntAddr == 192.168.10.1) {client name[0] == r1;} if echoClntAddr == 192.168.10.2) {clientname[1] == r2;} if (echoClntAddr == 192.168.10.3) { client name[2] == r3;} if (echoClntAddr == 192.168.10.4) { client name[3] == r4;} if (echoClntAddr == 192.168.10.5) { client name[4] == r5;} if (echoClntAddr == 192.168.11.1) { client name[5] == l1;} if (echoClntAddr == 192.168.11.2) { client name[6] == l2;} if (echoClntAddr == 192.168.11.3) { client name[7] == l3;} if (echoClntAddr == 192.168.11.4) { client name[8] == l4;} if (echoClntAddr == 192.168.11.5) { client name[9] == l5;} // printf ("please enter the finger you wish to move(r1-5 or l1-5 )"); // scanf(client_choice); //printf ("do you wish to open or close"); //scanf (command); // need to add the pain comand (read from client then send to all clients on subnet /*if (client_choice == r1) { select(client_name1) if (send(command, echoBuffer, recvMsgSize, 0) != recvMsgSize) DieWithError("send() failed"); close socket } repeat for all 10 clients } } } /* Close sockets */ for (port = 0; port < noPorts; port++) close(servSock[port]); /* Free list of sockets */ free(servSock); exit(0); }
sorry about the long postCode:#include <stdio.h> /* for printf() and fprintf() */ #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */ #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */ #include <stdlib.h> /* for atoi() and exit() */ #include <string.h> /* for memset() */ #include <unistd.h> /* for close() */ #include <fcnt1.h> /*for read only */ #define RCVBUFSIZE 32 /* Size of receive buffer */ void DieWithError(char *errorMessage); /* Error handling function */ int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in echoServAddr; /* Echo server address */ unsigned short echoServPort; /* Echo server port */ char *servIP; /* Server IP address (dotted quad) */ char *command; /* String to send to echo server */ char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */ unsigned int commandLen; /* Length of string to echo */ int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv() and total bytes read */ if ((argc < 3) || (argc > 4)) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s <Server IP> <command> [<Echo Port>]\n", argv[0]); exit(1); } servIP = argv[1]; /* First arg: server IP address (dotted quad) */ command = argv[2]; /* Second arg: string to echo */ if (argc == 4) echoServPort = atoi(argv[3]); /* Use given port, if any */ else echoServPort = 7; /* 7 is the well-known port for the echo service */ /* Create a reliable, stream socket using TCP */ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); /* Construct the server address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */ echoServAddr.sin_port = htons(echoServPort); /* Server port */ /* Establish the connection to the echo server */ if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("connect() failed"); commandLen = strlen(command); /* Determine input length */ while command != "end" { /* Receive a string back from the server */ totalBytesRcvd = 0; printf("Received: "); /* Setup to print the echoed string */ while (totalBytesRcvd < commandLen) { /* Receive up to the buffer size (minus 1 to leave space for a null terminator) bytes from the sender */ if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0) DieWithError("recv() failed or connection closed prematurely"); totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */ echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */ printf("%s", echoBuffer); /* Print the echo buffer */ //+++++++++++++++++++++++++++++++++++++++++++++++++ if command == "pain"; { /* Send the string to the server */ if (send(sock, command, commandLen, 0) != commandLen) DieWithError("send() sent a different number of bytes than expected"); } if command == "open"; { FILE fopen(pic1.jpg, O_RDONLY);/*print pic1(hand open)*/ } if command == "close"; { FILE fopen(pic2.jpg, O_RDONLY);/*print pic2 (hand closed)*/ } FILE fclose(); } printf("\n"); /* Print a final linefeed */ } close(sock); exit(0); }
the server should be able to connect to all clients and then ask for a client to connect to and an command to send to it (think of it like a set of fingers as clients and the server is the brain
the client shold recive a message from the server and use that message to print a picture file to the screen
can any one tell me if this is right or what i need to do to fix it all my lecturers have vanished over the "easter" break and im at my wits end
thanks



LinkBack URL
About LinkBacks


