1. 程式人生 > >accept獲取對端IP地址,getsockname獲取本端IP地址

accept獲取對端IP地址,getsockname獲取本端IP地址

getpeername函式用於獲取與某個套接字關聯的外地地址,accept函式在接收連線的時候也會獲取對端的地址。getsockname用於獲取本端的地址。看程式碼。

服務端:

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
 
int main()
{
    int sockSrv = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addrSrv;
    addrSrv.sin_family = AF_INET;
    addrSrv.sin_addr.s_addr =htonl(INADDR_ANY);
    addrSrv.sin_port = htons(8888);
 
    bind(sockSrv, (const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));
    listen(sockSrv, 5);
 
    struct sockaddr_in addrClient;
    socklen_t len = sizeof(struct sockaddr_in);
 
    int sockConn = accept(sockSrv, (struct sockaddr *)&addrClient, &len);
    printf("ip=%s\n",inet_ntoa(addrClient.sin_addr));
    printf("port=%d\n",ntohs(addrClient.sin_port));
 
    getchar();
    close(sockConn);
    close(sockSrv);
 
    return 0;
}

客戶端:

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <error.h>
 
int main()
{
     int ret=0;
     int sockClient = socket(AF_INET, SOCK_STREAM, 0);
 
     struct sockaddr_in addrSrv;
     addrSrv.sin_addr.s_addr=inet_addr("127.0.0.1");
     addrSrv.sin_family = AF_INET;
     addrSrv.sin_port = htons(8888);
     ret=connect(sockClient, ( const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));
 
     struct sockaddr_in addrClient;
     socklen_t len=sizeof(addrClient);
 
     getsockname(sockClient,(struct sockaddr *)&addrClient,&len);
     printf("ip=%s\n",inet_ntoa(addrClient.sin_addr));
     printf("port=%d\n",ntohs(addrClient.sin_port));
 
     getchar();
     close(sockClient);
 
     return 0;
}

在核心態

可以這樣寫:

struct socket * tnfs_socket_create(void)
{
        struct socket * sock;

        int ret 	= 0;
		struct timeval timeout = {3,0};

        sock = (struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);

        if(sock == NULL)
        {
                tnfs_print("client : sock kmalloc error");
                return sock;
        }

        // ret = sock_create_kern(&init_net,AF_INET,SOCK_STREAM,0,&sock);
  	    ret = sock_create_kern(AF_INET,SOCK_STREAM,0,&sock);
        if(ret < 0)
        {
            tnfs_print("clinet : sock create error: %d \n",ret);
		    //tnfs_socket_release(sock);
        }else{
            tnfs_print("client : sock create ok!\n");
			kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,(const char*)&timeout,sizeof(timeout));
			kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(const char*)&timeout,sizeof(timeout));
			
			struct sockaddr_in addrClient;
			int len=sizeof(addrClient);
 
			kernel_getsockname(sock,(struct sockaddr *)&addrClient,&len);
			//tnfs_print("ip=%s\n",inet_ntoa(addrClient.sin_addr));
			tnfs_print("port=%d\n",ntohs(addrClient.sin_port));

        }
		
        return sock;
}