#include #include #include #include #include #include #include #include #define SOCK_PATH "/tmp/echo_socket" #define BUF_SIZE 1024 int main(void) { int i = 0; int s, t, len; struct sockaddr_un remote; char str[BUF_SIZE]; if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } printf("Trying to connect...\n"); remote.sun_family = AF_UNIX; strcpy(remote.sun_path, SOCK_PATH); len = strlen(remote.sun_path) + sizeof(remote.sun_family); if (connect(s, (struct sockaddr *)&remote, len) == -1) { perror("connect"); exit(1); } printf("Connected.\n"); #if 1 while(1) { sprintf(str, "%d\n", i); if (send(s, str, strlen(str), 0) == -1) { perror("send"); exit(1); } if ((t=recv(s, str, BUF_SIZE, 0)) > 0) { str[t] = '\0'; printf("echo> %s", str); } else { if (t < 0) perror("recv"); else printf("Server closed connection\n"); //exit(1); } sleep(10); i++; } #endif close(s); return 0; }