admin管理员组文章数量:1192329
this is all happening in a random directory in my home /home/behe/Documents/development/beh/wallpaper_select
client.c
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
const char* xdg_dir = getenv("XDG_RUNTIME_DIR");
const char* home = getenv("HOME");
const char* hello_c = "/hello_c";
const char* prefix = "/beh/wallpaper_daemon.sock";
char* S_path = malloc(strlen(xdg_dir)+strlen(prefix)+1);
//test0
//strcpy(S_path,"hello_c");
//works
//test 1
//strcpy(S_path,xdg_dir);
//strcat(S_path,prefix);
//doesn't work
//test2
strcpy(S_path,home);
strcat(S_path,hello_c);
//doesn't work
//test3
//strcpy(S_path,"/tmp/hello_c");
//works
char buf[2400];
printf("%s\nand its length: %li\ndo I have access: %i\n",S_path,strlen(S_path),access(S_path,F_OK));
int client_socket = socket(AF_UNIX,SOCK_STREAM,0);
struct sockaddr_un tosend;
memset(&tosend,0,sizeof(tosend));
tosend.sun_family = AF_UNIX;
strlcpy(tosend.sun_path,S_path,sizeof(tosend.sun_path));
strlcpy(buf,"this is working finally",sizeof(buf));
printf("%s\n",tosend.sun_path);
int connection_status = connect(client_socket,(struct sockaddr *) &tosend,sizeof(struct sockaddr));
if (connection_status == -1) perror("connection failed: ");
if (write(client_socket,buf,sizeof(buf)) == -1) perror("write failed: ");
free(S_path);
return 0;
}
server.c
#include "sys/types.h"
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
int main() {
const char* xdg_dir = getenv("XDG_RUNTIME_DIR");
char* prefix[] = {"/beh","/wallpaper_daemon.sock"};
const char* home = getenv("HOME");
const char* hello_c = "/hello_c";
char* S_path = malloc(strlen(xdg_dir)+strlen(prefix[0])+strlen(prefix[1])+1);
//test0
//strcpy(S_path,"hello_c");
//works
//test1
//strcpy(S_path,xdg_dir);
//strcat(S_path,prefix[0]);
//printf("%s\n",S_path);
//mkdir(S_path,0700);
//strcat(S_path,prefix[1]);
//doesn't work
//test2
strcpy(S_path,home);
strcat(S_path,hello_c);
//doesn't work
//test3
//strcpy(S_path,"/tmp/hello_c");
//works
printf("%s\nand its length: %li\n",S_path,strlen(S_path));
//get a socket for server
int S_sock = socket(AF_UNIX,SOCK_STREAM,0);
if (S_sock == -1) perror("getting socket failed: ");
struct sockaddr_un S_sock_F;
//clearing out the ram for the structure
memset(&S_sock_F,0,sizeof(struct sockaddr_un));
S_sock_F.sun_family= AF_UNIX;
strlcpy(S_sock_F.sun_path,S_path,sizeof(S_sock_F.sun_path));
int S_bind_state = bind(S_sock,
(struct sockaddr *)&S_sock_F,
sizeof(S_sock_F));
if (S_bind_state == -1) perror("binding failed: ");
int S_listen_state = listen(S_sock,5);
if (S_listen_state == -1) perror("listening failed: ");
//int S_accept = accept(S_sock,(struct sockaddr *)&S_sock_F,sizeof(struct sockaddr));
char buf[2400];
int S_accept = accept(S_sock,NULL,NULL);
//waiting for clients
ssize_t msglen;
msglen = read(S_accept,buf,sizeof(buf));
printf("%s\n",buf);
//perror("");
close(S_sock);
unlink(S_sock_F.sun_path);
free(S_path);
return 0;
}
the tests are included /tmp works and the said random directory works I didn't try more directories since well why doesn't my home directory work the problem is why can't I create sockets in xdg_runtime_dir or my home dir
steps to reproduce:
step 1: put both files in a random directory in your home
step 2: gcc server.c -o server && ./server
step 3: gcc client.c -o client && ./client
step 4: try out the other tests to see that they work thats all
what value is HOME: /home/myusername what value is xdg_runtime_dir: /run/user/1000
an image of one of the outputs
a better image now (basically can connect to server using nc -U and not with my client so its confirmed the problem is with the client the connection is getting refused from the client) I'm using btrfs
this is all happening in a random directory in my home /home/behe/Documents/development/beh/wallpaper_select
client.c
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
const char* xdg_dir = getenv("XDG_RUNTIME_DIR");
const char* home = getenv("HOME");
const char* hello_c = "/hello_c";
const char* prefix = "/beh/wallpaper_daemon.sock";
char* S_path = malloc(strlen(xdg_dir)+strlen(prefix)+1);
//test0
//strcpy(S_path,"hello_c");
//works
//test 1
//strcpy(S_path,xdg_dir);
//strcat(S_path,prefix);
//doesn't work
//test2
strcpy(S_path,home);
strcat(S_path,hello_c);
//doesn't work
//test3
//strcpy(S_path,"/tmp/hello_c");
//works
char buf[2400];
printf("%s\nand its length: %li\ndo I have access: %i\n",S_path,strlen(S_path),access(S_path,F_OK));
int client_socket = socket(AF_UNIX,SOCK_STREAM,0);
struct sockaddr_un tosend;
memset(&tosend,0,sizeof(tosend));
tosend.sun_family = AF_UNIX;
strlcpy(tosend.sun_path,S_path,sizeof(tosend.sun_path));
strlcpy(buf,"this is working finally",sizeof(buf));
printf("%s\n",tosend.sun_path);
int connection_status = connect(client_socket,(struct sockaddr *) &tosend,sizeof(struct sockaddr));
if (connection_status == -1) perror("connection failed: ");
if (write(client_socket,buf,sizeof(buf)) == -1) perror("write failed: ");
free(S_path);
return 0;
}
server.c
#include "sys/types.h"
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
int main() {
const char* xdg_dir = getenv("XDG_RUNTIME_DIR");
char* prefix[] = {"/beh","/wallpaper_daemon.sock"};
const char* home = getenv("HOME");
const char* hello_c = "/hello_c";
char* S_path = malloc(strlen(xdg_dir)+strlen(prefix[0])+strlen(prefix[1])+1);
//test0
//strcpy(S_path,"hello_c");
//works
//test1
//strcpy(S_path,xdg_dir);
//strcat(S_path,prefix[0]);
//printf("%s\n",S_path);
//mkdir(S_path,0700);
//strcat(S_path,prefix[1]);
//doesn't work
//test2
strcpy(S_path,home);
strcat(S_path,hello_c);
//doesn't work
//test3
//strcpy(S_path,"/tmp/hello_c");
//works
printf("%s\nand its length: %li\n",S_path,strlen(S_path));
//get a socket for server
int S_sock = socket(AF_UNIX,SOCK_STREAM,0);
if (S_sock == -1) perror("getting socket failed: ");
struct sockaddr_un S_sock_F;
//clearing out the ram for the structure
memset(&S_sock_F,0,sizeof(struct sockaddr_un));
S_sock_F.sun_family= AF_UNIX;
strlcpy(S_sock_F.sun_path,S_path,sizeof(S_sock_F.sun_path));
int S_bind_state = bind(S_sock,
(struct sockaddr *)&S_sock_F,
sizeof(S_sock_F));
if (S_bind_state == -1) perror("binding failed: ");
int S_listen_state = listen(S_sock,5);
if (S_listen_state == -1) perror("listening failed: ");
//int S_accept = accept(S_sock,(struct sockaddr *)&S_sock_F,sizeof(struct sockaddr));
char buf[2400];
int S_accept = accept(S_sock,NULL,NULL);
//waiting for clients
ssize_t msglen;
msglen = read(S_accept,buf,sizeof(buf));
printf("%s\n",buf);
//perror("");
close(S_sock);
unlink(S_sock_F.sun_path);
free(S_path);
return 0;
}
the tests are included /tmp works and the said random directory works I didn't try more directories since well why doesn't my home directory work the problem is why can't I create sockets in xdg_runtime_dir or my home dir
steps to reproduce:
step 1: put both files in a random directory in your home
step 2: gcc server.c -o server && ./server
step 3: gcc client.c -o client && ./client
step 4: try out the other tests to see that they work thats all
what value is HOME: /home/myusername what value is xdg_runtime_dir: /run/user/1000
an image of one of the outputs
a better image now (basically can connect to server using nc -U and not with my client so its confirmed the problem is with the client the connection is getting refused from the client) I'm using btrfs
Share Improve this question edited Jan 23 at 23:43 Beh_12517843 asked Jan 23 at 21:25 Beh_12517843Beh_12517843 417 bronze badges 12 | Show 7 more comments1 Answer
Reset to default 1I did not reproduce the "No such file or directory" error presented in the question with any variation on the socket path. I did get a "connection refused" error from the client in some cases, however, and it seems to be caused by an error in the client's connect()
call:
int connection_status = connect(client_socket,(struct sockaddr *) &tosend,sizeof(struct sockaddr));
The third argument should be the size of the actual address type, which in this case is struct sockaddr_un
, but you have instead passed the size of the struct sockaddr
type, which is probably smaller.
That whether the client works or not depends on the socket path is consistent with that. It is plausible that it works when the offset of the last byte of the path fits within or close to the size of a struct sockaddr
, but not when it exceeds that by too much. Changing the above line to ...
int connection_status = connect(client_socket, (struct sockaddr *) &tosend,
sizeof(struct sockaddr_un));
... resolved the issue for me, as did ...
int connection_status = connect(client_socket, (struct sockaddr *) &tosend,
sizeof(tosend));
. The latter is probably better form.
本文标签:
版权声明:本文标题:c - I can't connect to sockets in XDG_RUNTIME_DIR or my own home directory (binding is possible somehow) - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738463411a2088160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
S_path
? – Barmar Commented Jan 23 at 22:47XDG_RUNTIME_DIR
have in cases where the program fails? – John Bollinger Commented Jan 23 at 22:56