Hi All ,
I have with me a server socket program, I am receiving all the clients, but what happens is i have to write it into a file.
This consumes time.
So we do not have a data loss, but as the time taken to write into the log file increases, some of the data does not get written.
This is the code
//ONLY SHOBA -
#define debug
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Data.SqlClient;
-
using System.Drawing;
-
using System.Text;
-
using System.Net;
-
using GeoBoundary;
-
using LLUTMConvert;
-
using System.Collections.Specialized;
-
using System.IO;
-
using System.Globalization;
-
using System.Net.Sockets;
-
using System.Collections;
-
using System.Threading;
-
using System.Configuration;
-
using System.Messaging;
-
using AmiComServerConsole;
-
using Microsoft.VisualBasic;
-
using Microsoft.VisualBasic.CompilerServices;
-
using System.Runtime.CompilerServices;
-
using System.Windows.Forms;
-
namespace SocketServer
-
{
-
-
public sealed class SingletonExample
-
{
-
private static SingletonExample instance = null;
-
private static readonly object objectlockCheck = new object();
-
-
private SingletonExample()
-
{
-
}
-
-
public static SingletonExample Instance
-
{
-
get
-
{
-
lock (objectlockCheck)
-
{
-
if (instance == null)
-
instance = new SingletonExample();
-
return instance;
-
}
-
}
-
-
-
-
}
-
public void WriteLogMessage(string strMessage)
-
{
-
try
-
{
-
-
string text4 = "D:\\COMSERVER\\9.7\\dummysobha\\bin\\Debug\\Log \\Log_first_8.11.2008.txt";
-
StreamWriter writer1 = File.AppendText(text4);
-
writer1.WriteLine(DateTime.Now.ToString() + " " + strMessage);
-
writer1.Close();
-
}
-
catch (Exception)
-
{
-
}
-
}
-
}
-
-
public class StateObject //class created by mahesh to clear the duplicate records
-
{
-
// Client socket.
-
public Socket workSocket = null;
-
// Size of receive buffer.
-
public const int BufferSize = 5000;
-
// Receive buffer.
-
public byte[] buffer = new byte[BufferSize];
-
// Received data string.
-
public string sb;
-
-
}
-
-
-
-
-
-
-
class Program
-
{
-
-
-
private string DatabaseType;
-
-
-
public static int i=0;
-
public string strConOra;
-
public string strConSql;
-
-
public AsyncCallback pfnWorkerCallBack;
-
private Socket m_mainSocket;
-
-
public int port = 7777;
-
public byte[] m_byBuff = new byte[5000];
-
public int flag2 = 0;
-
public string queuePath;
-
private string strDateFormat;
-
private string strLocation;
-
-
public byte[] inValue = new byte[] { 1, 0, 0, 0, 0, 0, 5, 0, 1, 0, 0, 0 }; //1 -Minutes // 0,5 -- 0 --seconds 6th parameter-->seconds 7th parameter-->minutes
-
-
public byte[] outvalue = new byte[10];
-
-
private string m_strConnection;
-
private SqlConnection ocon;
-
private SqlConnection ocon2;
-
-
public long Mylockcounter = 0;
-
-
private SqlConnection MyCon;
-
public string Conn;
-
public Program()
-
{
-
#if debug
-
this.queuePath = ".\\private$\\AMI";
-
#else
-
this.queuePath = ".\\private$\\AMI";
-
#endif
-
this.m_strConnection = ConfigurationSettings.AppSettings["Connection"];
-
this.strDateFormat = null;
-
this.strLocation = null;
-
StartListen();
-
-
-
}
-
public void StartListen()
-
{
-
-
-
try
-
{
-
NameValueCollection collection1 = ConfigurationSettings.AppSettings;
-
//string portStr = this.IPPort;
-
//int port = System.Convert.ToInt32(portStr);
-
// Create the listening socket...
-
m_mainSocket = new Socket(AddressFamily.InterNetwork,
-
SocketType.Stream,
-
ProtocolType.Tcp);
-
IPAddress ip;
-
-
#if debug
-
string ipstr = "192.168.0.20";
-
#else
-
string ipstr = "192.168.3.7";
-
#endif
-
-
ip = IPAddress.Parse(ipstr);
-
-
IPEndPoint ipLocal = new IPEndPoint(ip, port);
-
//// Bind to local IP Address...
-
m_mainSocket.Bind(ipLocal);
-
//// Start listening...
-
m_mainSocket.Listen(400);
-
-
-
-
// New method
-
-
string text5 = "provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=VTS;password=VTS;Data Source=DMGIS";
-
string text4 = "workstation id=192.168.0.20;Data Source=192.168.0.20;User ID=sa;Password=1234;Initial Catalog=VTS1;Max Pool Size=300;Min Pool Size=1;Pooling=True";
-
this.Conn = "workstation id=192.168.0.20;Data Source=192.168.0.20;User ID=sa;Password=1234;Initial Catalog=VTS1;Max Pool Size=300;Min Pool Size=1;Pooling=True";
-
-
MyCon = new SqlConnection(this.Conn);
-
MyCon.Open();
-
-
-
this.DatabaseType = "SQL";
-
this.strDateFormat = "us";
-
if (this.strLocation == null)
-
{
-
this.strLocation = "Road name not available";
-
}
-
if (this.DatabaseType == "SQL")
-
{
-
this.strConSql = text4;
-
}
-
else if (this.DatabaseType == "ORACLE")
-
{
-
this.strConOra = text5;
-
}
-
else
-
{
-
WriteErrorLogMessage("Proper database was not mentioned");
-
}
-
collection1 = null;
-
-
while (true)
-
{
-
-
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
-
Thread.Sleep(2000);
-
}
-
-
}
-
catch (Exception e1)
-
{
-
}
-
}
-
-
-
public void OnClientConnect(IAsyncResult asyn)
-
{
-
try
-
{
-
Socket msocket;
-
-
msocket = m_mainSocket.EndAccept(asyn);
-
-
msocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1);
-
msocket.IOControl(IOControlCode.KeepAliveValues, inValue, outvalue);
-
WaitForData(msocket);
-
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
-
-
}
-
-
catch (Exception e1)
-
{
-
// WriteErrorLogMessage(e1.Message);
-
}
-
}
-
-
public void WaitForData(System.Net.Sockets.Socket soc)
-
{
-
try
-
{
-
Console.WriteLine("\n");
-
Console.WriteLine("WaitforData");
-
Console.WriteLine("\n");
-
-
if (pfnWorkerCallBack == null)
-
{
-
-
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
-
}
-
-
StateObject state = new StateObject();
-
state.workSocket = soc;
-
-
soc.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, pfnWorkerCallBack, state);
-
-
-
}
-
catch (Exception e1)
-
{
-
}
-
-
}
-
public void OnDataReceived(IAsyncResult asyn)
-
{
-
MyCon = new SqlConnection(this.Conn);
-
-
try
-
{
-
Console.WriteLine("\n");
-
Console.WriteLine("OnDataRecd");
-
Console.WriteLine("\n");
-
-
StateObject stateobject = (StateObject)asyn.AsyncState;
-
Socket sock = stateobject.workSocket;
-
-
//assgning the app socket to new socket
-
Object thisLock = new Object();
-
-
string previousString;
-
int nBytesRec = sock.EndReceive(asyn);
-
if (nBytesRec > 0)
-
{
-
string sReceived = System.Text.Encoding.ASCII.GetString(stateobject.buffer, 0, StateObject.BufferSize);
-
sReceived = sReceived.TrimEnd('\0');
-
if (sReceived != null && sReceived != " " && sReceived != "")
-
{
-
{
-
-
{
-
WriteLogMessage(sReceived);
-
byte[] Buffer = new byte[StateObject.BufferSize];
-
stateobject.buffer = Buffer;
-
sock.BeginReceive(stateobject.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(OnDataReceived), stateobject); }
-
-
}
-
else
-
{
-
sock.Disconnect(false);
-
sock.Close();
-
stateobject = null;
-
sole.WriteLine("Inside the Else part in ondatareceived");
-
}
-
-
}
-
-
catch (SocketException e1)
-
{
-
-
Console.WriteLine("Inside the catch1 part in ondatareceived");
-
}
-
-
catch (Exception e1)
-
{
-
Console.WriteLine("Inside the Catch2 part in ondatareceived");
-
}
-
}
-
Any help will be appreciated.
Regards
cmrhema
2 1680 Plater 7,872
Recognized Expert Expert
I've not had trouble with any data loss when logging at high speeds.
I DO however see that you are not doing anything in the catch section of the try/catch for your logging. I would guess errors are being thrown.
I would also guess that sine you use async connections, that multiple connections are attempting to log at once and it is throwing exceptions. Try putting some code in to check for exceptions in your writelog function. I bet you will get a better idea of what is happening.
I've not had trouble with any data loss when logging at high speeds.
I DO however see that you are not doing anything in the catch section of the try/catch for your logging. I would guess errors are being thrown.
I would also guess that sine you use async connections, that multiple connections are attempting to log at once and it is throwing exceptions. Try putting some code in to check for exceptions in your writelog function. I bet you will get a better idea of what is happening.
We have done it, but we did another thing too, instead of writing in a log file, we write into a queue,
It writes up everything without any data loss.
But when we write up in a log file we face data loss.
As of now we decided to put up in a queue first and then read the data from it.
Any suggestions other than that.
Many thanks for the reply Plater.
Regards
cmrhema
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Tony C |
last post by:
Does Python have a function that is analogous to C's write() or
fwrite()-
that is , I want to write a file (of arbitrary data) that is 100K, or
1MB (or more) bytes long..
Both write() and fwrite() in C allow the user to specify the size of
the data to be written.
Python's write only allows a string to be passed in.
|
by: Jon Hyland |
last post by:
Hi all,
I'm looking for the fastest way to write (and/or read) binary to a
file in VC++. I've been using the fstream object in this way:
//unsigned char *pDataOut and long iLength initilized somewhere..
fstream file_out((const char *)pszOutFile,
ios::in|ios::trunc|ios::binary);
|
by: Christian Blackburn |
last post by:
Hi Gang,
I would like to know how to write a single bit to a file (if that's
possible). I would like to repetitiously write a 1 or 0 as the data I'm
trying to save would dictate.
Thanks in Advance for an example or explanation,
Christian Blackburn
|
by: tim |
last post by:
I have a multi-dimensional byte array,
private someArray(,,) as byte
and it's huge
redim someArray(100,640,480,3)
after populating the array, I write it to a text file to
save it. (so I don't run out of memory). I will retrieve
from the file later.
What I do is use streamwriter.write and streamreader.read
|
by: Eric Cadwell |
last post by:
Is there a faster way to write the last 100K of a large file? This code
takes almost two minutes to run on my machine.
int buffer = 100000;
int length = 2000000000;
string file = @"C:\test.txt";
if (File.Exists(file))
File.Delete(file);
| |
by: Sean Davis |
last post by:
I am working on a simple script to read from one database (oracle) and
write to another (postgresql). I retrieve the data from oracle in
chunks and drop the data to postgresql continuously. The author of one
of the python database clients mentioned that using one thread to
retrieve the data from the oracle database and another to insert the
data into postgresql with something like a pipe between the two threads
might make sense, keeping...
|
by: golden |
last post by:
Hello,
I am going to ask a question regarding
write and lseek. I will provide code at the end of this, but first
some background.
I am trying to identify the cause of some latency in writing to disk.
My user claims that performance is much slower on SAN than on local
disk. The developer provided me a C++ program that performed a write
|
by: LilacSkin |
last post by:
Hi,
Currently, I write an signed long long in a file with the fprintf
function:
signed long long * pData = NULL;
unsigned long long k = 0;
unsigned long DataAvail = 0 ;
pDataRx = (signed long long *) malloc ( sizeof(signed long long ) *
|
by: cmrhema |
last post by:
Hi,
We are receiving huge number of data thro sockets, which we write up in a Message Queue. Thereafter we take all the values from the Queue and write up into the database.
However we notice that there is some data loss when we send huge data. We tested out with 1,000,000 records. We recieved these records within 2 minutes, and we wrote all the data from the queue to the database (Kindly note that both are different exe, but both will...
|
by: Mohamed Yousef |
last post by:
Hello ,
let's say , I'm moving large files through network between devices
what is the fastest way to do this ?
what i came up with :-
1) using regular file operations with an in memory limit of 4MB which
when filled written to disk and re-filled again
2) using memory mapped files in the following way , map first 20MB of
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |