/*** * scli1.c * * Repeatly prompts user to enter a string, sends each string to the server, * prints reply from server, until string "quit" is encountered. * * To compile: gcc -lsocket -lnsl scli1.c -o scli1 * **/ #include #include #include #include #include #define CADDRESS "clientsocket" #define SADDRESS "mysocket" #define BUFLEN 40 int main(int argc, char *argv[]) { struct sockaddr_un party_addr, own_addr; int sockfd; char buf[BUFLEN]; int party_len; int quitting; quitting = 1; /* */ memset(&own_addr, 0, sizeof(own_addr)); own_addr.sun_family = AF_UNIX; strcpy(own_addr.sun_path, CADDRESS); /* */ if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { printf("can't create socket\n"); return 0; } /* */ unlink(own_addr.sun_path); if (bind(sockfd, (struct sockaddr *) &own_addr, sizeof(own_addr.sun_family)+ strlen(own_addr.sun_path)) < 0) { printf("can't bind socket!"); return 0; } /* */ memset(&party_addr, 0, sizeof(party_addr)); party_addr.sun_family = AF_UNIX; strcpy(party_addr.sun_path, SADDRESS); printf("type the string: "); while (gets(buf)) { /* ? */ quitting = (!strcmp(buf, "quit")); /* */ if (sendto(sockfd, buf, strlen(buf) + 1, 0, (struct sockaddr *) &party_addr, sizeof(party_addr.sun_family) + strlen(SADDRESS)) != strlen(buf) + 1) { printf("client: error writing socket!\n"); return 0; } /* */ if (recvfrom(sockfd, buf, BUFLEN, 0, NULL, 0) < 0) { printf("client: error reading socket!\n"); return 0; } printf("client: server answered: %s\n", buf); if (quitting) break; printf("type the string: "); } close(sockfd); return 0; }