📄 commsocket.java
字号:
/*
* Socket.java
*
* Created on 2003年3月10日, 下午2:32
*/
package collector.communication;
import java.io.*;
import java.net.*;
import javax.comm.*;
import collector.common.*;
/**
*
* @author WangJun
*/
public class CommSocket
extends Socket
implements IComm {
/** Creates a new instance of Socket */
private PortControl PortControln;
private short ControlPort;
private int m_PortNo = 0;
private String m_IpAddress = "";
public Socket m_Skt = null;
private InputStream m_InputStream = null;
private OutputStream m_OutputStream = null;
long m_LastInited = 0;
public CommSocket() {
}
public CommSocket(String m_IpAddress, int m_PortNo, int ControlPort) {
this.m_IpAddress = m_IpAddress;
this.m_PortNo = m_PortNo;
this.m_Skt = null;
this.ControlPort = (short) ControlPort;
}
public int initConnection(long t) {
/* if (t <= m_LastInited)
{
CollectorDefine.SystemPrintln("\ninitNet: already inited!\n");
return -1;
}
*/
return initConnection();
}
public boolean isAlive() {
boolean m_SuccessFlag = false;
try {
m_SuccessFlag = this.m_Skt.getKeepAlive();
}
catch (SocketException m_SocketException) {
CFunction.putHintString(1,
"isAlive in CommSocket Error #1" +
m_SocketException.getMessage());
return false;
}
return m_SuccessFlag;
}
public int closeConnection() {
if (this.m_Skt != null) {
try {
this.m_InputStream.close();
this.m_OutputStream.close();
this.m_Skt.close();
this.m_Skt = null;
// CFunction.putHintString ( 1 ,"关闭连接完毕 " + "IP地址:" + this.m_IpAddress + "端口号:" + this.m_PortNo);
return 1;
}
catch (IOException m_IOException) {
CFunction.putHintString(1,
" clearConn in CommSocket Error #1" + "IP地址:" +
this.m_IpAddress + "端口号:" + this.m_PortNo +
m_IOException.getMessage());
return -1;
}
}
return 1;
}
public int flushOutputStream() {
if (isConnectionOpen() == false) {
return -1;
}
try {
this.m_OutputStream.flush();
}
catch (IOException m_IOException) {
CFunction.putHintString(1,
" flushOutputStream in CommSocket Error #1" +
"IP地址:" +
this.m_IpAddress + "端口号:" + this.m_PortNo +
m_IOException.getMessage());
return -1;
}
return 1;
}
public int resetInputStream() {
if (isConnectionOpen() == false) {
return -1;
}
try {
this.m_InputStream.reset();
}
catch (IOException m_IOException) {
CFunction.putHintString(1,
" resetInputStream in CommSocket Error #1" +
"IP地址:" +
this.m_IpAddress + "端口号:" + this.m_PortNo +
m_IOException.getMessage());
return -1;
}
return 1;
}
public int initConnection() {
try {
this.m_Skt = null;
this.m_Skt = new Socket(this.m_IpAddress, this.m_PortNo);
if (this.m_Skt == null) {
CFunction.putHintString(1,
"initNet in CommSocket Error #1 " + "IP地址:" +
this.m_IpAddress +
"端口号:" + this.m_PortNo);
return -1;
}
this.m_InputStream = m_Skt.getInputStream();
this.m_OutputStream = m_Skt.getOutputStream();
}
catch (IOException e) {
CFunction.putHintString(1,
"initNet in CommSocket Error #2 " + "IP地址:" +
this.m_IpAddress +
"端口号:" + this.m_PortNo
+ e);
if (isConnectionOpen() == true) {
int m_SuccessFlag = closeConnection();
if (m_SuccessFlag < 0) {
CFunction.putHintString(1,
"initNet in CommSocket Error #3 " + "IP地址:" +
this.m_IpAddress + "端口号:" + this.m_PortNo);
}
}
return -1;
}
this.m_LastInited = System.currentTimeMillis();
// CFunction.putHintString (1," 建立连接完毕 " + "IP地址:" + this.m_IpAddress + "端口号:" + this.m_PortNo);
return 1;
}
public int sendBuffer(byte[] m_SendBuffer, int m_Length) {
try {
this.m_OutputStream.write(m_SendBuffer, 0, m_Length);
}
catch (IOException m_IoException) {
CFunction.putHintString(1,
"sendBuffer in CommSocket Error #1 " + "IP地址:" +
this.m_IpAddress +
"端口号:" + this.m_PortNo + m_IoException.getMessage());
// CFunction.putHintString(1, "Buffer Size =" + m_Length);
/* CFunction.putHintString(1,
CFunction.translateBytesToString(1, m_SendBuffer,
m_Length));
*/
return -1;
}
return 1;
}
public int sendBuffer(String m_String) {
try {
OutputStream outputStream = null;
outputStream = new ConvertedOutputStream(this.m_OutputStream);
outputStream.write(m_String.getBytes());
outputStream = null;
}
catch (IOException m_IoException) {
CFunction.putHintString(1,
"sendBuffer in CommSocket Error #1" + "IP地址:" +
this.m_IpAddress +
"端口号:" + this.m_PortNo + m_IoException.getMessage());
return -1;
}
return 1;
}
public int sendString(String m_String) {
try {
this.m_OutputStream.write(m_String.getBytes());
}
catch (IOException m_IOException) {
CFunction.putHintString(1,
"sendString in CommSocket Error #1 " + "IP地址:" +
this.m_IpAddress +
"端口号:" + this.m_PortNo + m_IOException.getMessage());
return -1;
}
return 1;
}
public String recStringBuffer(byte[] m_pRecBuffer, boolean m_lowercase,
int m_TimeCounter) {
int m_Count = 0;
int m_SuccessFlag = 1;
String m_receiveString = "";
try {
int m_RecAvailableNum = 0;
int bytes = 0;
while (m_SuccessFlag == 1) {
m_RecAvailableNum = m_InputStream.available();
if (m_RecAvailableNum > 0 &&
m_RecAvailableNum <= CollectorDefine.MAX_BYTE) {
//byte [] m_TempBuffer = new byte[m_RecAvailableNum];
//int bytes = m_InputStream.read(m_TempBuffer);
bytes = m_InputStream.read(m_pRecBuffer);
String m_String = new String(m_pRecBuffer, 0, bytes);
if (m_lowercase == true) {
m_receiveString = m_String.toLowerCase();
}
else {
m_receiveString = m_String;
}
break;
}
try {
Thread.sleep(CollectorDefine.COMM_SLEEP_TIME);
}
catch (InterruptedException interruptedexception) {
CFunction.putHintString(1,
"recStringBuffer in CommSocket Error #1 " +
"IP地址:" +
this.m_IpAddress + "端口号:" + this.m_PortNo);
return "";
}
m_Count++;
if (m_Count > m_TimeCounter) {
m_SuccessFlag = -1;
}
}
}
catch (IOException m_IoException) {
CFunction.putHintString(1,
"recStringBuffer in CommSocket Error #2" +
"IP地址:" +
this.m_IpAddress + "端口号:" + this.m_PortNo);
return "";
}
return m_receiveString;
}
public int recBuffer(byte[] m_pRecBuffer, int m_TimeCounter) {
int m_Count = 0;
int m_RecNum = -1;
try {
int m_RecAvailableNum = 0;
int bytes = 0;
while (true) {
m_RecAvailableNum = this.m_InputStream.available();
if (m_RecAvailableNum > 0 &&
m_RecAvailableNum <= CollectorDefine.MAX_BYTE) {
bytes = m_InputStream.read(m_pRecBuffer);
m_RecNum = bytes;
break;
}
try {
Thread.sleep(CollectorDefine.COMM_SLEEP_TIME);
}
catch (InterruptedException m_InterruptedException) {
CFunction.putHintString(1,
"recBuffer1 in CommSocket Error #1 " +
"IP地址:" +
this.m_IpAddress + "端口号:" + this.m_PortNo);
return -1;
}
m_Count++;
if (m_Count > m_TimeCounter) {
break;
}
}
}
catch (IOException m_IoException) {
CFunction.putHintString(1,
"recBuffer1 in CommSocket Error #2 " + "IP地址:" +
this.m_IpAddress +
"端口号:" + this.m_PortNo);
return -1;
}
return m_RecNum;
}
public int recBuffer(byte[] m_pRecBuffer) {
int m_Count = 0;
int m_RecNum = -1;
int m_TimeCounter = 1;
try {
int m_RecAvailableNum = 0;
int bytes = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -