/* Begin server3.c */ /* By William D. Larry [Ellectr0n] - Saturday, August 21, 2001 */ /* Copyright 2001(c) William D. Larry */ /* This program code is licensed under the GLP. Please read GPL for more information */ /* here we go... */ /* * A simple Server-Applikation, that receives a String and answers to the client. */ /* Include neccessery header files */ #include #include #include #include #include #include #include #define SOCKET_NAME "sockli" int main(void) { int sock, len; int result; int n; char buf[256]; //struct sockaddr_in addr; struct sockaddr_un addr = {0}; sock = socket(AF_UNIX, SOCK_DGRAM, 0); if(!sock) { perror("creating dgram socket"); exit(1); } memset(&addr, 0, sizeof(addr)); /* set up the address */ /*addr.sin_family = AF_INET; addr.sin_port = 9836; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); len = sizeof(addr); */ unlink(SOCKET_NAME); addr.sun_family = AF_UNIX; strcpy(addr.sun_path, SOCKET_NAME); len = sizeof(addr); result = bind(sock, (struct sockaddr *)&addr, len); if(result < 0) { perror("binding to dgram socket"); exit(1); } n = recvfrom(sock, buf, 256, 0, (struct sockaddr *)&addr, &len); buf[n] = '\0'; if(n < 1) { /* received less than 1 byte... that's illigal! */ fprintf(stderr, "server3: received data of illigal size\n"); exit(1); } printf("RECEIVED FROM CLIENT: %s\n", buf); memset(buf, '\0', 256); strcpy(buf, "\n\nHallo Client\n\n"); addr.sun_family = AF_UNIX; strcpy(addr.sun_path, SOCKET_NAME); len = sizeof(addr); n = sendto(sock, buf, strlen(buf), 0, (struct sockaddr *)&addr, len); if(n != strlen(buf)) { perror("sendto failed"); exit(1); } close(sock); //printf("\n\nSENT: %d, LEN: %d\n\n", n, strlen(buf)); }