Hi I am fairly new to C#. I am a java programmer that needed to program a certain application in C#. The hardware device i needed to program only had .Net support. I needed to create a socket server so i can comunicate with the device by sending in requests from my java application. My questions lie in thread safety. I am dealing with an embeded ocx or active x. When the request comes in and the thread is spawned do i have to do any kind ov invoking on the ocx control? or is that a thread safe object? I have posted my code of the server and then 2 methods from the other class that has the code dealing with the device control. does the axCoFPESO1 object have to be invoked or is the way i am handling it fine?
//Start Socket Server
public void startServer()
{
try
{
int port = Convert.ToInt32(portadd);
this.tcpListener = new TcpListener(IPAddress.Any, port);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
catch (SocketException se)
{
}
}
//Listen for inbound connnections
private void ListenForClients()
{
try
{
int count = 0;
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
sCclient = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
if (clientThread != null)
{
try
{
Console.WriteLine("Aborting " + clientThread.Name);
clientThread.Abort();
}
catch (Exception e)
{
e.GetBaseException();
}
}
++count;
clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Name = "Client Thread# " + count;
clientThread.Start(client);
}
}
catch (SocketException se)
{
}
}
//Conection is made handle request
private void HandleClientComm(object client)
{
try
{
tcpClient = (TcpClient)sCclient;
if (tcpClient != null)
{
clientStream = tcpClient.GetStream();
}
string request = "";
byte[] message = new byte[4096];
int bytesRead;
bool go = true;
while (go)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (SocketException se)
{
break;
}
if (bytesRead == 0)
{
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(
encoder.GetString(message, 0, bytesRead));
request = encoder.GetString(message, 0, bytesRead);
if (!DeviceName.Equals("VERIFONE SC5000"))
{
communicateToDevice(DeviceName, request);
}
else
{
sc5000Data = sc5000.ProcessData(request);
}
if (logicalName.Equals("MSR") || logicalName.Equals("SIGCAP") || logicalName.Equals("PINPAD"))
{
Thread current = Thread.CurrentThread;
Console.WriteLine(current.Name + " Waiting for Response");
while (!SymbolDevice.receivedData)
{
Thread.Sleep(200);
}
Console.WriteLine(current.Name + " Response Received");
returnResponse();
}
else
{
returnResponse();
}
go = false;
}
}
catch (Exception e)
{
}
}
//Send response bact to the client
public void returnResponse()
{
String response = "";
if (DeviceName.Equals("SYMBOL L4250"))
{
if (logicalName.Equals("MSR"))
{
String xml = "";
xml = sym.getXMLRequest();
xml = Header + xml + Trailer;
response = xml;
}
if (logicalName.Equals("PINPAD"))
{
String xml = "";
xml = sym.getXMLRequest();
xml = Header + xml + Trailer;
response = xml;
}
if (logicalName.Equals("SIGCAP") || logicalName.Equals("DISPLAY"))
{
response = "Done";
}
if (DeviceName.Equals("VERIFONE SC5000"))
{
response = sc5000Data;
}
}
NetworkStream clientStream = tcpClient.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = null;
buffer = encoder.GetBytes(response + "?" + "\n");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
sc5000Data = "";
tcpClient.Close();
}
public void startMSR()
{
try
{
string req = "V\\FNSWIPEFRM\\TK4\\TS";
axCoFPESO1.SendData(req, 1, 1, 1);
axCoFPESO1.SendData("V\\FE\\VNswipestatus", 1, 1, 1);
receivedData = false;
}
catch (Exception e1)
{
Console.WriteLine(e1.GetBaseException());
}
}
private void ReceivedTrack_DataEvent(object sender, AxFPEINTERFACELib.IFPESOlementationEvents_ReceivedTrackDataEventEvent e)
{
string track1 = "";
string track2 = "";
string exp = "";
string name = "";
axCoFPESO1.SendData("V\\FNWAITFRM", 1, 1, 1);
string data1 = (string)e.varTrack1Data;
data1data1 = data1.Substring(1);
track1 = data1.Substring(0, data1.IndexOf("^"));
name = data1.Substring(data1.IndexOf("^") + 1);
namename = name.Substring(0, name.IndexOf("^"));
exp = data1.Substring(data1.LastIndexOf("^") + 1, 4);
track2 = (string)e.varTrack2Data;
xmlRequest = "";
setINXML(ACCT_NUM, track1);
setINXML(EXP_DATE, exp);
setINXML(CARDHOLDER, name);
setINXML(TRACK_DATA, track2);
Console.WriteLine(track1);
Console.WriteLine(name);
Console.WriteLine(exp);
Console.WriteLine(track2);
receivedData = true;
}