发UDP数据包给内网机器,如何做
发UDP数据包给内网机器,怎么做?
电信给我假的公网IP地址
我从路由器里面看,给我的地址是:
MAC地址:E4:D3:32:20:EA:FD
IP 地址:100.64.38.64 PPPoE拨号
子网掩码:255.255.255.255
网关:100.64.0.1
DNS服务器:218.85.157.99 218.85.152.99
但是我从百度输入“ 看自己的ip”,显示的我的公网地址是:
本机IP: 117.29.100.224福建省福州市 电信
我现在需要和公网上某个IP互相UDP通讯,要怎么设置才可以双向通讯?
------解决思路----------------------
计算机网络课本中有一个以UDP方式发送信息的代码,我自己敲了一遍
客户端:
import java.io.*;
import java.net.*;
class UDPClient{
public static void main(String args[]) throws Exception //如果程序除了错误就抛出异常
{
BufferedReader inFromUser= //创建流对象,申请缓冲区
new BufferedReader(new InputStreamReader(System.in));//将字节流转变为字符流
DatagramSocket clientSocket=new DatagramSocket(); //创建套接字clientSocket
InetAddress IPAddress=InetAddress.getByName("100.64.126.212"); //调用DNS查询,将主机名转换为ip地址或直接输入IP地址
byte[] sendData=new byte[1024]; //创建字符数组,存储要发送的字符
byte[] receiveData=new byte[1024];//接收服务器返回的字符
String sentence=inFromUser.readLine(); //从键盘读取字符
sendData=sentence.getBytes(); //将字符串转化为字符数组
DatagramPacket sendPacket= //将sendPacket通过套接字发送到网络
new DatagramPacket(sendData,sendData.length,
IPAddress,50);
clientSocket.send(sendPacket); //clientSocket通过send的方法得到刚构造的分组,然后直接向网络发送
DatagramPacket receivePacket= //创建放置回收分组的输入流
new DatagramPacket(receiveData,
receiveData.length);
clientSocket.receive(receivePacket); //将接收分组放到receivePacket中
String modifiedSentence=
new String(receivePacket.getData()) ; //getData()提取数据,,实现字节数组向字符串的转化
System.out.println("FROM SERVER:"+modifiedSentence);//输出服务器返回的数据
clientSocket.close();
}
}
服务器端:
import java.io.*;
import java.net.*;
class UDPServer{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket=new //创建UDP套接字,端口999
DatagramSocket(998);
while(true) //当端口有响应的时候进入循环
{
byte[] receiveData=new byte[1024];//存储接收的字符
byte[] sendData=new byte[1024]; //存储将要发送的字符
DatagramPacket receivePacket= //创建流输入,将字节流转化为字符流
new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket);//通过套接字接收客户端传送的字节流
String sentence=new String( //将接收的字节流转为字符串存储在sentence中
receivePacket.getData());
InetAddress IPAddress= //提取iP地址
receivePacket.getAddress();
int port=receivePacket.getPort(); //提取端口号
String capitalizedSentence= //将小写的字符转为大写的字符并存储在capitalizedSentence中
sentence.toUpperCase();
sendData=capitalizedSentence.getBytes();//将字符流转字节流
DatagramPacket sendPacket= //创建输出流存储要发送的信息
new DatagramPacket(sendData,sendData.length,IPAddress,port);
serverSocket.send(sendPacket); //通过端口sendPacket套接字发送输出流对象
// String a=new String(receiveData); //创建字符数组a
// System.out.println("FROM client:"+a); //显示从客户端输入的字符串
System.out.println("FROM client2:"+sentence);
}
}
}
电信给我假的公网IP地址
我从路由器里面看,给我的地址是:
MAC地址:E4:D3:32:20:EA:FD
IP 地址:100.64.38.64 PPPoE拨号
子网掩码:255.255.255.255
网关:100.64.0.1
DNS服务器:218.85.157.99 218.85.152.99
但是我从百度输入“ 看自己的ip”,显示的我的公网地址是:
本机IP: 117.29.100.224福建省福州市 电信
我现在需要和公网上某个IP互相UDP通讯,要怎么设置才可以双向通讯?
------解决思路----------------------
计算机网络课本中有一个以UDP方式发送信息的代码,我自己敲了一遍
客户端:
import java.io.*;
import java.net.*;
class UDPClient{
public static void main(String args[]) throws Exception //如果程序除了错误就抛出异常
{
BufferedReader inFromUser= //创建流对象,申请缓冲区
new BufferedReader(new InputStreamReader(System.in));//将字节流转变为字符流
DatagramSocket clientSocket=new DatagramSocket(); //创建套接字clientSocket
InetAddress IPAddress=InetAddress.getByName("100.64.126.212"); //调用DNS查询,将主机名转换为ip地址或直接输入IP地址
byte[] sendData=new byte[1024]; //创建字符数组,存储要发送的字符
byte[] receiveData=new byte[1024];//接收服务器返回的字符
String sentence=inFromUser.readLine(); //从键盘读取字符
sendData=sentence.getBytes(); //将字符串转化为字符数组
DatagramPacket sendPacket= //将sendPacket通过套接字发送到网络
new DatagramPacket(sendData,sendData.length,
IPAddress,50);
clientSocket.send(sendPacket); //clientSocket通过send的方法得到刚构造的分组,然后直接向网络发送
DatagramPacket receivePacket= //创建放置回收分组的输入流
new DatagramPacket(receiveData,
receiveData.length);
clientSocket.receive(receivePacket); //将接收分组放到receivePacket中
String modifiedSentence=
new String(receivePacket.getData()) ; //getData()提取数据,,实现字节数组向字符串的转化
System.out.println("FROM SERVER:"+modifiedSentence);//输出服务器返回的数据
clientSocket.close();
}
}
服务器端:
import java.io.*;
import java.net.*;
class UDPServer{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket=new //创建UDP套接字,端口999
DatagramSocket(998);
while(true) //当端口有响应的时候进入循环
{
byte[] receiveData=new byte[1024];//存储接收的字符
byte[] sendData=new byte[1024]; //存储将要发送的字符
DatagramPacket receivePacket= //创建流输入,将字节流转化为字符流
new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket);//通过套接字接收客户端传送的字节流
String sentence=new String( //将接收的字节流转为字符串存储在sentence中
receivePacket.getData());
InetAddress IPAddress= //提取iP地址
receivePacket.getAddress();
int port=receivePacket.getPort(); //提取端口号
String capitalizedSentence= //将小写的字符转为大写的字符并存储在capitalizedSentence中
sentence.toUpperCase();
sendData=capitalizedSentence.getBytes();//将字符流转字节流
DatagramPacket sendPacket= //创建输出流存储要发送的信息
new DatagramPacket(sendData,sendData.length,IPAddress,port);
serverSocket.send(sendPacket); //通过端口sendPacket套接字发送输出流对象
// String a=new String(receiveData); //创建字符数组a
// System.out.println("FROM client:"+a); //显示从客户端输入的字符串
System.out.println("FROM client2:"+sentence);
}
}
}