/* unixechoclient.c - Communicate with the unixechoserver through a Unix socket associated with a file pathname [passed either as argv[1] or by default /tmp/unixechoserver] In a loop the user is prompted to enter a line of text and this data is sent to the server; the reply (echo) is printed out. The loop terminates when we enter a null line. */ #include #include #include #include #include #include #include #include #include #include #include #include char unixecho_path[256] = "/tmp/unixechoserver"; /* default */ int main(int argc, char **argv) { int sockfd; struct sockaddr_un servaddr; if ( (sockfd = socket(AF_UNIX, SOCK_STREAM, 0) ) < 0 ) { perror("Socket"); exit (1); } bzero((void *)&servaddr, sizeof(servaddr)); servaddr.sun_family = AF_UNIX; strcpy(servaddr.sun_path, unixecho_path); if ( connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) { perror("Connect"); exit (1); } for ( ; ; ) { int n; char c; /* Read a line from user and write it to socket */ n = 0; printf("Enter a line: "); for (;;) { c = getchar(); write(sockfd, &c, 1); if (c == '\n') break; n++; } /* Read a line from the socket */ for (;;) { read(sockfd, &c, 1); putchar(c); if (c == '\n') break; } if (n == 0) break; /* The line entered by user was null */ } exit(0); }