简体   繁体   English

如何从接受调用获取套接字端口号(C UNIX)

[英]how get socket port number from an accept call (C UNIX)

i've done a simple client/server program where the server wait for an external connection and return the connection-socket if the port number of the client is in the range of [1025-2048] otherwise return -1. 我已经完成了一个简单的客户端/服务器程序,其中服务器等待外部连接,如果客户端的端口号在[1025-2048]范围内,则返回连接套接字,否则返回-1。 The problem is that when i get the port number by the client adress (which should be stored in the sockaddr structure) it says me that the client port number is zero, but in the client program i've set the client portnumber to 1999. 问题是,当我通过客户端地址获取端口号(应该存储在sockaddr结构中)时,它说客户sockaddr为零,但是在客户端程序中,我将客户端端口号设置为1999。

SERVER 服务器

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>


int function(int fd_socket) {

    int fd_socket_acc;
    int len;
    int port;
    struct sockaddr_in client_addr;

    puts("WAITING FOR CLIENT...");

    fd_socket_acc = accept(fd_socket, (struct sockaddr*)&client_addr, &len);

    puts("CONNECTION DONE.");

    port = ntohs (client_addr.sin_port);
    printf("client port number: %d \n", port);


    if (port >= 1024 && port <= 2048) {

        close (fd_socket_acc);
        return fd_socket_acc;
    }   
    else {
        close(fd_socket_acc);
        return -1;
    }
}

int main(int argc, char *argv[]) {


    int fd_socket;
    struct sockaddr_in local_addr;

    fd_socket = socket(AF_INET, SOCK_STREAM, 0);

    local_addr.sin_family = AF_INET;
    local_addr.sin_port = htons(1887);
    local_addr.sin_addr.s_addr = INADDR_ANY;

    bind(fd_socket, (struct sockaddr*)&local_addr, sizeof(local_addr));

    listen(fd_socket, 3);

    function(fd_socket);

    //close(fd_socket);

}

CLIENT 客户

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

int main(int argc, char *argv[]) {

    int fd_socket;
    struct sockaddr_in local_addr;
    struct sockaddr_in server_addr;
    struct hostent *hp;

    fd_socket = socket(AF_INET, SOCK_STREAM, 0);

    local_addr.sin_family = AF_INET;
    local_addr.sin_port = htons(1999);
    local_addr.sin_addr.s_addr = INADDR_ANY;

    bind(fd_socket, (struct sockaddr*)&local_addr, sizeof(local_addr));

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(1887);
    //hostname is "ubuntu"
    hp = gethostbyname("ubuntu");
    bcopy(hp->h_addr, &server_addr.sin_addr, 4);

    printf("%d \n", ntohs(local_addr.sin_port));

    connect(fd_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));

    wait(2);

    close(fd_socket);

}

If i get the port number in client with a printf("%d", ntohs(local_addr.sin_port)) it stamps correctly 1999 , but if i get the port number of client in server with printf("%d", ntohs(client_addr.sin_port)) it stamps 0 . 如果我使用printf("%d", ntohs(local_addr.sin_port))获得客户端的端口号,它会正确标记1999 ,但是如果我使用printf("%d", ntohs(client_addr.sin_port))标记为0 Why? 为什么?

thanks in advance! 提前致谢!

In order to obtain the client port number in client_addr through accept you have to tell accept how big that buffer is by setting 为了通过acceptclient_addr获得客户client_addr ,您必须通过设置来告诉accept该缓冲区的大小

socklen_t len = sizeof(client_addr);

You can alternatively retrieve it by calling afterwards 您也可以稍后致电来获取

len = sizeof(client_addr);
getpeername(fd_socket_acc, (struct sockaddr*)&client_addr, &len);

Maybe because you do not set the variable len to anything, and I suspect that your compiler sets it to 0. 可能是因为您没有将变量len设置为任何值,并且我怀疑您的编译器将其设置为0。
What happens is that you try to accept with an undefined len size. 发生的情况是您尝试接受未定义的len大小。

Adding len=sizeof( struct sockaddr_in ); 添加len=sizeof( struct sockaddr_in ); before making a call to accept would help to fill the passed client_addr correctly. 在进行接受之前,将有助于正确填充传递的client_addr。

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

相关问题 如何从 unix c 中的 struct addrinfo 获取端口号 - How to get the port number from struct addrinfo in unix c Unix socket编程中如何获取本地IP地址和端口? - How to get local IP address and port in Unix socket programming? 在C套接字编程中使用accept()和connect()时,主机端口号是多少 - what's the host port number when we use accept() and connect() in C socket programming Unix串口编程:如何获取输出缓冲区中的字节数? - Unix serial port programming: How to get number of bytes in output buffer? C 套接字从接受返回的文件描述符获取 IP 地址 - C socket get IP address from filedescriptor returned from accept 如何接受指向具有任意数量参数的 function 的指针作为 C 中的参数并调用它? - How to accept pointer to a function with any number of parameters, as a parameter in C and call it? Unix 套接字卡在接受() - Unix socket gets stuck at accept() 如何通过C中的套接字号获取客户端IP - How to get client IP by the socket number in C 如何在同一C程序中侦听套接字并连接到不同的套接字(相同的IP地址但不同的端口号) - How to listen to a socket and connect to a different socket(same ip address but different port number) in a same c program unix套接字上的连接数 - Number of connections on unix socket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM