码迷,mamicode.com
首页 > 其他好文 > 详细

Socket programing(make a chat software) summary 1:How to accsess LAN from WAN

时间:2016-10-06 22:33:29      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

First we should know some basic conceptions about network:

         1.Every PC is supposed to have its own IP,So we can connent other‘s PC by WAN.That‘s just like a ID of netwrok world.

    2.But if  every PC has its own IP,Then IPs of this world will be insufficient.Because IPv4 is only 32-bit.

    3.So, in fact we are not using a IP of WAN, but using a IP of LAN.

     If there is a IP of 192.168.2.100 in one LAN, and there will also be another IP of 192.168.2.100 in another LAN.

            It‘s not contradict because we use NAT to convert the IP of LAN into the IP of WAN(the unique ip).

 

As for my software,I need two PC connected and chatting.But the IP of normal users are all LAN. different LANs.So they can not connect just by Socket program.

 

So,I must convert the IP from LAN to WAN.

At least one --- the server‘s.

 

I use a technique of PORT MAPPING. implemented by  a software called phddns.

 

Download the phddns: phddns_3.0_x86_64.deb

dpkg -i phddns_3.0_x86_64.deb

技术分享

 

Use the SN and password to log in the website of phddns.

Then set the network penetration:

 

技术分享

As for the code, very simple C socket program.

/**
 * client program of chat
 * by localhost pur whoami Atum
 * 0ct 5 2016 
 * version 1.0
 **/

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <netdb.h>  
#include <net/if.h>  
#include <sys/ioctl.h>  
#include <sys/types.h>

#define MAXBUF 1024
#define MAC_SIZE    18  
#define IP_SIZE     16  

// get ip by domain  
int get_ip_by_domain(const char *domain, char *ip)  
{  
    char **pptr;  
       struct hostent *hptr;   
     hptr = gethostbyname(domain);  
       if(NULL == hptr)  
       {  
            printf("gethostbyname error for host:%s/n", domain);  
               return -1;  
        }  
        for(pptr = hptr->h_addr_list ; *pptr != NULL; pptr++)  
            if (NULL != inet_ntop(hptr->h_addrtype, *pptr, ip, IP_SIZE) )   
                    return 0; 
        return -1;  
} 

int main(int argc, char **argv)
{
    int sockfd, len;
    struct sockaddr_in dest;

        char buffer[MAXBUF + 1];
    //1.create socket object
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        exit(errno);
    }
    printf("socket created.\n");
    //set dest 0
    bzero(&dest, sizeof(dest));
    //address protocal
    dest.sin_family = AF_INET;
    //the port of server
    dest.sin_port = htons(atoi(argv[2]));
    char ip[IP_SIZE];
    get_ip_by_domain(argv[1], ip);
    //get the ip of server
    if (inet_aton(ip, (struct in_addr *) &dest.sin_addr.s_addr) == 0)
    {
        perror(argv[1]);
        exit(errno);
    }
    //connect the server
    if(connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) == -1)
    {
        perror("connect");
        exit(errno);
    }
    printf("server connected\n");

    printf("pls send message to send:");
    fgets(buffer,MAXBUF,stdin);
    send(sockfd,buffer,strlen(buffer) - 1, 0);
    return 0;
}

 

/**
 * server program of lsp-chat 
 * by     WHOAMI localhost purstar Atum
 * Oct 5 2016
 * version 1.0    
 **/

#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
    unsigned int my_port, lisnum;
    int sockfd, new_fd;
    socklen_t len;
    if (argv[2])
        my_port = atoi(argv[2]);
    else
        my_port = 7575;
    if (argv[3])
        lisnum = atoi(argv[3]);
    else
        lisnum = 5; 
    struct sockaddr_in server_addr, clients_addr;
    //create socket object
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0))==-1)
    {
        perror("socket");
        exit(EXIT_FAILURE);
    }
    //set server_addr 0
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(my_port);
    if (argv[1])
        server_addr.sin_addr.s_addr = inet_addr(argv[1]);
    else
        server_addr.sin_addr.s_addr = INADDR_ANY;
    //bind the socket with ip and port
    if (bind(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) == -1)
    {
        perror("bind");
        exit(EXIT_FAILURE);
    }
    //listern and wait for connecting
    if(listen(sockfd,lisnum) == -1)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    printf("wait for connect\n");
    len = sizeof(struct sockaddr);
    //get the client connection
    if((new_fd = accept(sockfd, (struct sockaddr *) &clients_addr, &len) == -1))
    {
        perror("accept");
        exit(EXIT_FAILURE);
    }
    else
        printf("server: got connection from %s, port %d, socket %d\n",inet_ntoa(clients_addr.sin_addr),ntohs(clients_addr.sin_port),new_fd);
    return 0;
}

 

Then run the server program and the client program:

技术分享

 parameters are local ip and port.

技术分享

parameters are domain name and port mapped by phddns

 

It works.

 

Test with my friend‘s help,also no problem:

技术分享

Socket programing(make a chat software) summary 1:How to accsess LAN from WAN

标签:

原文地址:http://www.cnblogs.com/rixiang/p/5934655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!