我正在从类下面创建以下SocketClient实例,并从多线程环境调用如下所示。请让我知道以下代码是否是线程安全的。因为我看到反应混乱了。一些SocketClient收到了需要由另一个客户端接收的响应。
//从多线程环境调用。
SocketClient client=new SocketClient();
client.MakeRequest()
===============================================
public class SocketClient()
{
byte[] buffer;
public string MakeRequest()
{
string _response = string.Empty;
string _xml = string.Empty;
Socket _sender = null;
int _connectCount = 0;
try
{
/////////////////////////////Trying to connect to remote host
while (_connectCount <= 5)
{
_connectCount++;
try
{
if (_sender == null)
{
_sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_sender.ReceiveTimeout = ReceiveTimeout;
_sender.SendTimeout = SendTimeout;
_sender.Connect(RemoteEP);
}
}
catch (Exception ex)
{
if (_sender != null)
{
_sender.Shutdown(SocketShutdown.Both);
_sender.Close();
}
}
if (_sender != null)
{
if (_sender.Connected || _connectCount > 5)
{
break;
}
_sender.Shutdown(SocketShutdown.Both);
_sender.Close();
}
}
///////////////////////////Check connectivity brefore proceed futher
if (!_sender.Connected)
{
return _response;
}
else
{
NetLog.Write("MakeSyncRequest","L-E-P:"+_sender.LocalEndPoint.ToString()+":R-E-P:"+_sender.RemoteEndPoint.ToString()+ " Connected: count"+ _connectCount);
}
////////////////////////////Sending data to remote host
try
{
int bytesSent = 0;
bool _isHeaderRead = false;
int _bytesRec = 0;
_xml = getMessageHeader(xmlStr.Length) + "" + xmlStr;
object obydata = (Object)_xml;
Encoding oEncoding = System.Text.Encoding.GetEncoding(1252);
byte[] byData = oEncoding.GetBytes(obydata.ToString());
//check for 0 based Quotient and Remainder
if (Quotient == 0) byData[0] = 0;
if (Remainder == 0) byData[1] = 0;
if (_sender.Connected)
{
NetLog.Write("MakeSyncRequest", "Connected:");
bytesSent = _sender.Send(byData);
}
else
{
NetLog.Write("MakeSyncRequest" + "::Thread Id :" + Thread.CurrentThread.ManagedThreadId, "Not Connected:");
}
//////////////////////////Start receiving data from remote host
do
{
buffer = new byte[BufferSize];
NetLog.Write("MakeSyncRequest", "byteSent:" + bytesSent);
if (_sender.Connected)
{
_bytesRec = _sender.Receive(buffer);
TotbyteReceived = TotbyteReceived + _bytesRec;
}
else
{
NetLog.Write("MakeSyncRequest"+"::Thread Id :"+Thread.CurrentThread.ManagedThreadId, "Receiving data , Not conneted:");
break;
}
NetLog.Write("MakeSyncRequest" + "::Thread Id :" + Thread.CurrentThread.ManagedThreadId, "bytesRec:" + _bytesRec + ",TotbyteReceived:" + TotbyteReceived);
Array.Resize<byte>(ref buffer, _bytesRec);
if (_bytesRec > 0)
{
if (!_isHeaderRead)
{
_isHeaderRead = true;
TotResponseByteSize = GetResponseHeaderByteSize(buffer);
RsvbufferStartIndex = 2; // read except header
}
else
{
RsvbufferStartIndex = 0;
}
NetLog.Write("MakeSyncRequest" + "::Thread Id :" + Thread.CurrentThread.ManagedThreadId, "RsvbufferStartIndex:" + RsvbufferStartIndex);
_response = _response + Encoding.ASCII.GetString(buffer, RsvbufferStartIndex, _bytesRec - RsvbufferStartIndex);
Array.Clear(buffer, 0, buffer.Length);
NetLog.Write("MakeSyncRequest" + "::Thread Id :" + Thread.CurrentThread.ManagedThreadId, "Txn ID " + txnId + " responseg..data :" + _response);
}
else
{
NetLog.Write("MakeSyncRequest", "No data received exiting..:");
break;
}
}while (TotbyteReceived < TotResponseByteSize);
}
catch (Exception e)
{
NetLog.Write("MakeSyncRequest", "Exception :" + e.ToString());
}
}
catch (Exception e)
{
NetLog.Write("MakeSyncRequest", "Exception :" + e.ToString());
}
finally
{
if (_sender != null)
{
NetLog.Write("MakeSyncRequest", "Finally closing socket.");
_sender.Shutdown(SocketShutdown.Both);
_sender.Close();
}
else
{
NetLog.Write("MakeSyncRequest", "Finally socket is null :");
}
}
return _response;
}
}