Witam, próbuję napisać prostą parę klient - serwer i natrafiłem na problem. Robię wszystko analogicznie jak w przykładach, ale niestety serwer nie dostaje żadnych danych (read() zwraca -1). Oto kody:
Serwer:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#define SOCKET_PATH "/tmp/gniazdko"
int main(){
int shandle;
int len;
struct sockaddr_un addr;
shandle=socket(AF_UNIX, SOCK_STREAM, 0);
addr.sun_family=AF_UNIX;
strcpy(addr.sun_path, SOCKET_PATH);
unlink(addr.sun_path);
if(bind(shandle, &addr, sizeof(addr))<0) {
close(shandle);
printf("Serwer: problem z funkcją bind()\n");
exit(0);
}
if(listen(shandle, 2)<0){
close(shandle);
printf("Serwer: problem z funkcją listen()\n");
exit(0);
}
int i;
for(i=0; i<2; i++){
printf("Oczekiwanie na klienta\n");
int client=accept(shandle, NULL, NULL);
if(client<0){
close(shandle);
printf("Serwer: problem z funkcją accept()\n");
exit(0);
}
double buff=10.0;
printf("Klient się połączył\n");
int rc=read(shandle, &buff, 8);
printf("recv >> %d\n", rc);
printf("Odebrano: %f", buff);
printf("\nKlient się rozłączył\n");
}
return 0;
}
Klient:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdio.h>
int main(){
int shandle;
struct sockaddr_un addr;
shandle=socket(AF_UNIX, SOCK_STREAM, 0);
addr.sun_family=AF_UNIX;
strcpy(addr.sun_path, "/tmp/gniazdko");
if(connect(shandle, &addr, sizeof(addr))==-1){
close(shandle);
printf("Klient: problem z połączeniem");
exit(0);
}
double buff=20.0;
write(shandle, &buff, 8);
close(shandle);
return 0;
}
Czy ktoś mógłby mnie uświadomić, jaki błąd robię? :)