简体   繁体   English

如何通过C中的套接字号获取客户端IP

[英]How to get client IP by the socket number in C

I'm writing a simple client-server code in C. i was asked for the server to print the IP address of the client that connected to it. 我用C语言编写了一个简单的客户端-服务器代码。有人要求我打印与之连接的客户端的IP地址。 However, i can't seem to find a way to know the client's IP address from the server console. 但是,我似乎找不到从服务器控制台了解客户端IP地址的方法。 Is there a way to do that? 有没有办法做到这一点?

// Initialize Winsock.
if ( StartupRes != NO_ERROR )
{
    printf( "error %ld at WSAStartup( ), ending program.\n", WSAGetLastError() );
    // Tell the user that we could not find a usable WinSock DLL.                                  
    return;
}

/* The WinSock DLL is acceptable. Proceed. */

// Create a socket.    
MainSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( MainSocket == INVALID_SOCKET ) 
{
    printf( "Error at socket( ): %ld\n", WSAGetLastError( ) );
    return;
}

// Create a sockaddr_in object and set its values.
// Declare variables

Address = inet_addr(CHANNEL_IP);
if ( Address == INADDR_NONE )
{
    printf("The string \"%s\" cannot be converted into an ip address. ending program.\n",
            CHANNEL_IP);
    return;
}
service.sin_family = AF_INET;
service.sin_addr.s_addr = Address;
service.sin_port = htons(clientinfo->senderPort);

//Bind the socket
bindRes = bind( MainSocket, ( SOCKADDR* ) &service, sizeof( service ) );
if ( bindRes == SOCKET_ERROR ) 
{
    printf( "Channel-bind( ) failed with error %ld. Ending program\n", WSAGetLastError( ) );
    return;
}

 // Listen on the Socket.
ListenRes = listen( MainSocket, SOMAXCONN );
if ( ListenRes == SOCKET_ERROR ) 
{
    printf( "Failed listening on socket, error %ld.\n", WSAGetLastError() );
    return;
}
printf("Channel waiting for sender to connect...\n");

//Accepting connection
SenderSocket = accept( MainSocket, NULL, NULL );
    if ( SenderSocket == INVALID_SOCKET ){
        printf( "Accepting connection with client failed, error %ld\n", WSAGetLastError() ) ; 
        return;}
    else
        printf( "Sender Connected.\n" );

You need to pass in non-null values for the second and third parameters to accept : 您需要为第二个和第三个参数传递非null值才能accept

struct sockaddr_in client_addr;
socklen_t slen = sizeof(client_addr);
SenderSocket = accept( MainSocket, (struct sockaddr *)&client_addr, &slen );

You can then get the client's IP and port from client_addr . 然后,您可以从client_addr获取客户端的IP和端口。

暂无
暂无

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

相关问题 如何从套接字获取客户端的IP地址 - how to get ip address of client from socket 套接字 C 客户端特定编号 - socket C client specific number 在C中的服务器TCP / IP套接字编程中,客户端端口号显示错误 - Client Port number displaying wrong in server TCP/IP Socket Programming 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 C套接字编程:获取服务器绑定到的IP地址(IPv6或IPv4)和客户端连接到的地址? - C socket programming: get IP address (IPv6 or IPv4) server bounds to, and client connected to? 如何获得eth:使用C语言的UDP客户端的IP地址 - How to get the eth: IP address of Client in UDP in C language C(Linux)中的套接字-如何获取客户端IP和端口 - Sockets in C(Linux) - How do I get client IP & Port 在“ C”中的套接字编程中,如何找到连接到服务器的客户端的IP地址? - In socket prgramming in “C” how do I find out the IP address of the Client who is connecting to the Server? 如何使用c socket程序在报告中显示客户端的IP地址? - How do i display client's ip address in the report using c socket program? C TCP / IP套接字:如何将输入从客户端套接字同步到服务器套接字以交替进行 - C TCP/IP Sockets: How to synchronise input from client sockets to a server socket to be alternating
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM