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 -;
}
for(pptr = hptr->h_addr_list ; *pptr != NULL; pptr++)
if (NULL != inet_ntop(hptr->h_addrtype, *pptr, ip, IP_SIZE) )
return ;
return -;
} int main(int argc, char **argv)
{
int sockfd, len;
struct sockaddr_in dest; char buffer[MAXBUF + ];
//1.create socket object
if((sockfd = socket(AF_INET, SOCK_STREAM, )) < )
{
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[]));
char ip[IP_SIZE];
get_ip_by_domain(argv[], ip);
//get the ip of server
if (inet_aton(ip, (struct in_addr *) &dest.sin_addr.s_addr) == )
{
perror(argv[]);
exit(errno);
}
//connect the server
if(connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) == -)
{
perror("connect");
exit(errno);
}
printf("server connected\n"); printf("pls send message to send:");
fgets(buffer,MAXBUF,stdin);
send(sockfd,buffer,strlen(buffer) - , );
return ;
}
/**
* 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[])
my_port = atoi(argv[]);
else
my_port = ;
if (argv[])
lisnum = atoi(argv[]);
else
lisnum = ;
struct sockaddr_in server_addr, clients_addr;
//create socket object
if((sockfd = socket(AF_INET, SOCK_STREAM, ))==-)
{
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[])
server_addr.sin_addr.s_addr = inet_addr(argv[]);
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)) == -)
{
perror("bind");
exit(EXIT_FAILURE);
}
//listern and wait for connecting
if(listen(sockfd,lisnum) == -)
{
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) == -))
{
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 ;
}
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的更多相关文章
- 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 ...
- (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/d ...
- .NET开源高性能Socket通信中间件Helios介绍及演示
一:Helios是什么 Helios是一套高性能的Socket通信中间件,使用C#编写.Helios的开发受到Netty的启发,使用非阻塞的事件驱动模型架构来实现高并发高吞吐量.Helios为我们大大 ...
- C# socket 实现消息中心向消息平台 转发消息 (修改)
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using ...
- C# socket 实现消息中心向消息平台 转发消息
公司用到,直接粘代码了 using System; using System.Collections.Generic; using System.Configuration; using System ...
- Python Socket 编程——聊天室示例程序
上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和客户端的代码了解基本的 Python Socket 编程模型.本文再通过一个例子来加强一下对 Socket 编程的 ...
- WPF socket通讯 UDP接收消息
客户端: private Socket socket; private IPEndPoint ipEndPoint; private void sendMessageHandler() { //服务端 ...
- C# 实现的多线程异步Socket数据包接收器框架
转载自Csdn : http://blog.csdn.net/jubao_liang/article/details/4005438 几天前在博问中看到一个C# Socket问题,就想到笔者2004年 ...
- Socket(TCP)客户端请求和服务端监听和链接基础(附例子)
一:基础知识回顾 一: Socket 类 实现 Berkeley 套接字接口. Socket(AddressFamily, SocketType,ProtocolType) 使用指定的地址族.套接字类 ...
随机推荐
- 学习微信小程序之css8
实战画一个圣诞树 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- ci框架登陆之后每隔几分钟就需要重新登录的问题
一个简单的登陆写好之后,发现每次进入需要登陆之后才能进入的页面都会跳转到登录页面,猜测应该是session被清了,打印出来,果然为空,但是我没有设置session的生存周期,按照默认的应该是24小时, ...
- 在powerdesigner中创建物理数据模型
物理数据模型(PDM)是以常用的DBMS(数据库管理系统)理论为基础,将CDM/LDM中所建立的现实世界模型生成相应的DBMS的SQL语言脚本.PDM叙述数据库的物理实现,是对真实数据库的描述 PDM ...
- 已知服务器ftp的账号密码,求解数据库表的内容
一开始觉得这两个是完全不相干的东西,直到出现了这样一个问题,对方网站只有ftp的账号密码,并且能正常访问到代码.但是当需求了解注册人数的时候,后台没有显示,只能到数据库去找,这时怎么找呢? 原来是可以 ...
- C#调用C++动态库方法及动态库封装总结
我只是粗浅的学习过一些C++语法, 变量类型等基础内容, 如有不对的地方还望指出. 如果你跟我一样, 对指针操作不了解, 对封装C++动态库头疼的话, 下面内容还是有帮助的. 转载请注明出处: htt ...
- Ajax浅学习
写在前面:响应事件 function addEvent(obj, type, fn) { if (obj.addEventListener) { obj.addEventListener(type, ...
- 【枚举】POJ 3279
直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...
- Web前端面试之HTML
1. 对WEB标准以及W3C的理解与认识 web标准规范要求,书写标签闭合.小写.不乱嵌套,可提高搜索机器人对网页内容的搜索几率.--- SEO 使用外链css和js脚本,结构与行为.结构与表现分离, ...
- Qt for Mac 安装(包括PyQt)
下载Qt: http://qt-project.org/downloads Qt5.2 for Mac:http://download.qt-project.org/official_releases ...
- java并发编程(十八)阻塞队列和阻塞栈
阻塞队列 阻塞队列是Java 5并发新特性中的内容,阻塞队列的接口是java.util.concurrent.BlockingQueue,它有多个实现类:ArrayBlockingQueue.Dela ...