Ok i will try to explain it better:
I wrote some function in a file.c where a variable of type struct sockaddr is declared as well.
Code:
serverChannel_t createServerChannel(const char* path) {
serverChannel_t sd; /* socket descriptor */
ec_null1( path, EINVAL )
if (strlen(path) > UNIX_PATH_MAX) {
errno=EINVAL;
return SOCKNAMETOOLONG;
}
strncpy(sa.sun_path, path, UNIX_PATH_MAX);
sa.sun_family=AF_UNIX;
ec_neg1( (sd=socket(AF_UNIX, SOCK_STREAM, 0)), 0 )
return sd;
}
channel_t acceptConnection(serverChannel_t s) {
channel_t c_sd; /* client socket descriptor */
ec_neg1( s-1, EINVAL )
ec_neg1( bind(s, (struct sockaddr*)&sa, sizeof(sa)), 0 )
ec_neg1( listen(s, SOMAXCONN), 0 ) /* marks s to accept at most SOMAXCONN clients */
ec_neg1( (c_sd=accept(s, NULL, 0)), 0 )
return c_sd;
}
These two above are called bye the server in the main, so basically the struct sockaddr_un is not used at all in the main (could this be a problem?).
This is the function the client calls:
Code:
channel_t openConnection(const char* path) {
channel_t ch;
ec_neg1( (ch=(channel_t)createServerChannel(path)), 0)
while (connect((int)ch, (struct sockaddr*)&sa, sizeof(sa))==-1)
if (errno==ENOENT) {
sleep(1);
continue;
}
return ch;
}
channel_t and serverChannel_t are integers. So the code become:
Code:
//Server
int main() {
message_t msg_s, msg_r;
int res, i=0;
serverChannel_t sd1;
channel_t sd2;
msg_s.type=MSG_MKAGENDA;
msg_s.length=20;
ec_null1( (msg_r.buffer=(char*)malloc(sizeof(char)*20)), 0 )
ec_null1( (msg_s.buffer=(char*)malloc(sizeof(char)*20)), 0 )
sprintf(msg_s.buffer, "The server says hi!");
ec_neg1( (s1=createServerChannel("tmp/socket")), 0 )
ec_neg1( (s2=acceptConnection(s1), 0 )
while (1) {
if (-1==read(sd2, msg_r.buffer, 20)<0) {
perror("No message received from the client");
pthread_exit(&fail);
}
if (-1==write(sd2, msg_s.buffer, msg_s.length)){
perror("No message written to the client");
pthread_exit(&fail);
}
else printf("Server got: %s\n", msg_r.buffer);
sleep(1);
}
}
//Client
int main(int argc, char *argv[]) {
message_t msg_s, msg_r;
int res, i;
channel_t sd;
msg_s.type=MSG_MKAGENDA;
msg_s.length=20;
ec_null1( (msg_r.buffer=(char*)malloc(sizeof(char)*20)), 0 )
ec_null1( (msg_s.buffer=(char*)malloc(sizeof(char)*20)), 0 )
sprintf(msg_s.buffer, "The client says hi!");
ec_neg1( (sd=openConnection("tmp/sock"), 0 )
do {
if (write (sd, msg_s.buffer, msg_s.length) == -1)
perror("Error in write() client side");
if (-1==read(sd, msg_r.buffer, 20))
perror("Error in read() client side");
sleep(1);
}
while (1);
}
Now i hope it's better to understand . I think i'm doing some stupid mistake witch the socket initialization! The output is this:
client output:
[cls2@it159 DPLAN]$ Error in write() client side : Broken pipe
server output:
[cls2@it159 DPLAN]$ No message received from the client: Transport endpoint is not connected
Please help me! I am stuck and the deathline of this assignment is near! Thanks