您的位置:首页 > 其它

Udp内网穿透终于测试成功

2006-01-14 16:59 821 查看
//file: Test.java
package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.zip.CRC32;
import java.util.zip.GZIPOutputStream;

import field.coze.Server.UserData;
import field.coze.common.FileHashtable;
import field.coze.common.StringPacket;

public class Test {

public void SaveArry(){
Hashtable ht = new Hashtable();
String [] a = new String [2];
a[0] = "00000000000";
a[1] = "111111111111";

ht.put("a",a);
ht.put("b","bbbbbbbbb");
ht.put("b1","bbbbbbbbb");
ht.put("b2","bbbbbbbbb");
ht.put("b3","bbbbbbbbb");
String [] c = (String [])ht.get("a");
System.out.println(c[1]);
File f = new File("test.txt");
File f1 = new File("test1.txt");
try {
FileOutputStream fos = new FileOutputStream(f);
FileOutputStream fos1 = new FileOutputStream(f1);
GZIPOutputStream gzipo = new GZIPOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(gzipo);
ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
oos.writeObject(ht);
oos1.writeObject(ht);
oos.close();
fos.close();
oos1.close();
fos1.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void server(){
System.out.println("in server");
byte [] ary = new byte [1000];
DatagramPacket dp = new DatagramPacket(ary,ary.length);

try {
DatagramSocket ds = new DatagramSocket(1111);
int i = 0;
while (true) {
i++;
Thread.sleep(1000);
System.out.println("ReceiveBufferSize: "
+ ds.getReceiveBufferSize());
ds.receive(dp);
String strAddress = dp.getAddress().toString().substring(1);
String strPort = String.valueOf(dp.getPort());

System.out.println(strAddress);
System.out.println(strPort);
System.out.println(new String(dp.getData()));
System.out.println(dp.getData().toString());

DatagramPacket dp1 = new DatagramPacket(ary, ary.length);
dp1.setAddress(InetAddress.getByName(strAddress));
dp1.setPort(Integer.parseInt(strPort));
ds.send(dp1);
System.out.println("i = " + i);
}

} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void testStrToBytes(){
String a = "";
byte [] b = a.getBytes();
System.out.println(b.length);
}
public void testStaticMethod(){
String [] a = new String [3];
a[0] = "58.24.216.238";
a[1]="5888";
InetAddress add = StringPacket.getInetAddress(a);
System.out.println(add);
System.out.println(StringPacket.getPort(a));

}
void testReturn(int i){
if( i <3){
System.out.println("i<3");
return;
}
else{
System.out.println("i=" + i);
}
}
void testReturnExe(){
testReturn(2);
System.out.println("ffffff");
}
void fileExist(){
File f = new File("user.data");
Hashtable ht = new Hashtable();

if(f.exists()){

}
else{
try {
FileOutputStream fos = new FileOutputStream(f);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
void testFileHashtable(){
FileHashtable fh = new FileHashtable("b1.dat");
fh.ht.put("a","aaa");
fh.updateHt();

//fh.readHt();
System.out.println("--------in test -------------/n"
+ fh.ht);
}
void deleteFile(){
File f = new File("aa1.dat");
System.out.println(f.delete());
}
void testUserData(){
UserData ud = new UserData();
//ud.addRecord("field1","qweqwe","aaa");
//ud.readHt();
//System.out.println(ud.getNickName("field1"));
//System.out.println(ud.getPSW("field1"));
Enumeration e = ud.ht.elements();
System.out.println("-----------show all elements--------");
while(e.hasMoreElements()){
System.out.println(e.nextElement());
}
}
void IntegerToBytes(){
int i = 33;
String s = String.valueOf(i);
s = "00" + s ;
System.out.println(s);
int ii = Integer.parseInt(s);
System.out.println(ii);
}
void testCException() throws CException{
throw new CException();
}
void runTestException(){
try {
testCException();
} catch (CException e) {
System.out.println(e.getMessage());
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
void testCRC(){
CRC32 crc = new CRC32();

}
void testUdpHoleServer(){
DatagramSocket ds = null;
DatagramPacket dp = null;
byte [] ary = new byte [512];
try {
ds = new DatagramSocket(1111);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i = 0;
LinkedList ll = new LinkedList();
while(i<2){
i++;
dp = new DatagramPacket(ary,ary.length);
try {
ds.receive(dp);
System.out.println("dp.getAddress().getHostAddress() = " + dp.getAddress().getHostAddress());
System.out.println("dp.getAddress().getHostName() = " + dp.getAddress().getHostName());
System.out.println(dp.getPort());
String strIP = dp.getAddress().getHostAddress();
String strPort = String.valueOf(dp.getPort());
StringBuffer bf = new StringBuffer(strIP);
while(bf.length()<15){
bf.append(" ");
}
strIP = bf.toString();
bf = new StringBuffer(strPort);
while(bf.length()<5){
bf.append(" ");
}
strPort = bf.toString();
bf = new StringBuffer();
bf.append(strIP).append(strPort);
ll.add(ll.size(),bf.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("packet "+ i );
}
System.out.println("packet "+ i );
System.out.println("------------成功接受2个包-------------");

String strA = (String ) ll.get(0);
String strB = (String ) ll.get(1);
System.out.println("strA="+strA + strA.length());
System.out.println("strB="+strB + strB.length());
System.out.println(strA.substring(0,15));
System.out.println(strA.substring(15,20));

dp.setAddress(getAddress(strA.substring(0,15)));
System.out.println(strA.substring(15,20));
dp.setPort(getPort(strA.substring(15,20)));
dp.setData(strB.getBytes());
try {
ds.send(dp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

dp.setAddress(getAddress(strB.substring(0,15)));
dp.setPort(getPort(strB.substring(15,20)));
dp.setData(strA.getBytes());
try {
ds.send(dp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
int getPort(String port){
System.out.println("--------this is in method getPort-----------" + port.trim());
return Integer.parseInt(port.trim());
}
InetAddress getAddress(String address){
InetAddress add = null;
try {
add = InetAddress.getByName(address.trim());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return add;

}
void testFillStr(){
String strIP = "1.0.1.0";
String strPort ="20";
int port = 0;
StringBuffer bf = new StringBuffer(strIP);
while(bf.length()<15){
bf.append(" ");
}
strIP = bf.toString();
bf = new StringBuffer(strPort);
while(bf.length()<10){
bf.append(" ");
}
strPort = bf.toString();
port = Integer.parseInt(strPort.trim());
System.out.println("strPort.length() = " +strPort.length() );
System.out.println("strPort="+strPort+"===");
System.out.println("port -1 = " + --port);
System.out.println(strIP.length() );
System.out.println(strIP );

}
public static void main(String[] args) {

Test t = new Test();
//t.testFillStr();
t.testUdpHoleServer();
//t.runTestException();
//t.IntegerToBytes();
//t.testUserData();
//t.deleteFile();
//t.testFileHashtable();
//t.fileExist();
//fh1.ht.put("a","aaaaa");
//fh1.updateHt();

//fh.readHt();

//t.SaveArry();
//t.server();
//t.testStrToBytes();
//t.testStaticMethod();
//t.testReturnExe();

}

}
//client
//file:

package test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import field.coze.common.StringPacket;

public class Test98 {

void teshHC() {
String a = "field";
DatagramPacket dp = new DatagramPacket(a.getBytes(),
a.getBytes().length);
String strIP = null;
String strPort = null;
String strSend = null;
DatagramSocket ds = null;
try {
dp.setAddress(InetAddress.getByName("58.24.216.238"));
dp.setPort(1111);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ds = new DatagramSocket(2000);
ds.send(dp);
System.out.println("打洞包发送!");
//------------------接受打洞包 ---
byte[] ary1 = new byte[512];
DatagramPacket dp1 = new DatagramPacket(ary1, ary1.length);
ds.receive(dp1);
System.out.println("server中介包收到");
//int length = Integer.parseInt(new String(ary1,0,4));
strIP = new String(ary1, 0, 15);
strPort = new String(ary1, 15, 5);
strSend = "打洞成功";
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//----------------------
DatagramPacket dp3 = new DatagramPacket(new byte[512], 512);
try {
dp3.setAddress(InetAddress.getByName(strIP.trim()));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dp3.setPort(Integer.parseInt(strPort.trim()));
dp3.setData(strSend.getBytes());
dp3.setData(strSend.getBytes());

try {
ds.setSoTimeout(6000);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int i = 0;
while (i<2) {
i++;
try {
ds.send(dp3);
System.out.println("已向内网发送包 " + i);
byte[] ary2 = new byte[512];
DatagramPacket dp2 = new DatagramPacket(ary2, ary2.length);
ds.receive(dp2);
System.out.println(dp2.getAddress().toString() + "/n"
+ dp2.getPort() + "/n" + new String(dp2.getData()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

public static void main(String[] args) {
Test98 t = new Test98();
t.teshHC();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: