📄 dataser.java
字号:
/*
* 程序员杨龙:本视频会议服务器端代码:根据自己定义的协议:能构穿透nat 需要java jmf框架
* 我开放源代码是为了更好的让java这门程序艺术更好的发展:您可以研究代码,您可以自己使用
* 但是不要使用在商业用途上 让我们更好的把开源事业在中国做下去
* 2008年 3月 杨龙 完成服务器端的框架
* 信箱 :y_flash@163.com
* QQ:274797328
* www.0351soft.com www.lx08.com
*/
package ytp.javaserver.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
/**
*
* YangLong:2007-12-26
*/
/*===================================================================
= 功能:在端口2000侦听截获数据报文 然后返回新的数据报文 =
= 返回自己的地址和端口 =
===================================================================*/
public class dataSer {
private DatagramSocket dataSocket = null;
private int intPort = 2000;
private int intPortList = 2002;
private String IP = null;
public dataSer() {
if(this.init() != true)
{
this.init();
}
}
public String getIP()
{
return this.IP;
}
public int getPort()
{
return this.intPort;
}
private boolean init()
{
try
{
//读取配置文件 进行配置文件设置服务器
File file = new File("config/config.dat");
if(!file.exists())
{
return false;
}
if(file.length()<=10)
{
//首次启动 先配置服务器
new setConfig().setVisible(true);
}
FileInputStream fileIn = new FileInputStream(file);
int fileSize = fileIn.available();
byte[] byteRead = new byte[fileSize+1];
String strTemp = null;
//文件中有可以配置的信息
if(fileSize>0)
{
fileIn.read(byteRead);
strTemp = new String(byteRead); //构造配置文件字符串
String[] strs = strTemp.split("&");
for(int i=0; i<strs.length;i++)
{
//各自将信息写入到需要的文件框中
if(strs[i].startsWith("IP:")||strs[i].startsWith("ip:"))
{
IP = strs[i].substring(3);
// jLip.setText(strs[i].substring(3));
System.out.println(IP);
}
if(strs[i].startsWith("port:")||strs[i].startsWith("PORT:"))
{
this.intPort = Integer.parseInt(strs[i].substring(5));
// jLport.setText(strs[i].substring(5));
}
}
//建立一个socket端口侦听
System.out.println("一下是地址");
System.out.println(InetAddress.getByName(IP).getHostName());
dataSocket = new DatagramSocket(this.intPort,InetAddress.getByName(IP)); //根据配置文件建立一个端口
}else //文件中没有可以配置的信息
{
}
// dataSocket.bind(inet);
//接收消息
}catch(SocketException e)
{
// JOptionPane.showMessageDialog(this,"发生错误"+e);
// JOptionPane.showme
System.out.print(e.toString());
}catch(UnknownHostException e)
{
System.out.print(e.toString());
}catch(IOException e)
{
System.out.print(e.toString());
}catch(NullPointerException e)
{
System.out.println(e.toString());
}catch(Exception e)
{
System.out.print("发生错误"+e.toString());
}
return true;
}
public DatagramSocket getSocket()
{
if(this.dataSocket == null )
{
this.init();
System.out.println("获取建立的端口");
}
return this.dataSocket;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -