简体   繁体   English

C AF_UNIX中的连接拒绝套接字编程

[英]Connection refused socket programming in C AF_UNIX

Hi i'm trying to make a simple program that forks and then should act like client/server. 嗨,我正在尝试制作一个简单的分叉程序,然后像客户端/服务器一样工作。 Here is my code: 这是我的代码:

int main (){
int sfd,fdc;
struct sockaddr_un sa;
strncpy(sa.sun_path,SOCKNAME,UNIX_PATH_MAX);
sa.sun_family = AF_UNIX;
char buf[N+1];

if (fork() != 0){
    sfd = socket(AF_UNIX,SOCK_STREAM,0);
    bind (sfd, (struct sockaddr *) &sa ,sizeof(sa));
    listen(sfd, SOMAXCONN);
    fdc= accept(sfd,NULL,0);
    read(fdc,buf,N);
    printf("Server got: %s\n",buf);
    write(fdc,"bye!",5);
    close(fdc);
    close(sfd);
    exit(EXIT_SUCCESS);
}
else {
    sfd = socket(AF_UNIX,SOCK_STREAM,0);

    while(connect(sfd, (struct sockaddr *)&sa,sizeof(sa)) == -1){
        if (errno == ENOENT){
            printf("Aspetto 1 sec\n");
            sleep(1);
        }
        else {perror(NULL); exit(EXIT_FAILURE) ;}   
    }
    write(sfd,"Hallo!",7);
    read(sfd,buf,N);
    printf("Client got: %s\n",buf);
    close(sfd);
    exit(EXIT_SUCCESS);
    }

I don't understand why this fail when i try to connect the client to server. 我不明白为什么在尝试将客户端连接到服务器时此操作失败。 I got this error: Connection Refused. 我收到此错误:连接被拒绝。 I can't really find where the problem is, thanks for your help. 感谢您的帮助,我真的找不到问题所在。

First, regarding simple program that forks and then should act like client/server : Having a client and server in the same logical block is not a good idea. 首先,关于分叉然后应该像客户端/服务器一样工作的简单程序 :将客户端服务器放在同一逻辑块中不是一个好主意。 although it can be written to be syntactically correct (ie it will compile and build), I do not see how it will ever be practical. 尽管可以将其写成在语法上正确(即可以编译和构建),但我看不出它将如何实用。

There are many tutorial examples, using Posix C to create separate server and client apps. 有许多教程示例,使用Posix C创建单独的服务器和客户端应用程序。 Here is one . 这是一个 Here is another , and a third . 这是另一个第三个

One general suggestion, when using a family of functions that have prerequisites (such as a handle for a socket, or for a previous successful call of another function), you should always use the return value. 一个普遍的建议是,在使用具有前提条件的函数系列(例如套接字的句柄或先前成功调用另一个函数)时,应始终使用返回值。 For example, in the following lines of your example code: 例如,在示例代码的以下行中:

if (fork() != 0){
    sfd = socket(AF_UNIX,SOCK_STREAM,0);
    bind (sfd, (struct sockaddr *) &sa ,sizeof(sa));
    listen(sfd, SOMAXCONN);
    fdc= accept(sfd,NULL,0); 

the only line you choose to verify is the result of fork (that is good). 您选择验证的唯一一行是fork的结果(很好)。 But you should also check the results of each of the others, using the results as a prerequisite of continuing to the next step. 但是,您还应该检查其他每个结果,并将结果作为继续进行下一步的先决条件。

At the very least something like this: 至少是这样的:

if (fork() != 0)
{
    sfd = socket(AF_UNIX,SOCK_STREAM,0);
    if(sfd < 0)
    {
        //notify user
        return -1;
    }
    ret = bind (sfd, (struct sockaddr *) &sa ,sizeof(sa));
    if(ret < 0)
    {
        //check value of errno, and notify user
        return -1;
    }
    ret = listen(sfd, SOMAXCONN);
    if(ret < 0)
    {
        //check value of errno, and notify user
        return -1;
    }
    new_s= accept(sfd,NULL,0); 
    if(new_s < 0)
    {
        //check value of errno, and notify user
        return -1;
    }
    // continue...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM