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

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:

时间: 2024-10-03 18:34:34

Socket programing(make a chat software) summary 1:How to accsess LAN from WAN的相关文章

12 Best Live Chat Software for Small Business Compared (2019) 最佳的wordpress在线聊天工具推荐插件 来帮你和潜在客户互动

12 Best Live Chat Software for Small Business Compared (2019) Did you know that more than 67% of users on eCommerce websites abandon their shopping carts without checking out? Customers want their questions to be answered instantly, and any uncertain

(OK) Linux epoll模型—socket epoll server client chat

http://www.cnblogs.com/venow/archive/2012/11/30/2790031.html http://blog.csdn.net/denkensk/article/details/41978015 定义: epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率.因为它会复用文件描述符集合来传递结果而不是迫使开发者每次等待事

(OK) Linux epoll模型—socket epoll server client chat—pthread

http://www.cnblogs.com/venow/archive/2012/11/30/2790031.html http://blog.csdn.net/denkensk/article/details/41978015 定义: epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率.因为它会复用文件描述符集合来传递结果而不是迫使开发者每次等待事

.NET开源高性能Socket通信中间件Helios介绍及演示

一:Helios是什么 Helios是一套高性能的Socket通信中间件,使用C#编写.Helios的开发受到Netty的启发,使用非阻塞的事件驱动模型架构来实现高并发高吞吐量.Helios为我们大大的简化了Socket编程,它已经为我们处理好了高并发情况下的解包,粘包,buffer管理等等. GitHub:https://github.com/helios-io/helios/ 二:Helios的特点 1.Powerful APIs Takes the complexity out of so

C# socket 实现消息中心向消息平台 转发消息 (修改)

using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using Jinher.AMP.SNS.Chat.Client; usin

C# socket 实现消息中心向消息平台 转发消息

公司用到,直接粘代码了 using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using Jinher.AMP.SNS.Chat.

Python Socket 编程——聊天室演示样例程序

上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和client的代码了解主要的 Python Socket 编程模型.本文再通过一个样例来加强一下对 Socket 编程的理解. 聊天室程序需求 我们要实现的是简单的聊天室的样例,就是同意多个人同一时候一起聊天.每一个人发送的消息全部人都能接收到,类似于 QQ 群的功能,而不是点对点的 QQ 好友之间的聊天.例如以下图: 图来自:http://www.ibm.com/developerworks/linux/tu

Socket.IO初探

建立Server //server.jsvar io = require('socket.io')(80); var chat = io .of('/chat') //设定命名空间 .on('connection', function (socket) { socket.emit('a message', { //这个只会发送给自己的socket that: 'only' , '/chat': 'will get' }); chat.emit('a message', { //全局发送 ever

C#高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)

原创性申明 本文作者:小竹zz  博客地址:http://blog.csdn.net/zhujunxxxxx/article/details/43573879转载请注明出处 引言 我一直在探寻一个高性能的Socket客户端代码.以前,我使用Socket类写了一些基于传统异步编程模型的代码(BeginSend.BeginReceive,等等)也看过很多博客的知识,在linux中有poll和epoll来实现,在windows下面 微软MSDN中也提供了SocketAsyncEventArgs这个类来