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
Expand|Select|Wrap|Line Numbers
- #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