谁能给我提供一些用java编写网络程序的源代码???(200分)

  • 主题发起人 主题发起人 loneboy
  • 开始时间 开始时间
L

loneboy

Unregistered / Unconfirmed
GUEST, unregistred user!
谁能给我提供一些用java编写网络程序的源代码???
比如说:用java 写代理服务器等等
我的email是:handsome_2000@263.net
谢谢。
 
package Socket.File_Loader;
import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.Math.*;
public class File_Loader
{
int Loader_Mode=-1;
/**
Loader_Mode 的四种模式
-1 没有设置模式
0 请求上传模式(要求把本地的一个文件传送到远端的地址)
1 请求下载模式(要求远端地址的一个文件传送到本地)
2 响应上传模式(响应模式 0 ,把本地的一个文件传送到请求端)
3 响应下载模式(响应模式 1 ,要求把请求端的一个文件下载到本地)
*/
//全部大写为可以从初始文件中得到数据
//public int MAX_CONNECT_USER;
public int MAX_ERROR_REPEAT;//出现错误的重试次数
public int IN_LOCALPORT=11113;
//本地端口
public String IN_LOCALHOST=null;
//本地主机名或ip地址
public int MAX_SENDFILE_NUM;//同时连接的最大传送文件数
public String RECEIVEINIFILEPATH=null;
//传送文件使用的信息文件存放位置
public String SOURCEDIRECTORY;//存放发送文件的目录
public String DESTDIRECTORY;//存放接收文件的目录
public String in_LocalHost;//本程序使用的本机的一个ip地址,或者本机名称
// public int in_LocalPort=IN_LOCALPORT;//本地端口
ServerSocket ListenSocket;//监听用套接字
Socket[] RequestSocket;
//请求用套接字
FileLink_State FState_Send=new FileLink_State();//开启发送用文件管理器
FileLink_State FState_Receive=new FileLink_State();//开启接收用文件管理器
public boolean Listened=false;
//是否已启动监听
Main_Listen listen=null;//定义唯一一个监听器
int ListenCount=0;
int Sequence_Listen=-1;

//构造函数
public File_Loader()
{
IniData();
}
private void IniData()//从初始化文件读取初始数据
{
Init_Files ini=new Init_Files("c:/","File_Loader.ini");
RECEIVEINIFILEPATH=ini.GetValue("RECEIVEINIFILEPATH");
MAX_SENDFILE_NUM=Integer.parseInt(ini.GetValue("MAX_SENDFILE_NUM"));
IN_LOCALHOST=ini.GetValue("IN_LOCALHOST");
IN_LOCALPORT=Integer.parseInt(ini.GetValue("IN_LOCALPORT"));
MAX_ERROR_REPEAT=Integer.parseInt(ini.GetValue("MAX_ERROR_REPEAT"));
SOURCEDIRECTORY=ini.GetValue("SOURCEDIRECTORY");//存放发送文件的目录
DESTDIRECTORY=ini.GetValue("DESTDIRECTORY");//存放接收文件的目录
FState_Send=new FileLink_State();//开启发送用文件管理器
FState_Receive=new FileLink_State();//开启接收用文件管理器
RequestSocket=new Socket[MAX_SENDFILE_NUM];
}
//传送完整的一个文件
public void SendFile()
{
}
//从文件的某一个位置开始传递文件
private void SendFromPosition(int FromPosition)
{
}
//请求上传文件
public void Request_UpLoad(String RemoteHost,
String RemoteFileName,
String LocalFileName)//套接字分类标示:0
{
{
Socket ReqSocket=null;
try
{
int lport;
ReqSocket=new Socket(RemoteHost,IN_LOCALPORT);
BufferedOutputStream bos=new BufferedOutputStream(ReqSocket.getOutputStream(),512);
BufferedInputStream bis=new BufferedInputStream(ReqSocket.getInputStream(),512);
byte[] writebyte=new byte[6];
for(int i=0;i<4;i++)
{
writebyte="sstt".getBytes();
}
writebyte[4]=75;
writebyte[5]=11;
bos.write(writebyte);
bos.flush();
byte[] readbyte=new byte[7];
bis.read(readbyte);
if (BytesEquals("sstt".getBytes(),0,readbyte,0,4)==true)
{
if(readbyte[4]==26)
{
int lportl=((readbyte[6]>0)?((int)(readbyte[6])):(((int)(readbyte[6]))&amp;((int)255)));
lport=readbyte[5]*256+lportl;
}
else
return;
}
else
{
return;
}
ReqSocket=new Socket(RemoteHost,lport);
bos=new BufferedOutputStream(ReqSocket.getOutputStream(),512);
bis=new BufferedInputStream(ReqSocket.getInputStream(),512);
byte[] outbyte=new byte[512];
for(int i=0;i<4;i++)
{
outbyte="sstt".getBytes();
}
outbyte[4]=21;
byte[] dfileb=RemoteFileName.getBytes();
byte[] sfileb=LocalFileName.getBytes();
for(int i=5;i<sfileb.length+5;i++)
{
outbyte=sfileb[i-5];
}
for(int i=sfileb.length+5;i<220;i++)
{
outbyte=" ".getBytes()[0];
}
for(int i=220;i<dfileb.length+220;i++)
{
outbyte=dfileb[i-220];
}
for(int i=dfileb.length+220;i<435;i++)
{
outbyte=" ".getBytes()[0];
}
System.out.println(1);
bos.write(outbyte);
bos.flush();
System.out.println(1);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
//请求下载文件
public void Request_Download(String RemoteHost,
String RemoteFileName,
String LocalFileName,
int FileStartPos //文件开始的位置
)//套接字分类标示:1
{
Request_DownloadFile rdf1=new Request_DownloadFile(RemoteHost,RemoteFileName,LocalFileName,FileStartPos,-1);
rdf1.start();
}
public class Request_DownloadFile extends Thread
{
String RemoteHost;
String RemoteFileName;
String LocalFileName;
int FileStartPos;
int filespos;
public Request_DownloadFile(String RemoteHost,
String RemoteFileName,
String LocalFileName,
int FileStartPos, //文件开始的位置
int fsp//在文件管理器中的位置
)//套接字分类标示:1
{
this.RemoteHost=RemoteHost;
this.RemoteFileName=RemoteFileName;
this.LocalFileName=LocalFileName;
this.FileStartPos=FileStartPos;
//文件开始的位置
this.filespos=fsp;
}
public void run()
{
if(filespos==-1)
{
filespos=FState_Receive.getMinCanUsedPos();
}
int FileLength=0;
try
{
if (filespos!=-1)
{
int lport;
RequestSocket[filespos]=new Socket(RemoteHost,IN_LOCALPORT);
BufferedOutputStream bos=new BufferedOutputStream(RequestSocket[filespos].getOutputStream(),512);
BufferedInputStream bis=new BufferedInputStream(RequestSocket[filespos].getInputStream(),512);
byte[] writebyte=new byte[6];
for(int i=0;i<4;i++)
{
writebyte="sstt".getBytes();
}
writebyte[4]=75;
writebyte[5]=11;
bos.write(writebyte);
bos.flush();
byte[] readbyte=new byte[7];
bis.read(readbyte);
if (BytesEquals("sstt".getBytes(),0,readbyte,0,4)==true)
{
if(readbyte[4]==26)
{
int lportl=((readbyte[6]>0)?((int)(readbyte[6])):(((int)(readbyte[6]))&amp;((int)255)));
lport=readbyte[5]*256+lportl;
}
else
return;
}
else
{
return;
}
//////////////
RequestSocket[filespos].close();
RequestSocket[filespos]=new Socket(RemoteHost,lport);
bos=new BufferedOutputStream(RequestSocket[filespos].getOutputStream(),512);
bis=new BufferedInputStream(RequestSocket[filespos].getInputStream(),512);
byte[] osb=new byte[512];//请求下载用文件流
byte[] st="sstt".getBytes();
for(int i=0;i<4;i++) //0--3 "sstt"
{
osb=st;
}
System.out.println(String.valueOf((int)(osb[0]))+String.valueOf((int)(osb[1]))+String.valueOf((int)(osb[2]))+String.valueOf((int)(osb[3])));
osb[4]=1;
// 4 请求下载的标志:1
byte[] rfbyte=RemoteFileName.getBytes();
byte[] lfbyte=LocalFileName.getBytes();
for(int i=5;i<5+RemoteFileName.length();i++)
{
osb=rfbyte[i-5];
//5--219被下载的源文件名长度不足补空格
}
for(int i=RemoteFileName.length()+5;i<220;i++)
{
osb=" ".getBytes()[0];
}
for(int i=220;i<220+LocalFileName.length();i++)
{
osb=lfbyte[i-220];
//220--434下载后的目的文件名长度 不足补空格
}
for(int i=220+LocalFileName.length();i<435;i++)
{
osb=" ".getBytes()[0];
}
byte[] fileposbyte=new byte[4];
for(int i=0;i<4;i++)
{
fileposbyte=(byte)(FileStartPos>>(3-i)*8);
}
for(int i=435;i<439;i++)
{
osb=fileposbyte[i-435];
//435--439被请求文件的开始位置
}
int AcceptCode;
Random seed1=new Random();
for(;;)
{
AcceptCode=seed1.nextInt();//随机产生一个确认号
if(FState_Receive.GetPosFromAccept(AcceptCode)>-1)//载本地检查确认号码是否重复
{
continue;
}
byte[] isRepeatCode=new byte[9];
//以下 载本地检查确认号码是否重复
isRepeatCode[0]=115;
isRepeatCode[1]=115;
isRepeatCode[2]=116;
isRepeatCode[3]=116;
isRepeatCode[4]=10;
isRepeatCode[5]=(byte)(AcceptCode>>24);
isRepeatCode[6]=(byte)(AcceptCode>>16);
isRepeatCode[7]=(byte)(AcceptCode>>8);
isRepeatCode[8]=(byte)(AcceptCode);
bos.write(isRepeatCode);
bos.flush();
byte[] head=new byte[4];
bis.read(head);
System.out.println("xxx"+String.valueOf((int)(head[0]))+String.valueOf((int)(head[1]))+String.valueOf((int)(head[2]))+String.valueOf((int)(head[3])));
if(BytesEquals(head,0,"sstt".getBytes(),0,4)==true)
{
System.out.println("进入");
int p=bis.read();
if(p==11)
{
System.out.println("p==11");
bis.read(head);
break;
}
else
if(p==12)
{
System.out.println("有重复");
bis.read(head);
}
else
{
bis.close();
System.out.println("有错误发生");
return;
}
}
}
for(int i=0;i<4;i++)
{
osb[439+i]=(byte)(AcceptCode>>((3-i)*8));
}
System.out.println("确认码比特流"+String.valueOf((int)(osb[439]))+String.valueOf((int)(osb[439+1]))+String.valueOf((int)(osb[439+2]))+String.valueOf((int)(osb[439+3])));
for(int i=443;i<512;i++)
{
osb=32;
}
FState_Receive.sFileNames[filespos]=RemoteFileName;//设置被传送的源文件
FState_Receive.dFileNames[filespos]=LocalFileName;//设置被传送的目的文件
FState_Receive.sHost[filespos]=RemoteHost;
FState_Receive.dHost[filespos]=IN_LOCALHOST;
FState_Receive.Current_ReceivePos[filespos]=FileStartPos;
FState_Receive.isSend[filespos]=false;
FState_Receive.AcceptCode[filespos]=AcceptCode;
Init_Files inifile=new Init_Files(RECEIVEINIFILEPATH,"Fini"+Integer.toString(filespos)+".ini");
inifile.SetValue("sFileName",RemoteFileName);
inifile.SetValue("dFileName",LocalFileName);
inifile.SetValue("sHost",RemoteHost);
inifile.SetValue("dHost",IN_LOCALHOST);
inifile.SetValue("Current_ReceivePos",Integer.toString(FileStartPos));
inifile.SetValue("isSend",Boolean.FALSE.toString());
inifile.SetValue("FileEnd","false");
inifile.SetValue("FileLength",Integer.toString(FileLength));
System.out.println("开始写");
//载发送新标志之前要重新建立连接
RequestSocket[filespos]=new Socket(RemoteHost,lport);
bos=new BufferedOutputStream(RequestSocket[filespos].getOutputStream(),512);
bis=new BufferedInputStream(RequestSocket[filespos].getInputStream(),512);
bos.write(osb);//把请求信息流传递过去
bos.flush();
System.out.println("写完");
byte recbyte[]=new byte[11];
bis.read(recbyte);
byte[] head=new byte[4];
for(int i=0;i<4;i++)
{
head=recbyte;
}
System.out.println(String.valueOf((int)(head[0]))+String.valueOf((int)(head[1]))+String.valueOf((int)(head[2]))+String.valueOf((int)(head[3])));
if(BytesEquals(head,0,"sstt".getBytes(),0,4)==true)
{
if(recbyte[4]==4)
{
System.out.println("接收到端口号");
System.out.println(AcceptCode);
if(AcceptCode==ByteToInt(recbyte,5))
{
System.out.println("接收到相同确认码.");
int rportl=((recbyte[10]>0)?((int)(recbyte[10])):(((int)(recbyte[10]))&amp;((int)255)));
int rport=recbyte[9]*256+rportl;
NewDownload ndl1=new NewDownload(RemoteHost,
rport,FileStartPos,AcceptCode,LocalFileName,RemoteFileName,filespos);
System.out.println("上传用端口"+rport);
ndl1.start();
}
}
}
}
else
{
return;
}
}
catch(IOException e)
{
System.out.println("1:"+e);
ErrorProcess();
}
// }//end for
}
}
//上传
private void Upload()
{
}
//下载
private voiddo
wnload()
{
}
//监听
public void ListenIt()
{
if (this.Listened==false)
{
Main_Listen listen=new Main_Listen();
listen.start();
}
else
{
System.out.println("监听已启动");
return;
}
}
public void StopListenIt()
{
if(this.Listened==true)
{
listen.destroy();
}
else
System.out.println("监听未启动");
}
public class Main_Listen extends Thread
{
public Main_Listen()
{
}
public void run()
{
try
{
if (IN_LOCALHOST==null)
{
ListenSocket=new ServerSocket(IN_LOCALPORT,MAX_SENDFILE_NUM);
}
else
{
ListenSocket=new ServerSocket(IN_LOCALPORT,MAX_SENDFILE_NUM,InetAddress.getByName(IN_LOCALHOST));
}
//////////////////////////////////////'
Socket LinkSocket=null;
for(;;)
{
try
{
Listen ls;
LinkSocket=ListenSocket.accept();
BufferedInputStream bis=new BufferedInputStream(LinkSocket.getInputStream(),256);
BufferedOutputStream bos=new BufferedOutputStream(LinkSocket.getOutputStream(),256);
byte[] head=new byte[4];
bis.read(head);
if (BytesEquals(head,0,"sstt".getBytes(),0,4)==false)
{
LinkSocket.close();
continue;
}
if(bis.read()==75 &amp;&amp;
bis.read()==11)
{
Sequence_Listen=FState_Send.
getMinCanUsedPos();
ls=new Listen(Sequence_Listen);
ls.start();
while(ls.LPort==0)
{
try
{
this.sleep(10);
}
catch(InterruptedException e)
{
System.out.println("2:"+e);
}
}
System.out.println("生成得端口号是:"+ls.LPort);
byte[] writebyte=new byte[7];
for(int i=0;i<4;i++)
{
writebyte="sstt".getBytes();
}
writebyte[4]=26;
writebyte[5]=(byte)(ls.LPort>>8);
writebyte[6]=(byte)(ls.LPort);
bos.write(writebyte);
bos.flush();
////////////////////
}
}
catch(IOException e)
{
System.out.println("3:"+e);
}
finally
{
try
{
if (LinkSocket!=null) LinkSocket.close();
}
catch (IOException e)
{
System.out.println("4:"+e);
ErrorProcess();
}
}
}
}
catch(IOException e)
{
System.out.println("5:"+e);
}
}
}
public class Listen extends Thread
{
int Sequence;
public int LPort=0;
int lastnu=-1;
public Listen(int sequence_listen)
{
Sequence=sequence_listen;
}
public void run()
{
ServerSocket LSocket;
try
{
if (IN_LOCALHOST==null)
{
LSocket=new ServerSocket(0,0);
}
else
{
LSocket=new ServerSocket(0,0,InetAddress.getByName(IN_LOCALHOST));
}
//////////////////////////////////////'
LPort=LSocket.getLocalPort() ;
Socket LinkSocket=null;
for(;;)
{
try
{
LinkSocket=LSocket.accept();
BufferedInputStream bis=new BufferedInputStream(LinkSocket.getInputStream(),512);
byte[] head=new byte[4];
bis.read(head);
if (BytesEquals(head,0,"sstt".getBytes(),0,4)==false)
{
LinkSocket.close();
}
int ch=bis.read();
System.out.println("监听号码:"+ch);
switch (ch)
{
case 0:
{
byte[] fsname=new byte[215];
byte[] fdname=new byte[215];
bis.read(fsname);
String fsnamestr=new String(fsname,"GB2312");
bis.read(fdname);
String fdnamestr=new String(fdname,"GB2312");
byte[] fstart=new byte[4];
bis.read(fstart);
int filestart;//要传送文件的开始位置
filestart=fstart[0]*16777216+fstart[1]*65536+fstart[2]*256+fstart[3];//把byte型转换成int型
}
case 1://当监听到请求下载文件信息时
{
lastnu=1;
byte[] sfilenameb=new byte[215];
byte[] dfilenameb=new byte[215];
bis.read(sfilenameb);
bis.read(dfilenameb);
byte[] filestartbyte=new byte[4];
bis.read(filestartbyte);
int FileStartPos=0;
for(int i=0;i<4;i++)
{
FileStartPos=ByteToInt(filestartbyte,0);
}
byte[] AcceptCodeb=new byte[4];
bis.read(AcceptCodeb);
int AcceptCode=0;
AcceptCode=ByteToInt(AcceptCodeb,0);
System.out.println("AcceptCode="+AcceptCode);
int filespos=Sequence;
if (filespos==-1)
{
//反馈系统信息:超出最大文件传送数
LinkSocket.close();
}
String sfilename=new String(sfilenameb);//源文件名字符串
String dfilename=new String(dfilenameb);//目的文件名字符串
FState_Send.
sFileNames[filespos]=sfilename;//设置被传送的源文件
FState_Send.
dFileNames[filespos]=dfilename;//设置被传送的目的文件
FState_Send.
sHost[filespos]=IN_LOCALHOST;
FState_Send.
dHost[filespos]=(LinkSocket.getInetAddress()).getHostAddress();
FState_Send.
Current_ReceivePos[filespos]=FileStartPos;
FState_Send.
isSend[filespos]=true;
FState_Send.
AcceptCode[filespos]=AcceptCode;
System.out.println(AcceptCode);
NewUpload nuld=new NewUpload(sfilename,FileStartPos,AcceptCode);
nuld.start();

//以下获取上传线程的本地端口,将此端口传到请求下载方
boolean canexit=false;//是否可以退出下面的循环
while(canexit==false)
{
if (nuld.out_Prepared==1)
{
FState_Send.
sPort[filespos]=nuld.out_SendLocalPort;
System.out.println("检测处:"+nuld.out_SendLocalPort);
canexit=true;
}
}
BufferedOutputStream bos=new BufferedOutputStream(LinkSocket.getOutputStream(),512);
byte[] outst=new byte[11];//建立回应下载流
for(int i=0;i<4;i++)
{
outst="sstt".getBytes();
outst[i+5]=AcceptCodeb;
}
outst[4]=4;
outst[9]=(byte)((FState_Send.
sPort[filespos])>>8);
outst[10]=(byte)(FState_Send.
sPort[filespos]);
bos.write(outst);
bos.flush();
break;
}
case 21://监听请求上传
{
System.out.println(21);
lastnu=21;
byte[] fname=new byte[215];
bis.read(fname);
String RFileName=new String(fname);
bis.read(fname);
String LFileName=new String(fname);
String RHost=new String(LinkSocket.getInetAddress().getHostAddress());
Request_Download(RHost,RFileName,LFileName,0);
/////////////////////
break;
}
case 10://确认请求下载传递的监听号码
{
byte[] atp=new byte[4];
bis.read(atp);
int AptCode=atp[0]*16777216+atp[1]*65536+atp[2]*256+atp[3];
BufferedOutputStream bos=new BufferedOutputStream(LinkSocket.getOutputStream(),9);
if(FState_Send.
GetPosFromAccept(AptCode)<0)
{
byte[] rtapt=new byte[13];
rtapt[0]=115;
rtapt[1]=115;
rtapt[2]=116;
rtapt[3]=116;
System.out.println("产生可以使用的号码");
rtapt[4]=11;
//传送到请求下载端,此确认码可以使用
rtapt[5]=atp[0];
rtapt[6]=atp[1];
rtapt[7]=atp[2];
rtapt[8]=atp[3];
bos.write(rtapt);
bos.flush();
}
else
{
byte[] rtapt=new byte[9];
rtapt[0]=115;
rtapt[1]=115;
rtapt[2]=116;
rtapt[3]=116;
rtapt[4]=12;
//传送到请求下载端,此确认码不可以使用
rtapt[5]=atp[0];
rtapt[6]=atp[1];
rtapt[7]=atp[2];
rtapt[8]=atp[3];
bos.write(rtapt);
bos.flush();
}
}
}//end switch
}
catch(IOException e)
{
System.out.println("6:"+e);
ErrorProcess();
}
finally
{
try
{
if (LinkSocket!=null) LinkSocket.close();
}
catch (IOException e)
{
System.out.println("7:"+e);
ErrorProcess();
}
}
if (lastnu==1 || lastnu==21)break;
}//end for
}
catch(IOException e)
{
System.out.println("8:"+e);
ErrorProcess();
}
}//end listen
}
private void ErrorProcess()//错误纪录
{
}
public void setLoader_Mode(int Loader_Mode) {
this.Loader_Mode = Loader_Mode;
switch (Loader_Mode)
{
case 0:
{
}
case 1:
{
}
case 2:
{
}
case 3:
{
}
default:
{
}
}
}
public int getLoader_Mode() {
return Loader_Mode;
}
public class FileLink_State//正在传送的文件和文件信息管理。
{
public boolean[] CurrentSendFileLink;//当前正在传送文件的使用情况的记录,为true表示正在传送,由MAX_SENDFILE_NUM决定该数组的最大个数
public String[] sFileNames;//所有文件信息
public String[] dFileNames;//所有文件信息
public String[] sHost;
public String[] dHost;
public int[] sPort;//源端口
public boolean[] isSend;
//是否为发送方,是发送防为true,接收方为false
public int[] Current_ReceivePos;//文件当前被传送的字节数,只在文件接收端使用
public int[] AcceptCode;//文件确认号码
public FileLink_State()
{
CurrentSendFileLink=new boolean[MAX_SENDFILE_NUM];//当前正在传送文件的使用情况的记录,为true表示正在传送,由MAX_SENDFILE_NUM决定该数组的最大个数
sFileNames=new String[MAX_SENDFILE_NUM];//所有文件信息
dFileNames=new String[MAX_SENDFILE_NUM];//所有文件信息
sHost=new String[MAX_SENDFILE_NUM];
dHost=new String[MAX_SENDFILE_NUM];
sPort=new int[MAX_SENDFILE_NUM];//源端口
isSend=new boolean[MAX_SENDFILE_NUM];//是否为发送方,是发送防为true,接收方为false
Current_ReceivePos=new int[MAX_SENDFILE_NUM];//文件当前被传送的字节数,只在文件接收端使用
AcceptCode=new int[MAX_SENDFILE_NUM];//文件确认号码
for(int i=0;i<MAX_SENDFILE_NUM;i++)
{
sFileNames=new String();
dFileNames=new String();
sHost=new String();
dHost=new String();
}
for(int i=0;i<MAX_SENDFILE_NUM;i++)
{
CurrentSendFileLink=false;
}
}
public synchronized int getMinCanUsedPos()//读取可用的最小的一个文件位置
{
int MinCanUsedPos;
for (int i=0 ;i<MAX_SENDFILE_NUM;i++)
{
if (CurrentSendFileLink==false)
{
MinCanUsedPos=i;
System.out.println("设位占用");
CurrentSendFileLink=true;
return MinCanUsedPos;
}
}
MinCanUsedPos=-1;
return MinCanUsedPos;
}
public synchronized int GetPosFromAccept(int AptCode)
{
int pos=-1;
for(int i=0;i<MAX_SENDFILE_NUM;i++)
{
if (CurrentSendFileLink==true)
{
if(AptCode==AcceptCode)
{
System.out.println("返回acceptcode的位置"+i);
pos=i;
return i;
}
}
}
return pos;
}
public int GetUsedCount()//获得已经使用的文件管理的数目
{
int usedcount=0;
for(int i=0;i<MAX_SENDFILE_NUM;i++)
{
if (CurrentSendFileLink==true)
usedcount++;
}
return usedcount;
}
public int GetUsedPos(int used)//获取已用管理序数表示的文件管理的位置
{
int usedcount=0;
for(int i=0;i<MAX_SENDFILE_NUM;i++)
{
if (CurrentSendFileLink==true)
{
usedcount++;
if (usedcount-1==used)
{
return i;
}
}
}
return usedcount;
}
}//end class FileLink_State

public class NewDownload extends Thread//接收文件方线程
{
public String in_RemoteHost=null;//远程ip地址,或者远程机器名称
public int in_RemotePort=0;//远程端口
public int AcceptCode;
private int FileStartPos;
public int Current_ReceivePos;
public int FileLength;
boolean Request_ReDownload=false;
private String dFileName,dFileName_nopath,sFileName;
private int filespos;
public NewDownload(String in_RemoteHost,int in_RemotePort,int FileStartPos,int AcceptCode,String dFileName,String sFileName,int fsp)
{
this.in_RemoteHost=in_RemoteHost;
this.in_RemotePort=in_RemotePort;
this.AcceptCode=AcceptCode;
this.FileStartPos=FileStartPos;
this.Current_ReceivePos=FileStartPos;
this.dFileName=DESTDIRECTORY+dFileName;
this.dFileName_nopath=dFileName;
this.sFileName=sFileName;
this.filespos=fsp;
}
public void run()
{
Socket recsocket=null;
int filespos;
try
{
//产生一个文件管理记录inifile
System.out.println("进入下载:"+FState_Receive.GetPosFromAccept(AcceptCode));
filespos=FState_Receive.GetPosFromAccept(AcceptCode);
if (filespos==-1)
{
Request_ReDownload=true;
return;
}
Init_Files inifile=new Init_Files(RECEIVEINIFILEPATH,"Fini"+Integer.toString(filespos)+".ini");
recsocket=new Socket(in_RemoteHost,in_RemotePort);
int ch;
BufferedInputStream isr=new BufferedInputStream(recsocket.getInputStream());
//FileOutputStream fis=new FileOutputStream("dFileName");
RandomAccessFile fis=new RandomAccessFile(dFileName,"rw");
fis.seek(FileStartPos);
byte[] fileseg=new byte[2048];//带有附加信息的字节比特数组
byte[] filecontent=new byte[2028];//去掉附加信息的字节比特数组
System.out.println("Request_ReDownload="+Request_ReDownload);
if (Current_ReceivePos!=0)//当文件不是第一个流发送时取得该文件长度
{
FileLength=Integer.parseInt(inifile.GetValue("FileLength"));
}
for(int i=0;;i++)
{
ch=isr.read(fileseg);
boolean fileend=false;
if (ch==-1) break;
//判断程序标识和确认码是否相符,如果次不相符则终止下载,并且要求重新请求下载
byte[] stacr=new byte[8];
byte[] stacl=new byte[8];
for (int j=0;j<8;j++)
{
stacr[j]=fileseg[j];
stacl[j]="sstt ".getBytes()[j];
}
stacl[4]=(byte)(AcceptCode>>24);
stacl[5]=(byte)(AcceptCode>>16);
stacl[6]=(byte)(AcceptCode>>8);
stacl[7]=(byte)(AcceptCode);
if (BytesEquals(stacr,0,stacl,0,8)==false)
{
//System.out.println("BytesEquals(stacr,0,stacl,0,8)==false");
Request_ReDownload=true;
break;
}
//判断文件开始位置是否相符,如果连续两次不相符则终止下载,并且要求重新请求下载
int reccurpos;
if(Current_ReceivePos==0)
{
reccurpos=0;
}
else
{
reccurpos=ByteToInt(fileseg,8);
}
if (reccurpos!=Current_ReceivePos)
{
//System.out.println("reccurpos!=Current_ReceivePos");
//System.out.println(reccurpos);
System.out.println("reccurpos!=Current_ReceivePos"+Current_ReceivePos);
Request_ReDownload=true;
break;
}
if (Current_ReceivePos==0)//当文件的第一个流发送时取得该文件长度
{
FileLength=ByteToInt(fileseg,8);
inifile.SetValue("FileLength",Integer.toString(FileLength));;
}
//判断传送流是否发送了文件结束标志
int strelen=ByteToInt(fileseg,12);
byte[] fien=new byte[4];
for(int j=0;j<4;j++)
{
fien[j]=fileseg[j+16+strelen];
}
if(BytesEquals(fien,0,"fien".getBytes(),0,4)==true)
{
fileend=true;
}
for(int j=0;j<ch-20;j++)
{
filecontent[j]=fileseg[j+16];
}
if (fileend==false)
{
fis.write(filecontent);
Current_ReceivePos+=2028;
FState_Receive.Current_ReceivePos[filespos]=Current_ReceivePos;
inifile.SetValue("Current_ReceivePos",Integer.toString(Current_ReceivePos));
}
else
{
byte[] filecontent2=new byte[ByteToInt(fileseg,12)];
System.out.println("最后一次接收长度1:"+ByteToInt(fileseg,12));
for(int j=0;j<ByteToInt(fileseg,12);j++)
{
filecontent2[j]=fileseg[j+16];
}
fis.write(filecontent2);
Current_ReceivePos+=ByteToInt(fileseg,12);
FState_Receive.Current_ReceivePos[filespos]=Current_ReceivePos;
inifile.SetValue("Current_ReceivePos",Integer.toString(Current_ReceivePos));
inifile.SetValue("FileEnd","true");
/* if (Current_ReceivePos!=FileLength)
{
System.out.println("Current_ReceivePos!=FileLength"+Current_ReceivePos+" "+FileLength);
Request_ReDownload=true;
break;
}
*/
System.out.println("结束111");
break;
}
}
System.out.println("完成");
fis.close();
isr.close();
System.out.println("Request_ReDownload="+Request_ReDownload);
if (Request_ReDownload==true)
{
Request_DownloadFile rqdl=new Request_DownloadFile(in_RemoteHost,sFileName,
dFileName_nopath,(Current_ReceivePos-2028*2)>0?(Current_ReceivePos-2028*2):0,filespos);
rqdl.start();
}
else
{
FState_Receive.CurrentSendFileLink[filespos]=false;
}
}
catch(IOException e)
{
System.out.println("9:"+e+"网络未连接");
}
}//end run
}

public class NewUpload extends Thread//发送文件方线程
{
ServerSocket SendServerSocket;
//文件传送用发送方监听套接字
Socket sscSocket;
//文件传送用发送用套接字
public int out_SendLocalPort=0;
public int out_Prepared=0;
//用以判断是否可以开始传送文件, 0为不可以,1为可以,2为结束
//当检测到该参数为1时,应该手工设置其值为2
private String in_FileName=null;
private int StartPos=0;
private int AcceptCode;
public NewUpload(String in_FileName,int StartPos,int AcceptCode)//上传文件名,文件开始位置,确认号码。
{
this.in_FileName=SOURCEDIRECTORY+in_FileName;
this.StartPos=StartPos;
this.AcceptCode=AcceptCode;
System.out.println("NewUpload取得的AcceptCode="+AcceptCode);
System.out.println("FState_Send.
AcceptCode[0]"+FState_Send.
AcceptCode[0]);
System.out.println("FState_Send.
CurrentSendFileLink[0]"+FState_Send.
CurrentSendFileLink[0]);
}
public void Send_Process()//执行该方法进行文件传送
{
int startpos=0;
int OneSegLen;
OneSegLen=2028;//每一段流包含有效的文件长度
RandomAccessFile fis=null;
try
{
BufferedOutputStream out=new BufferedOutputStream(sscSocket.getOutputStream(),OneSegLen+20);
//FileInputStream fis=new FileInputStream(in_FileName);
fis=new RandomAccessFile(in_FileName,"r");
//该段定义
int ch=0;
byte[] filereadstr=new byte[OneSegLen];
byte[] head=new byte[4];
//头文件定义
int length=0;
startpos=StartPos;
fis.seek(startpos);
boolean lastonce=false;
for(int i=0;;i++)
{
byte[] putstring=new byte[OneSegLen+20];
try
{
if (fis.length()-fis.getFilePointer()<=OneSegLen)
{
lastonce=true;
}
ch=fis.read(filereadstr);
}
catch(IOException e)
{
ErrorProcess();
System.out.println("10:"+e);
}
if (ch>0)
{
length=ch;
// System.out.println(startpos+" "+ch);
String tou=new String("sstt");
String wei=new String("send");
for (int j=0;j<4;j++)//装入程序标示
{
head[j]=tou.getBytes()[j];
putstring[j]=head[j];
//0-3写入"sstt"
}
//写如确认码
head[0]=(byte)(AcceptCode>>24);
head[1]=(byte)(AcceptCode>>16);
head[2]=(byte)(AcceptCode>>8);
head[3]=(byte)(AcceptCode);
for (int j=4;j<8;j++)//装入程序标示
{
putstring[j]=head[j-4];
//4-7写入确认码AcceptCode
}
if(startpos==0) //如果位第一次传送该文件,则把length设为文件总长度
{
int filelen=(int)fis.length();
head[0]=(byte)(filelen>>24);
head[1]=(byte)(filelen>>16);
head[2]=(byte)(filelen>>8);
head[3]=(byte)(filelen);
}
else
{
head[0]=(byte)(startpos>>24);
head[1]=(byte)(startpos>>16);
head[2]=(byte)(startpos>>8);
head[3]=(byte)(startpos);
}
for (int j=8;j<12;j++)
{
putstring[j]=head[j-8];
//8-11写入该段流读取文件的开始位置
}
head[0]=(byte)(length>>24);
head[1]=(byte)(length>>16);
head[2]=(byte)(length>>8);
head[3]=(byte)(length);
for (int j=12;j<16;j++)
{
putstring[j]=head[j-12];
//12-15流包含读取文件的长度
}
for (int j=16;j<ch+16;j++)//写入读取的文件流
{
putstring[j]=filereadstr[j-16];//16--(ch+16)文件正文
}
if (lastonce==true)//如果是最后一次读文件
{
System.out.println("读取最后一次");
wei="fien";
}
for (int j=ch+16;j<ch+20;j++)
{
head[j-ch-16]=wei.getBytes()[j-ch-16];
putstring[j]=head[j-ch-16];
//(OneSegLen+16)--结尾尾部标志当标志是"fien"标示文件结束
}
if (lastonce==false)
{
out.write(putstring);
}
else
//处理最后一次从文件中读数据
{
byte[] putstring2=new byte[ch+20];
for (int j=0;j<ch+20;j++)
{
putstring2[j]=putstring[j];
}
try
{
out.write(putstring2);
}
catch(IOException e)
{
if(fis!=null){fis.close();}
System.out.println("15:"+e+startpos+ch);
}
System.out.println("最或一次发送文件长度"+putstring2.length);
}
try
{
out.flush();
}
catch(IOException e)
{
System.out.println("16:"+e+startpos+ch);
}
startpos=startpos+ch;
}//end if
if (fis.length()-fis.getFilePointer()==0)
{
fis.close();
break;
}
}//end for
int filespos=FState_Send.
GetPosFromAccept(AcceptCode);
FState_Send.
CurrentSendFileLink[filespos]=false;
}
catch (IOException e)
{
System.out.println("11:"+e+startpos);
try
{
fis.close();
if (sscSocket!=null)
{
sscSocket.close();
int filespos=FState_Send.
GetPosFromAccept(AcceptCode);
if(filespos>0) FState_Send.
CurrentSendFileLink[filespos]=false;
// this.destroy();
}
}
catch (IOException e2)
{
int filespos=FState_Send.
GetPosFromAccept(AcceptCode);
if(filespos>0) FState_Send.
CurrentSendFileLink[filespos]=false;
System.out.println("12:"+e2);
// this.destroy();
}
}
finally
{
try
{
if (sscSocket!=null)
{
sscSocket.close();
if(fis!=null){fis.close();}
// this.destroy();
}
}
catch (IOException e)
{
System.out.println("13:"+e);
}
}//结束finally
}//结束sendprocess
public void run()
{
try
{
SendServerSocket=new ServerSocket(0,4);
out_SendLocalPort=SendServerSocket.getLocalPort();
out_Prepared=1;
System.out.println("开始监听:"+AcceptCode+" "+FState_Send.
GetPosFromAccept(AcceptCode));
System.out.println("端口:"+out_SendLocalPort);
sscSocket=SendServerSocket.accept();
Send_Process();
sscSocket.close();
}
catch(IOException e)
{
System.out.println("14:"+e);
ErrorProcess();
}
}//end run
}//////end class NewUpload
public boolean BytesEquals(byte[] bytes1,int st1,byte[] bytes2,int st2,int len)
{
for(int i=0;i<len;i++)
{
if(bytes1[i+st1]!=bytes2[i+st2])
{
return false;
}
}
return true;
}
public int ByteToInt(byte[] byte1,int st)
{
int result=0;
result+=byte1[0+st]*16777216;
for(int i=1;i<4;i++)
{
if (byte1[i+st]>=0)
{
result+=((int)(byte1[i+st]))*Math.pow(256,(3-i));
}
else
{
result+=(((int)(byte1[i+st]))&amp;((int)255))*Math.pow(256,(3-i));
}
}
return result;
}
}
 
what is the class you used in file: Init_Files
私は ここ フエルを 見ません   
 
接受答案了.
 
后退
顶部