使用INADDR_LOOPBACK的环回示例不起作用 [英] Loopback example using INADDR_LOOPBACK does not work

查看:333
本文介绍了使用INADDR_LOOPBACK的环回示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C中设置回送套接字,但没有任何效果.我正在尝试制作一个函数,该函数使用环回地址打开套接字,将数据发送到套接字,然后从另一个函数读取数据,但是没有任何效果.我相信我不知道如何使用与连接有关的功能.这是我到目前为止所完成的:

I am trying to set a loopback socket in C but nothing works. I'm trying to make a function that opens a socket with the loopback address ,send data to socket and from another function read the data but nothing works. I believe that I don't know how to use the functions related to connections. Here is what I accomplished so far:

#include <sys/wait.h>       
#include <sys/types.h>       
#include <sys/socket.h>     
#include <netinet/in.h>       
#include <netdb.h>           
#include <unistd.h>             
#include <stdlib.h>         
#include <ctype.h>          
#include <signal.h>         
#include <iostream>
#include <cerrno>
#include <pthread.h>

int internal_s;

void function1(){
    if ((internal_s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        perror_exit("socket");

    /* Find server address */
    struct sockaddr_in loopback;
    struct sockaddr *serverptr = (struct sockaddr*)&loopback; 

    /*Convert port number to integer*/        
    loopback.sin_family = AF_INET;       /* Internet domain */
    loopback.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
    loopback.sin_port=htons(10000);

    /* Initiate connection */
    if (bind(internal_s,serverptr, sizeof(loopback))<0)
        perro("bind");

    int test=1;
    err=write(internal_s,&test,sizeof(int));
    if(err<0)
        perror(write);
}

void Open_Internal_sock(int socket_s){
    struct sockaddr_in loopback;
    struct sockaddr *serverptr = (struct sockaddr*)&loopback; 

    /*Convert port number to integer*/        
    loopback.sin_family = AF_INET;       /* Internet domain */
    loopback.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
    loopback.sin_port=htons(10000);

    /* Initiate connection */
    if (bind(socket_s,serverptr, sizeof(loopback))<0)
        perror("bind");//Invalid argument
    int test;
    if(read(socket_s,&test,sizeof(int))<0)
        perror("read");//herer it prints:Transport endpoint is not connected
}

int main(){
    function1(i);
    Open_Internal_sock(internal_s);
}

推荐答案

简而言之,客户端(发送者,写者")需要调用connect(),而服务器(侦听器,接收者,阅读者")需要调用listen()accept().

In short, the client(sender, "writer") needs to call connect() and the server(listener, receiver, "reader") needs to cal listen() and accept().

服务器和客户端还需要单独的执行线程,因为某些套接字操作会阻塞并且会导致单个执行线程永远停止.最简单的方法可能是将server.c和client.c制作为单独的程序.

The server and client also need separate threads of execution, because some of the socket operations block and would cause a single thread of execution to stop forever. Easiest is probably to make a server.c and client.c as separate programs.

此外,尝试在启用警告的情况下编译代码,例如gcc -Wall.现在有很多错误,编译器可以为您指出.要获得更清晰的消息,请尝试使用clang而不是gcc作为编译器.

Additionally, try compiling your code with warnings enabled, e.g., gcc -Wall . There are now quite many errors, which the compiler can point out for you. For clearer messages, try clang instead of gcc as a compiler.

我建议您查看 http://kohala.com/start/unpv12e/unpv12e. tar.gz .用tar xzvf unpv12e.tar.gz解压缩,然后查看unpv12e/tcpcliserv/tcpcli01.c和unpv12e/tcpcliserv/tcpserv01.c.如果您想复制和粘贴,请注意,例如Listen()中的大写字母需要更改为小写,这样代码才能在没有unpv头的情况下工作.此更改还删除了所有错误检查,因此请自行处理错误.

I suggest looking at http://kohala.com/start/unpv12e/unpv12e.tar.gz . Unpack with tar xzvf unpv12e.tar.gz and look at unpv12e/tcpcliserv/tcpcli01.c and unpv12e/tcpcliserv/tcpserv01.c . In case you are tempted to copy&paste, notice that the Capital letters in, e.g., Listen() need to be changed to lower case for the code to work without unpv headers. This change also removes all checks for errors, so put in your own error handling.

这篇关于使用INADDR_LOOPBACK的环回示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆