[英]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 为了通过
accept
在client_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.