Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File Client1u.C
- * ECHO UDP CLIENT with the following feqatures:
- * - Gets server IP address and port from keyboard
- * - LINE/ORIENTED:
- * > continuously reads lines from keyboard
- * > sends each line to the server
- * > waits for response (at most for a fixed amount of time) and diaplays it
- * - Terminates when the "close" or "stop" line is entered
- */
- #include <stdlib.h>
- #include <string.h>
- #include <inttypes.h>
- #include "errlib.h"
- #include "sockwrap.h"
- #define BUFLEN 128 /* BUFFER LENGTH */
- #define TIMEOUT 15 /* TIMEOUT (seconds) */
- /* FUNCTION PROTOTYPES */
- int mygetline(char *line, size_t maxline, char *prompt);
- void showAddr(char *str, struct sockaddr_in *a);
- int iscloseorstop(char *buf);
- /* GLOBAL VARIABLES */
- char *prog_name;
- int main(int argc, char *argv[])
- {
- char buf[BUFLEN]; /* transmission buffer */
- char rbuf[BUFLEN]; /* reception buffer */
- uint32_t taddr_n; /* server IP addr. (net/host ord) */
- uint16_t tport_n, tport_h; /* server port number */
- int s;
- struct sockaddr_in saddr;
- fd_set cset;
- struct timeval tval;
- prog_name = argv[0];
- /* input IP address and port of server */
- mygetline(buf, BUFLEN, "Enter host IPv4 address (dotted notation):");
- taddr_n = inet_addr(buf);
- if (taddr_n == INADDR_NONE)
- err_sys("Invalid address");
- mygetline(buf, BUFLEN, "Enter port : ");
- if (sscanf(buf, "%" SCNu16, &tport_h)!=1)
- err_sys("Invalid port number");
- tport_n = htons(tport_h);
- /* create the socket */
- printf("Creating socket\n");
- s = Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- printf("done. Socket number: %d\n",s);
- /* prepare server address structure */
- bzero(&saddr, sizeof(saddr));
- saddr.sin_family = AF_INET;
- saddr.sin_port = tport_n;
- saddr.sin_addr.s_addr = taddr_n;
- /* main client loop */
- printf("Use message 'close' or 'stop' to terminate program.\n");
- for (buf[0]='\0' ; !iscloseorstop(buf); ) //main loop where we get a line of text from the keyboard
- {
- size_t len, n;
- struct sockaddr_in from;
- socklen_t fromlen;
- mygetline(buf, BUFLEN, "Enter line (max 127 char): ");
- len = strlen(buf);
- n=sendto(s, buf, len, 0, (struct sockaddr *) &saddr, sizeof(saddr));
- if (n != len)
- {
- printf("Write error\n");
- continue;
- }
- printf("waiting for response...\n");
- FD_ZERO(&cset);
- FD_SET(s, &cset);
- tval.tv_sec = TIMEOUT;
- tval.tv_usec = 0;
- n = Select(FD_SETSIZE, &cset, NULL, NULL, &tval);
- if (n>0)
- {
- /* receive datagram */
- fromlen = sizeof(struct sockaddr_in);
- n=recvfrom(s,rbuf,BUFLEN-1,0,(struct sockaddr *)&from,&fromlen);
- if (n != -1)
- {
- rbuf[n] = '\0';
- showAddr("Received response from", &from);
- printf(": [%s]\n", rbuf);
- }
- else printf("Error in receiving response\n");
- }
- else printf("No response received after %d seconds\n",TIMEOUT);
- printf("=======================================================\n");
- }
- close(s);
- exit(0);
- }
- /* Gets a line of text from standard input after having printed a prompt string
- Substitutes end of line with '\0'
- Empties standard input buffer but stores at most maxline-1 characters in the
- passed buffer
- */
- int mygetline(char *line, size_t maxline, char *prompt)
- {
- char ch;
- size_t i;
- printf("%s", prompt);
- for (i=0; i< maxline-1 && (ch = getchar()) != '\n' && ch != EOF; i++)
- *line++ = ch;
- *line = '\0';
- while (ch != '\n' && ch != EOF)
- ch = getchar();
- if (ch == EOF)
- return(EOF);
- else return(1);
- }
- /* Utility function to display a string str
- followed by an internet address a, written
- in decimal notation
- */
- void showAddr(char *str, struct sockaddr_in *a)
- {
- char *p;
- p = inet_ntoa(a->sin_addr);
- printf("%s %s",str,p);
- printf("!%u\n", ntohs(a->sin_port));
- }
- /* Funzione per controllare se un buffer contiene la stringa "close" o "stop" */
- int iscloseorstop(char *buf)
- {
- return (!strcmp(buf, "close") || !strcmp(buf, "stop"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement