Initial
This commit is contained in:
202
test/SocketControl.java
Normal file
202
test/SocketControl.java
Normal file
@@ -0,0 +1,202 @@
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class SocketControl extends Socket
|
||||
{
|
||||
public final int OkResult=220;
|
||||
public final int ErrorResult=500;
|
||||
private InputStream mInputStream=null;
|
||||
private OutputStream mOutputStream=null;
|
||||
private boolean mIsConnected=false;
|
||||
|
||||
public SocketControl()
|
||||
{
|
||||
}
|
||||
public SocketControl(String strHost,int port)throws IOException
|
||||
{
|
||||
super(strHost,port);
|
||||
// try{mInputStream=getInputStream();}
|
||||
// catch(IOException exception){close();return;}
|
||||
// try{mOutputStream=getOutputStream();}
|
||||
// catch(IOException exception){close();return;}
|
||||
isConnected(true);
|
||||
}
|
||||
public SocketControl(InetAddress inetAddress,int port)throws IOException
|
||||
{
|
||||
super(inetAddress,port);
|
||||
try{mInputStream=getInputStream();}
|
||||
catch(IOException exception){close();return;}
|
||||
try{mOutputStream=getOutputStream();}
|
||||
catch(IOException exception){close();return;}
|
||||
isConnected(true);
|
||||
}
|
||||
public void closeSocket()throws IOException
|
||||
{
|
||||
isConnected(false);
|
||||
close();
|
||||
}
|
||||
public boolean isConnected()
|
||||
{
|
||||
return mIsConnected;
|
||||
}
|
||||
private void isConnected(boolean isConnected)
|
||||
{
|
||||
mIsConnected=isConnected;
|
||||
}
|
||||
public String readLine()
|
||||
{
|
||||
byte streamByte[];
|
||||
String strLine=new String();
|
||||
|
||||
if(!isConnected())return strLine;
|
||||
strLine=new String();
|
||||
while(true)
|
||||
{
|
||||
streamByte=new byte[1];
|
||||
try{mInputStream.read(streamByte);}
|
||||
catch(IOException exception){break;}
|
||||
if(13==streamByte[0])continue;
|
||||
else if(10==streamByte[0])break;
|
||||
strLine+=new String(streamByte);
|
||||
}
|
||||
return strLine;
|
||||
}
|
||||
public boolean writeLine(String strLine)
|
||||
{
|
||||
if(!isConnected()||0==strLine.length())return false;
|
||||
strLine+=new String("\r\n");
|
||||
byte streamData[]=strLine.getBytes();
|
||||
try{mOutputStream.write(streamData);}
|
||||
catch(IOException exception){return false;}
|
||||
return true;
|
||||
}
|
||||
public int getResultCode(String strLine)
|
||||
{
|
||||
if(0==strLine.length())return 0;
|
||||
String strResult=new String();
|
||||
int strIndex=0;
|
||||
while(true)
|
||||
{
|
||||
if(strIndex>=3)break;
|
||||
if(' '==strLine.charAt(strIndex))break;
|
||||
strResult+=strLine.charAt(strIndex);
|
||||
strIndex++;
|
||||
}
|
||||
if(0==strResult.length())return 0;
|
||||
return Integer.parseInt(strResult);
|
||||
}
|
||||
public String getResult(String strLine)
|
||||
{
|
||||
String strResult=new String();
|
||||
int strIndex=0;
|
||||
int strLength=strLine.length();
|
||||
if(0==strLength)return strResult;
|
||||
while(' '!=strLine.charAt(strIndex)&&strIndex<strLength)strIndex++;
|
||||
strIndex++;
|
||||
if(strIndex>=strLength)return strResult;
|
||||
while(strIndex<strLength)
|
||||
{
|
||||
strResult+=strLine.charAt(strIndex);
|
||||
strIndex++;
|
||||
}
|
||||
return strResult;
|
||||
}
|
||||
public boolean receive(StringArray receiveStrings,StringArray responseStrings)
|
||||
{
|
||||
boolean isInMultiLine=false;
|
||||
boolean returnCode=true;
|
||||
String seriesItem=new String();
|
||||
String stringData=new String();
|
||||
|
||||
receiveStrings.clear();
|
||||
if(!isConnected())return false;
|
||||
while(true)
|
||||
{
|
||||
stringData=readLine();
|
||||
if(0==stringData.length())break;
|
||||
if(0==receiveStrings.size())
|
||||
{
|
||||
seriesItem=stringData.substring(0,3);
|
||||
if(!isInResponse(seriesItem,responseStrings))
|
||||
{
|
||||
returnCode=false;
|
||||
receiveStrings.insert(stringData);
|
||||
break;
|
||||
}
|
||||
if('-'==stringData.charAt(3))isInMultiLine=true;
|
||||
}
|
||||
if(0!=receiveStrings.size()&&isInMultiLine&&'-'!=stringData.charAt(3)&&stringData.substring(0,3)==seriesItem)
|
||||
{
|
||||
receiveStrings.insert(stringData);
|
||||
break;
|
||||
}
|
||||
receiveStrings.insert(stringData);
|
||||
if(!isInMultiLine)break;
|
||||
}
|
||||
return returnCode;
|
||||
}
|
||||
public boolean receive(StringArray receiveStrings)
|
||||
{
|
||||
boolean isInMultiLine=false;
|
||||
boolean returnCode=false;
|
||||
String seriesItem=new String();
|
||||
String stringData=new String();
|
||||
|
||||
receiveStrings.clear();
|
||||
if(!isConnected())return false;
|
||||
while(true)
|
||||
{
|
||||
stringData=readLine();
|
||||
if(0==stringData.length())break;
|
||||
if(0==receiveStrings.size())
|
||||
{
|
||||
seriesItem=stringData.substring(0,3);
|
||||
if('-'==stringData.charAt(3))isInMultiLine=true;
|
||||
}
|
||||
if(0!=receiveStrings.size()&&isInMultiLine&&'-'!=stringData.charAt(3)&&stringData.substring(0,3).equals(seriesItem))
|
||||
{
|
||||
receiveStrings.insert(stringData);
|
||||
break;
|
||||
}
|
||||
receiveStrings.insert(stringData);
|
||||
if(!isInMultiLine)break;
|
||||
}
|
||||
if(0!=receiveStrings.size())return true;
|
||||
return false;
|
||||
}
|
||||
public boolean isInResponse(String responseString,StringArray responseStrings)
|
||||
{
|
||||
if(0==responseString.length())return false;
|
||||
for(int index=0;index<responseStrings.size();index++)
|
||||
{
|
||||
if(responseString.equals(responseStrings.getAt(index)))return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public boolean accept(int port)throws IOException
|
||||
{
|
||||
ServerSocketControl serverSocketControl=null;
|
||||
|
||||
try{serverSocketControl=new ServerSocketControl(port);}
|
||||
catch(IOException exception){return false;}
|
||||
try{serverSocketControl.accept(this);}
|
||||
catch(IOException exception){return false;}
|
||||
try{mInputStream=getInputStream();}
|
||||
catch(IOException exception)
|
||||
{
|
||||
try{closeSocket();}
|
||||
finally{return false;}
|
||||
}
|
||||
try{mOutputStream=getOutputStream();}
|
||||
catch(IOException exception)
|
||||
{
|
||||
try{closeSocket();}
|
||||
finally{return false;}
|
||||
}
|
||||
isConnected(true);
|
||||
return isConnected();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user