📄 cserverthread.java
字号:
import java.io.*;
import java.net.*;
/**
* Transact peer to peer communication.<p>
* 2005.8.30
* @version 0.1.2
* @author Administrator
*
*/
public class CserverThread extends Thread{
/**
* If it is true this thread will stop.
*/
boolean stop_sSocket=false;
/**
* Point to the socket which is created by the client's CmainFrame_ServerThread.
* The socket will transact the communication between the client and the connected peer.
*/
Socket cs;
/**
* Contain the message sent by the connected peer.
*/
String cs_in=null;
/**
* Point to the CmainFrame's csf. When this thread is initialized it will tell
* the csf to add the connected peer to its list.
*/
CserverFrame csf;
/**
* When the peer want to send a XML IDMEF file,this window will be open.
*/
CreceiveIDMEF ccrf;
/**
* The connected peer's username.
*/
String conningname=null;
/**
* Point to cs's DataInputStream
*/
DataInputStream cs_dis=null;
/**
* Point to cs's DataOutputStream
*/
DataOutputStream cs_dos=null;
/**
* Use DOM parse XML message which send from the peer.
*/
ClientXMLParser cxp=new ClientXMLParser();
/**
* The name of the file which will be send by the peer.
*/
String filename_s=null;
/**
* The stream to write file.
*/
FileOutputStream file_out;
/**
* When one peer want to send file to this client, the client will get an unused port
* to listen to the peer's connect request.
*/
ServerSocket file_ss=null;
/**
* When one peer get the open port number, it will connect the client ,if the file_ss
* accept() the file_soc will get the socket.
*/
Socket file_soc=null;
/**
* Whether get an unused port.
*/
boolean fileport_b=false;
/**
* Try from 65535 to lower until find an unused port.
*/
int fileport_num=65535;
/**
* The length of the file.
*/
long filesize=0;
/**
* How many bytes of the file have been received.
*/
long receivedsize=0;
/**
*
* @param s The socket which is created by the client's CmainFrame_ServerThread.
* @param sf CmainFrame's csf.
*/
CserverThread(Socket s, CserverFrame sf)
{
cs=s;
csf=sf;
try
{
cs_dis=new DataInputStream(cs.getInputStream());
cs_dos=new DataOutputStream(cs.getOutputStream());
ccrf=new CreceiveIDMEF(this);
}
catch(IOException e)
{
try
{
cs.close();
}
catch(IOException ee)
{
}
}
//System.out.println("sserverThread constructor is ok");
}
/**
* After the thread start,the client continue to read the peer's sended message.
*/
public void run()
{
while(!stop_sSocket)
{
try
{
cs_in=cs_dis.readUTF();
//System.out.println("before parse:"+cs_in);
s_parser();
}
catch(IOException e)
{
System.out.println("error:"+e);
if(conningname!=null)
{
int n=csf.l_conning.getItemCount();
for(int j=0;j<n;j++)
{
if(csf.l_conning.getItem(j).equals(conningname))
{
csf.l_conning.remove(j);
csf.l_serverThread.remove(j);
break;
}
}
}
break;
}
}
}
/**
* Call ClientXMLParser to parse the peer's XML message can perform the action.
*
*/
public void s_parser()
{
cxp.in=new StringBufferInputStream(cs_in);
cxp.parser();
if(cxp.sendmyname_b)
{
cxp.sendmyname_b=false;
conningname=cxp.sendmyname_s;
csf.l_conning.add(conningname);
csf.l_serverThread.add(this);
}
if(cxp.sendfile_b)
{
cxp.sendfile_b=false;
ccrf.ta_msg.setText("");
//ccrf.tf_size.setText("");
ccrf.p_bar.setValue(0);
//ccrf.b_cancel.setEnabled(true);
ccrf.b_no.setEnabled(true);
ccrf.b_yes.setEnabled(true);
ccrf.receiving_i=0;
filesize=cxp.filesize_l;
ccrf.ta_msg.append("User: "+cxp.peername_s+" want to send IDMEF file. \n");
ccrf.ta_msg.append("File name is: "+cxp.sendfile_s+",size is: "+cxp.filesize_l+". \n");
filename_s=cxp.sendfile_s;
getport();
ccrf.setVisible(true);
}
if(cxp.sendbe_b)
{
cxp.sendbe_b=false;
//System.out.println("senddb:"+cxp.sendbe_i);
if(cxp.sendbe_i==1)
{
receivedsize=0;
ccrf.ta_msg.append("Receive begin \n");
write_file();
}
if(cxp.sendbe_i==0)
{
fileport_b=false;
receivedsize=0;
ccrf.ta_msg.append("Send stopped \n");
ccrf.b_cancel.setEnabled(false);
ccrf.receiving_i=3;
}
if(cxp.sendbe_i==2)
{
receivedsize=0;
StringBuffer c_sb1=new StringBuffer();
c_sb1.append("<msg><ack_file><num>"+0+"</num></ack_file></msg>");
//System.out.println(c_sb1.toString());
try
{
cs_dos.writeUTF(c_sb1.toString());
}
catch(IOException ee)
{
//System.out.println(ee);
}
fileport_b=false;
try
{
if(file_ss!=null)
{
file_ss.close();
}
}
catch(IOException e1)
{
}
receivedsize=0;
try
{
if(file_soc!=null)
{
file_soc.close();
}
}
catch(IOException e1)
{
}
ccrf.ta_msg.append("Receive over \n");
ccrf.b_cancel.setEnabled(false);
ccrf.receiving_i=2;
}
}
}
/**
* Close the CserverThread thread.
*
*/
public void CserverThreadclose()
{
stop_sSocket=true;
try
{
cs.close();
}
catch(IOException e)
{
}
}
/**
* Start thread CfilesendThread2 to find an unused port.
*
*/
public void getport()
{
while(!fileport_b)
{
try
{
file_ss=new ServerSocket(fileport_num);
fileport_b=true;
}
catch(IOException e)
{
fileport_num--;
}
}
new CfilesendThread2(this).start();
}
/**
* Start thread CfilesendThread1 to continue to receive the file.
*
*/
public void write_file()
{
if(file_soc!=null)
{
//System.out.println("start read");
new CfilesendThread1(this).start();
}
}
}
/**
* Find an unused port.<p>
* 2005.9.5
* @version 0.1.2
* @author Daxin Tian
*
*/
class CfilesendThread2 extends Thread
{
/**
* Point the CserverThread which call this thread. If find an unused port then
* tell to cst.
*/
CserverThread c_cst;
CfilesendThread2(CserverThread cst)
{
c_cst=cst;
}
public void run()
{
while(c_cst.fileport_b)
{
try
{
c_cst.file_soc=c_cst.file_ss.accept();
}
catch(IOException e)
{
break;
}
if(c_cst.file_soc!=null)
{
break;
}
}
}
}
/**
* Continue to receive the file.<p>
* 2005.9.5
* @version 0.1.2
* @author Daxin Tian
*
*/
class CfilesendThread1 extends Thread
{
/**
* Point to the CserverThread which call this thread.
*/
CserverThread c_cst;
/**
* How many bytes read one time.
*/
int wi;
/**
* Once received content will be read into it.
*/
byte wb[]=new byte[1400];
/**
* Peer to Peer 's stream used to transimit XML IDMEF file
*/
DataInputStream f_dis=null;
/**
* Peer to Peer 's stream used to transimit XML IDMEF file
*/
DataOutputStream f_dos=null;
CfilesendThread1(CserverThread cst)
{
this.setPriority(Thread.MIN_PRIORITY);
c_cst=cst;
try
{
c_cst.file_out=new FileOutputStream(c_cst.filename_s);
}
catch(IOException e)
{
}
}
public void run()
{
try
{
f_dis=new DataInputStream(c_cst.file_soc.getInputStream());
f_dos=new DataOutputStream(c_cst.file_soc.getOutputStream());
}
catch(IOException e)
{
}
StringBuffer c_sb1=new StringBuffer();
c_sb1.append("<msg><ack_file><num>"+ -3 +"</num></ack_file></msg>");
//System.out.println(c_sb1.toString());
try
{
c_cst.cs_dos.writeUTF(c_sb1.toString());
}
catch(IOException ee)
{
//System.out.println(ee);
}
c_cst.ccrf.receiving_i=1;
while(true)
{
try
{
wi=f_dis.read(wb);
if(wi!=-1)
{
c_cst.file_out.write(wb,0,wi);
c_cst.receivedsize=c_cst.receivedsize+wi;
//c_cst.ccrf.tf_size.setText(String.valueOf(c_cst.receivedsize));
c_cst.ccrf.p_bar.setValue((int)(100*c_cst.receivedsize/c_cst.filesize));
}
else
{
break;
}
}
catch(IOException e)
{
break;
}
}
try
{
c_cst.file_out.close();
}
catch(IOException e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -