[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Problemer med linux socket programmering



Hej

Jeg forsoeger at skrive et library som kan bruges til IPC, og baade
fungere som server og client.

Jeg har dog det problem at accept og connect tilsyndeladende ikke
funger helt som jeg troede.

Foelgende er et lille test program jeg har skrevet til at illusterer problemet:

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <linux/un.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

struct sockaddr_un a;
static const char * path = "/tmp/socket";

int server() {
    int fd = socket (AF_UNIX, SOCK_STREAM, 0);
    fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK);

    unlink(path);
    bind(fd, (const struct sockaddr *)&a, sizeof(a));
    listen(fd, 0);

    sleep(2); // wait for client to connect
    return 0;
}

void client() {
    int fd = socket (AF_UNIX, SOCK_STREAM, 0);
    fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK);
    sleep(1); // make sure server have called listen
    int r = connect(fd, (struct sockaddr *)&a, sizeof(a));
    printf("r = %d\n", r);
}

int main(int argc, const char *argv[]) {
    int s, p;
    a.sun_family = AF_FILE;
    strncpy(a.sun_path, path, UNIX_PATH_MAX );

    if((p = fork()) == 0) return server();
    client();
    waitpid(p, &s, 0);
    return 0;
}

Hvis programmet kompileres:
gcc -static -Wall a.c -o a

og koeres:
strace -f -r -o a.trace ./a

vil man ved at kigge i a.trace kunne se a connect har retuneret 0
paatrods af at der ikke er nogen accept system kald i serveren, og at
listen har en backlog paa 0

Dette ville jeg ikke have forventet... Er der nogen work-around paa dette?

MVH
Allan


PS:
Hvis man ikke gider til at kompilere og koere programmet er output her:

28654      0.000000 execve("./a", ["./a"], [/* 74 vars */]) = 0
28654      0.000186 uname({sys="Linux", node="awn-pc", ...}) = 0
28654      0.000045 brk(0)              = 0x10c8000
28654      0.000021 brk(0x10c9180)      = 0x10c9180
28654      0.000025 arch_prctl(ARCH_SET_FS, 0x10c8860) = 0
28654      0.000030 brk(0x10ea180)      = 0x10ea180
28654      0.000021 brk(0x10eb000)      = 0x10eb000
28654      0.000037 clone(child_stack=0,
flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,
child_tidptr=0x10c8b30) = 28655
28654      0.000045 socket(PF_FILE, SOCK_STREAM, 0) = 3
28655      0.000023 socket(PF_FILE, SOCK_STREAM, 0 <unfinished ...>
28654      0.000006 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
28654      0.000020 rt_sigprocmask(SIG_BLOCK, [CHLD],  <unfinished ...>
28655      0.000013 <... socket resumed> ) = 3
28654      0.000005 <... rt_sigprocmask resumed> [], 8) = 0
28655      0.000008 fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK <unfinished ...>
28654      0.000006 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
28654      0.000023 rt_sigprocmask(SIG_SETMASK, [],  <unfinished ...>
28655      0.000007 <... fcntl resumed> ) = 0
28654      0.000005 <... rt_sigprocmask resumed> NULL, 8) = 0
28654      0.000008 nanosleep({1, 0},  <unfinished ...>
28655      0.000011 unlink("/tmp/socket") = 0
28655      0.000038 bind(3, {sa_family=AF_FILE, path="/tmp/socket"}, 110) = 0
28655      0.000046 listen(3, 0)        = 0
28655      0.000018 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
28655      0.000018 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
28655      0.000019 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
28655      0.000015 nanosleep({2, 0},  <unfinished ...>
28654      0.999927 <... nanosleep resumed> 0x7fffb4f08900) = 0
28654      0.000044 connect(3, {sa_family=AF_FILE, path="/tmp/socket"}, 110) = 0
28654      0.000075 fstat(1, {st_mode=S_IFCHR|0600,
st_rdev=makedev(136, 16), ...}) = 0
28654      0.000034 mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f474e631000
28654      0.000033 write(1, "r = 0\n", 6) = 6
28654      0.000023 wait4(28655,  <unfinished ...>
28655      0.999985 <... nanosleep resumed> 0x7fffb4f08900) = 0
28655      0.000099 exit_group(0)       = ?
28654      0.000083 <... wait4 resumed> [{WIFEXITED(s) &&
WEXITSTATUS(s) == 0}], 0, NULL) = 28655
28654      0.000013 --- SIGCHLD (Child exited) @ 0 (0) ---
28654      0.000022 exit_group(0)       = ?


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2011-07-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *