TCP IP Listener Problem Received data form client and store in SQL database

  • Thread starter Thread starter Skhan07
  • Start date Start date
S

Skhan07

Guest
hi i am new in filed of programming .

I am create a TCPIP Listener to received a data from Medical Device and store in text file . for this purpose create TCP IP Lister Listener run successfully and devise connect successfully with my server .


As per document of devise .

When we try to send data from medical device to server .

medical devise send : ENQ

Server Reply : ACK

Then Medical Devise next frame to server look like this : H|\^&|||00-99100^00-00^^^^medical-100 01^12345678||||||||E1394-97

Server received this frame and safe it and send ACK again to client for next frame .

Client Send Next Frame . O|1||^^ 12RT5ABCDE^B|^^^^WdC\^^^^RdC\^^^^HwB\ ^^^^HuT\ ^^^^MTV\^^^^MCH\^^^^MCHC\^^^^PLT\^^^^W-SCR\ ^^^^W-MCR\^^^^W-LCR\^^^^W-SCC\^^^^W-UNC\^^^^W\ ^^^^RFW\^^^^RYW-CV\^^^^PFW\^^^^MYV\^^^^Q-LCR |||||||N||||||||||||||F

<cr>Server send again ACK to Client till Client send EOT when server received EOT from Client save data in text file and clear the memory </cr>

<cr>and server ready for next Record </cr>


namespace ServerOK
{

class Program
{
static void Main(string[] args)
{
try
{
string HostName = Dns.GetHostName();

// This line is obsolet
String MyIP = Dns.GetHostByName(HostName).AddressList[0].ToString();
IPAddress IPAd = IPAddress.Parse(MyIP);


IPAddress ipAd = IPAddress.Parse(MyIP); //use local m/c IP address, and use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
while (true)
{
Socket s = myList.AcceptSocket(); // This line is blocking
Task connectionTask = new Task(() =>
{
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(""));
//=========================

string path = @"D:\MyTest.txt";

// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string createText = k + Environment.NewLine;
File.WriteAllText(path, createText);
}

// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = k + Environment.NewLine;
File.AppendAllText(path, appendText);



//=========================




Console.WriteLine("\n ACK");

/* clean up */
s.Close();
});
connectionTask.Start();
}
myList.Stop(); // You will never reach this line
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}



}

}
}


<cr>Kindly advise is what changes should be required in my server code .</cr>

Continue reading...
 
Back
Top