我在WinCE 6.0设备上运行了2个应用程序并且都是在Visual C#上编写的,我需要在2个应用程序之间传递信息并在内部执行一些操作,因此我在应用程序上创建了2个服务器和2个客户端服务器在端口1287上运行,在端口10001上的应用程序B上运行,问题是应用程序A的客户端正在丢失连接,但是在应用程序B上的服务器日志上发送了正确的响应,我不断得到一个IOException,我无法找到原因,但是当我调试时,一步一步连接工作正常并且应用程序一起工作正常,但如果我让他们自己运行我继续获取我为这种情况创建的错误屏幕,这是我在申请A上得到的信息:
DEBUG || TcpWrapperServer || Socket closed...
DEBUG || TcpClient || Enviando solicitud(110.152.100.117:10001):{WEBCLOCK|SHIFT|0002149}
DEBUG || TcpClient || try/catch
DEBUG || TcpClient || Connecting... 110.152.100.117:10001
DEBUG || TcpClient || getStream()...
DEBUG || TcpClient || preparing to read stream...
DEBUG || TcpClient ||
WARNING data not available...
ERROR || TcpClient || IOException: Unable to read data from the transport connection.
ERROR || TcpClient || IOException(InnerExc): System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.ReceiveNoCheck(Byte[] buffer, Int32 index, Int32 request, SocketFlags socketFlags)
ERROR || TcpClient || IOException(traace): at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.IO.StreamReader.ReadBuffer(Char[] userBuffer, Int32 userOffset, Int32 desiredChars, Boolean& readToUserBuffer) at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)
at pvc_20.pvcTcpClient.sendRequest(String msg) at pvc_20.BarcodeSession.proccessPartnerID(String partnerid)
这是应用B上的登录服务器:
INFO || TcpServerWrapper || ProcessClient ||
DEBUG ||TcpWrapperServer || processCommand || msg:{WEBCLOCK|SHIFT|0001371}
DEBUG || TcpWrapperServer || processCommand || response:{WEBCLOCK|SHIFT|ONPROCESS}
这也是我调试时得到的日志:
DEBUG || TcpClient || Enviando
solicitud(110.152.100.117:10001):{WEBCLOCK|SHIFT|0001371}
DEBUG || TcpClient || try/catch 1/1/2000
DEBUG || TcpClient || Connecting...110.152.100.117:10001
DEBUG || TcpClient || getStream()...
DEBUG || TcpClient || reading stream...
DEBUG || TcpClient || respuesta recibida:{WEBCLOCK|SHIFT|ONPROCESS}
这是应用程序A上的客户端部分:
public TcpClient() {
m_server = Settings.ServerIP;
m_tcpPort = Settings.TcpPort;
m_hasReponse = false;
m_response = String.Empty;
m_buffer = null;
}
public TcpClient( String server , int tcpPort)
{
m_server = server;
m_tcpPort = tcpPort;
m_hasReponse = false;
m_response = String.Empty;
m_buffer = null;
}
public void sendRequest(String msg)
{
Log.debug("TcpClient", "sending to (" + m_server + ":" + m_tcpPort+"):"+ msg);
string m_currentIP = Utils.LocalIPAddress();
m_clientSocket = null;
m_hasReponse = false;
m_response = String.Empty;
Log.debug("TcpClient", "try/catch");
try
{
m_clientSocket = new TcpClient();
Log.debug("TcpClient", "Connecting... "+m_server+":"+ m_tcpPort);
m_clientSocket.Connect(
new IPEndPoint(
IPAddress.Parse(m_server),
m_tcpPort) );
Log.debug("TcpClient", "getStream()...");
m_stream = m_clientSocket.GetStream();
byte[] outstream = System.Text.Encoding.ASCII.GetBytes(msg);
m_stream.Write(outstream, 0, outstream.Length);
m_stream.Flush();
StreamReader streamReader = new StreamReader(m_stream);
if (m_stream.CanRead)
{
Log.debug("TcpClient", "preparing to read stream...");
StringBuilder res = new StringBuilder();
byte[] inStream = new byte[1024];
int bytesread = 0;
int index = 0;
char[] bfrRes = new char[1024];
if (!m_stream.DataAvailable)
{
Log.debug("TcpClient", "warning data not available...");
}
/* while (bytesread != 125)
{
bytesread = m_stream.ReadByte();
Log.debug("TcpClient", "reading byte: " + bytesread);
char c;
try
{
c = Convert.ToChar(bytesread); }
catch
{
c = ' ';
}
res.Append(c);
index++;
}*/
streamReader.Read(bfrRes, 0, 1024);
// m_response = res.ToString();
m_response = new string(bfrRes);
Log.debug("TcpClient", "response:" + m_response);
}
if (m_response.Length > 0)
m_hasReponse = true;
else
m_hasReponse = false;
}
catch (SocketException e)
{
Log.error("TcpClient", "SocketException: "+e.Message);
m_hasReponse= false;
m_response = String.Empty;
}catch(WebException we){
Log.error("TcpClient", "Webxception: " + we.Message);
Log.error("TcpClient", "WebException(InnerExc): " + we.InnerException);
Log.error("TcpClient", "WebException(traace): " + we.StackTrace);
m_hasReponse = false;
m_response = String.Empty;
}
catch (IOException ioe)
{
Log.error("TcpClient", "IOException: " + ioe.Message);
Log.error("TcpClient", "IOException(InnerExc): " + ioe.InnerException);
Log.error("TcpClient", "IOException(trace): " + ioe.StackTrace);
m_hasReponse = false;
m_response = String.Empty;
}
catch (Exception e)
{
Log.error("TcpClient","Exception:"+ e.Message);
m_hasReponse = false;
m_response = String.Empty;
}
finally
{
if (null != m_stream)
m_stream.Close();
m_clientSocket.Close();
}
return ;
}
public String response
{
get { return m_response; }
}
public bool hasResponse {
get { return m_hasReponse; }
}
public byte[] buffer {
get { return m_buffer; }
}
这是应用程序B上的服务器包装器:
class TcpServerWrapper
{
TcpClient m_client;
WebClock m_main;
bool isValidCommand = false;
public TcpServerWrapper(TcpClient client, WebClock m)
{
Log.debug("TcpServerWrapper", "init");
m_client = client;
m_main = m;
}
public void ProcessClient()
{
String remoteIP = ((IPEndPoint)m_client.Client.RemoteEndPoint).Address.ToString();
Log.info("TcpServerWrapper", " ProcessClient || Processing from:" + remoteIP);
NetworkStream clientStream = m_client.GetStream();
byte[] message = new byte[2048];
int bytesRead;
bytesRead = 0;
try
{
//blocks until a client sends a message
int i = 0;
int b = 0;
while (b != 125)
{
b = clientStream.ReadByte();
message[i] = (byte)b;
i++;
}
bytesRead = message.Length;
}
catch (Exception e)
{
//a socket error has occured
Log.error("TcpWrapperServer", "ProcessClient || Can't read client petition || ex:" + e.Message);
bytesRead = 0;
//break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
Log.warning("TcpWrapperServer", "ProcessClient || Client disconected ");
//break;
}
else
{
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
String req = encoder.GetString(message, 0, bytesRead);
String res = processCommand(req);
NetworkStream stream = m_client.GetStream();
byte[] buffer = encoder.GetBytes(res);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
clientStream.Close();
m_client.Close();
if (isValidCommand)
{
m_main.wakeApp("");
}
Log.debug("TcpWrapperServer", "ProcessClient || Socket closed");
}
private String processCommand(String msg)
{
Log.debug("TcpWrapperServer", "processCommand || msg:" + msg);
String response = "";
msg = msg.Replace(" ", String.Empty);
msg = msg.ToUpper();
if (!msg.StartsWith("{") || !msg.EndsWith("}"))
{
return "{WEBCLOCK| RESULT=ERROR|MESSAGE=El mensaje no es un comando valido de la aplicacion.}";
}
msg = msg.Replace("{", "");
msg = msg.Replace("}", "");
String[] commands = msg.Split('|');
//{WEBCLOCK|SHIFT|0001371}
if (commands[0] == "WEBCLOCK")
{
if (commands[1] == "SHIFT")
{
string socioid = commands[2];
if (socioid.Length == 7)
{
Settings.TMP_ScannedPartnerId = socioid;
isValidCommand = true;
response = "{WEBCLOCK|SHIFT|ONPROCESS}\n";
}
else
{
response = "{WEBCLOCK| RESULT=ERROR|MESSAGE=El mensaje no es un comando valido de la aplicacion.}";
}
}
else {
response = "{WEBCLOCK| RESULT=ERROR|MESSAGE=El mensaje no es un comando valido de la aplicacion.}";
}
}else
{
response = "{WEBCLOCK| RESULT=ERROR|MESSAGE=El mensaje no es un comando valido para WEBCLOCK}";
}
Log.debug("TcpWrapperServer", "processCommand || response:" + response);
return response;
}
}
我真的不明白为什么应用程序B上的本地服务器正在关闭。