here i've an code for accepting the data from the server socket, the data is very realtime and it will save every data that come in,
here is my code,
please help me with the saving to database, so it can't cause cpu usage over 50%,
using System;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
using Npgsql;
using System.Text;
using System.Linq;
using System.Web;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using Program;
namespace program
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(IPAddress.Any,12001);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");
counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
handleClinet client = new handleClinet();
client.startClient(clientSocket, Convert.ToString(counter));
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
}
public class handleClinet
{
static int port = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings.Get("port"));
string dbserver = System.Configuration.ConfigurationSettings.AppSettings.Get("dbserver").ToString();
string dbuser = System.Configuration.ConfigurationSettings.AppSettings.Get("dbuser").ToString();
string dbpassword = System.Configuration.ConfigurationSettings.AppSettings.Get("dbpassword").ToString();
string dbdatabase = System.Configuration.ConfigurationSettings.AppSettings.Get("dbdatabase").ToString();
string destdirectory = System.Configuration.ConfigurationSettings.AppSettings.Get("pathEODDirectory").ToString();
TcpClient clientSocket;
string clNo;
public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[clientSocket.ReceiveBufferSize];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = 0;
NetworkStream ns = clientSocket.GetStream();
serverResponse = "Username : ";
sendBytes = Encoding.ASCII.GetBytes(serverResponse);
ns.Write(sendBytes, 0, sendBytes.Length);
System.IO.StreamReader streamReader1 = new System.IO.StreamReader(ns);
if (streamReader1.ReadLine().Equals("afat") == true)
{
if(streamReader1.ReadLine().Equals("worldwar") == true)
{
while ((true))
{
try
{
Thread.Sleep(10);
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
Console.WriteLine(">>" + streamReader.ReadLine());
LogFile log = new LogFile();
log.createLog(">> " + streamReader.ReadLine() + "\n");
string data = streamReader.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(" >> " + ex.ToString());
break;
}
}
}
}
}
private Thread startTheThread( string input )
{
var t = new Thread(() => doSave(input));
t.Start();
return t;
}
private void doSave(string input)
{
Thread.Sleep(5);
NpgsqlConnection conn = new NpgsqlConnection("Server=" + dbserver + ";Port=5432;User Id=" + dbuser + ";Password=" + dbpassword + ";Database=" + dbdatabase + "; Pooling=false;MinPoolSize=1;MaxPoolSize=20;ConnectionLifeTime=15;");
conn.Open();
string[] lines = input.Split(';');
if (lines[0] == "s" || lines[0] == "S")
{
NpgsqlCommand cmdPerTick = new NpgsqlCommand("insert into tbtstockrealtimepertick(cdstock,previouscloseprice,openprice,closeprice,highprice,lowprice,volume,value,freq,bestbid,bestbidvolume,bestoffer,bestoffervolume,lastupdate) values (@cdstock,@previouscloseprice,@openprice,@closeprice,@highprice,@lowprice,@volume,@valuetotal,@freq,@bestbid,@bestbidvolume,@bestoffer,@bestoffervolume,@lastupdate)", conn);
cmdPerTick.Parameters.AddWithValue("@cdstock", lines[1]);
cmdPerTick.Parameters.AddWithValue("@previouscloseprice", lines[2]);
cmdPerTick.Parameters.AddWithValue("@openprice", lines[3]);
cmdPerTick.Parameters.AddWithValue("@closeprice", lines[4]);
cmdPerTick.Parameters.AddWithValue("@highprice", lines[5]);
cmdPerTick.Parameters.AddWithValue("@lowprice", lines[6]);
cmdPerTick.Parameters.AddWithValue("@volume", lines[7]);
cmdPerTick.Parameters.AddWithValue("@valuetotal", lines[8]);
cmdPerTick.Parameters.AddWithValue("@freq", lines[9]);
cmdPerTick.Parameters.AddWithValue("@bestbid", lines[10]);
cmdPerTick.Parameters.AddWithValue("@bestbidvolume", lines[11]);
cmdPerTick.Parameters.AddWithValue("@bestoffer", lines[12]);
cmdPerTick.Parameters.AddWithValue("@bestoffervolume", lines[13]);
cmdPerTick.Parameters.AddWithValue("@lastupdate", lines[14]);
try
{
int rowsAffected = cmdPerTick.ExecuteNonQuery();
Console.WriteLine("It Insert {0} lines in table tbtstockrealtimepertick", rowsAffected);
}
catch (Exception ex)
{
}
NpgsqlCommand cmd = new NpgsqlCommand("update tbtstockrealtime set previouscloseprice = @previouscloseprice, openprice = @openprice, closeprice = @closeprice, highprice = @highprice, lowprice = @lowprice, volume = @volume, valuetotal = @valuetotal , freq = @freq , bestbid = @bestbid ,bestbidvolume = @bestbidvolume ,bestoffer = @bestoffer, bestoffervolume = @bestoffervolume, lastupdate = @lastupdate where cdstock = @cdstock", conn);
cmd.Parameters.AddWithValue("@previouscloseprice", lines[2]);
cmd.Parameters.AddWithValue("@openprice", lines[3]);
cmd.Parameters.AddWithValue("@closeprice", lines[4]);
cmd.Parameters.AddWithValue("@highprice", lines[5]);
cmd.Parameters.AddWithValue("@lowprice", lines[6]);
cmd.Parameters.AddWithValue("@volume", lines[7]);
cmd.Parameters.AddWithValue("@valuetotal", lines[8]);
cmd.Parameters.AddWithValue("@freq", lines[9]);
cmd.Parameters.AddWithValue("@bestbid", lines[10]);
cmd.Parameters.AddWithValue("@bestbidvolume", lines[11]);
cmd.Parameters.AddWithValue("@bestoffer", lines[12]);
cmd.Parameters.AddWithValue("@bestoffervolume", lines[13]);
cmd.Parameters.AddWithValue("@lastupdate", lines[14]);
cmd.Parameters.AddWithValue("@cdstock", lines[1]);
try
{
int rowsaffected = cmd.ExecuteNonQuery();
Console.WriteLine("It update {0} lines in table tbtstockrealtime", rowsaffected);
if (rowsaffected == 0)
{
NpgsqlCommand cmd1 = new NpgsqlCommand("insert into tbtstockrealtime(cdstock,previouscloseprice,openprice,closeprice,highprice,lowprice,volume,valuetotal,freq,bestbid,bestbidvolume,bestoffer,bestoffervolume,lastupdate) values (@cdstock,@previouscloseprice,@openprice,@closeprice,@highprice,@lowprice,@volume,@valuetotal,@freq,@bestbid,@bestbidvolume,@bestoffer,@bestoffervolume,@lastupdate)", conn);
cmd1.Parameters.AddWithValue("@cdstock", lines[1]);
cmd1.Parameters.AddWithValue("@previouscloseprice", lines[2]);
cmd1.Parameters.AddWithValue("@openprice", lines[3]);
cmd1.Parameters.AddWithValue("@closeprice", lines[4]);
cmd1.Parameters.AddWithValue("@highprice", lines[5]);
cmd1.Parameters.AddWithValue("@lowprice", lines[6]);
cmd1.Parameters.AddWithValue("@volume", lines[7]);
cmd1.Parameters.AddWithValue("@valuetotal", lines[8]);
cmd1.Parameters.AddWithValue("@freq", lines[9]);
cmd1.Parameters.AddWithValue("@bestbid", lines[10]);
cmd1.Parameters.AddWithValue("@bestbidvolume", lines[11]);
cmd1.Parameters.AddWithValue("@bestoffer", lines[12]);
cmd1.Parameters.AddWithValue("@bestoffervolume", lines[13]);
cmd1.Parameters.AddWithValue("@lastupdate", lines[14]);
try
{
int rowsAffected = cmd1.ExecuteNonQuery();
Console.WriteLine("It Insert {0} lines in table tbtstockrealtime", rowsAffected);
}
catch (Exception ex)
{
}
}
}
catch (Exception e)
{
Console.Write("Error : " + e.Message + "\n");
}
}
else if (lines[0] == "i" || lines[0] == "I")
{
NpgsqlCommand cmdPertick = new NpgsqlCommand("insert into tbtindexrealtime(cdstock,previouscloseprice,openprice,closeprice,highprice,lowprice,volume,valuetotal,freq,lastupdate) values (@cdstock,@previouscloseprice,@openprice,@closeprice,@highprice,@lowprice,@volume,@valuetotal,@freq,@lastupdate)", conn);
cmdPertick.Parameters.AddWithValue("@cdstock", lines[1]);
cmdPertick.Parameters.AddWithValue("@previouscloseprice", lines[2]);
cmdPertick.Parameters.AddWithValue("@openprice", lines[3]);
cmdPertick.Parameters.AddWithValue("@closeprice", lines[4]);
cmdPertick.Parameters.AddWithValue("@highprice", lines[5]);
cmdPertick.Parameters.AddWithValue("@lowprice", lines[6]);
cmdPertick.Parameters.AddWithValue("@volume", lines[7]);
cmdPertick.Parameters.AddWithValue("@valuetotal", lines[8]);
cmdPertick.Parameters.AddWithValue("@freq", lines[9]);
cmdPertick.Parameters.AddWithValue("@lastupdate", lines[10]);
try
{
int rowsAffected = cmdPertick.ExecuteNonQuery();
Console.WriteLine("It Insert {0} lines in table tbtstockrealtime", rowsAffected);
}
catch (Exception ex)
{
}
NpgsqlCommand cmd = new NpgsqlCommand("update tbtindexrealtime set previouscloseprice = @previouscloseprice, openprice = @openprice, closeprice = @closeprice, highprice = @highprice, lowprice = @lowprice, volume = @volume, valuetotal = @valuetotal , freq = @freq, lastupdate = @lastupdate where cdstock = @cdstock", conn);
cmd.Parameters.AddWithValue("@previouscloseprice", lines[2]);
cmd.Parameters.AddWithValue("@openprice", lines[3]);
cmd.Parameters.AddWithValue("@closeprice", lines[4]);
cmd.Parameters.AddWithValue("@highprice", lines[5]);
cmd.Parameters.AddWithValue("@lowprice", lines[6]);
cmd.Parameters.AddWithValue("@volume", lines[7]);
cmd.Parameters.AddWithValue("@valuetotal", lines[8]);
cmd.Parameters.AddWithValue("@freq", lines[9]);
cmd.Parameters.AddWithValue("@lastupdate", lines[10]);
cmd.Parameters.AddWithValue("@cdstock", lines[1]);
try
{
int rowsaffected = cmd.ExecuteNonQuery();
Console.WriteLine("It update {0} lines in table tbtindexrealtime", rowsaffected);
if (rowsaffected == 0)
{
NpgsqlCommand cmd1 = new NpgsqlCommand("insert into tbtindexrealtime(cdstock,previouscloseprice,openprice,closeprice,highprice,lowprice,volume,valuetotal,freq,lastupdate) values (@cdstock,@previouscloseprice,@openprice,@closeprice,@highprice,@lowprice,@volume,@valuetotal,@freq,@lastupdate)", conn);
cmd1.Parameters.AddWithValue("@cdstock", lines[1]);
cmd1.Parameters.AddWithValue("@previouscloseprice", lines[2]);
cmd1.Parameters.AddWithValue("@openprice", lines[3]);
cmd1.Parameters.AddWithValue("@closeprice", lines[4]);
cmd1.Parameters.AddWithValue("@highprice", lines[5]);
cmd1.Parameters.AddWithValue("@lowprice", lines[6]);
cmd1.Parameters.AddWithValue("@volume", lines[7]);
cmd1.Parameters.AddWithValue("@valuetotal", lines[8]);
cmd1.Parameters.AddWithValue("@freq", lines[9]);
cmd1.Parameters.AddWithValue("@lastupdate", lines[10]);
try
{
int rowsAffected = cmd1.ExecuteNonQuery();
Console.WriteLine("It Insert {0} lines in table tbtindexrealtime", rowsAffected);
}
catch (Exception ex)
{
}
}
}
catch (Exception e)
{
Console.Write("Error : " + e.Message + "\n");
}
}
}
}
}