Tom Knowlton
asked on
Server pushing data to client - slowly
I have a server program (windows form) sending data to a client via TCP/IP.
I am reading an entife file into an ArrayList first.......then stepping through the ArrayList line by line and pushing the data to the client.
It seems to me that the entire process should take no longer than 5 or 10 seconds. Instead it is taking minutes.
I cannot for the life of me figure-out where the delay is. My intent is to push the data through as fast as possible.
The data file is 1.2 MB in size.
The timer that sends the data fires every 1 milliseconds.
Here is the Server side code:
using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace DefaultNamespace
{
/// <summary>
/// Description of SocketServer.
/// </summary>
public class SocketServer : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextB ox textBoxPort;
private System.Windows.Forms.TextB ox textBoxMsg;
private System.Windows.Forms.Butto n buttonStopListen;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextB ox textBoxIP;
private System.Windows.Forms.Butto n buttonStartListen;
private System.Windows.Forms.Butto n buttonClose;
//Tom
private System.IO.StreamReader filereader = null;
private ArrayList linesFromFile = new ArrayList();
private int curLine=0;
const int MAX_CLIENTS = 10;
public AsyncCallback pfnWorkerCallBack ;
private Socket m_mainSocket;
private Socket [] m_workerSocket = new Socket[10];
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.ICon tainer components;
private System.Windows.Forms.ListB ox listBox1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextB ox textBox1;
private System.Windows.Forms.TextB ox textBox2;
private int m_clientCount = 0;
public SocketServer()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
// Display the local IP address on the GUI
textBoxIP.Text = GetIP();
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new SocketServer());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Cont ainer();
this.buttonClose = new System.Windows.Forms.Butto n();
this.buttonStartListen = new System.Windows.Forms.Butto n();
this.textBoxIP = new System.Windows.Forms.TextB ox();
this.label1 = new System.Windows.Forms.Label ();
this.buttonStopListen = new System.Windows.Forms.Butto n();
this.textBoxMsg = new System.Windows.Forms.TextB ox();
this.textBoxPort = new System.Windows.Forms.TextB ox();
this.label2 = new System.Windows.Forms.Label ();
this.label3 = new System.Windows.Forms.Label ();
this.timer1 = new System.Windows.Forms.Timer (this.comp onents);
this.listBox1 = new System.Windows.Forms.ListB ox();
this.label6 = new System.Windows.Forms.Label ();
this.textBox1 = new System.Windows.Forms.TextB ox();
this.textBox2 = new System.Windows.Forms.TextB ox();
this.SuspendLayout();
//
// buttonClose
//
this.buttonClose.Location = new System.Drawing.Point(152, 152);
this.buttonClose.Name = "buttonClose";
this.buttonClose.Size = new System.Drawing.Size(88, 24);
this.buttonClose.TabIndex = 11;
this.buttonClose.Text = "Close";
this.buttonClose.Click += new System.EventHandler(this.B uttonClose Click);
//
// buttonStartListen
//
this.buttonStartListen.Bac kColor = System.Drawing.Color.Blue;
this.buttonStartListen.Fon t = new System.Drawing.Font("Tahom a", 8.25F, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, ((System.Byte)(0)));
this.buttonStartListen.For eColor = System.Drawing.Color.Yello w;
this.buttonStartListen.Loc ation = new System.Drawing.Point(152, 56);
this.buttonStartListen.Nam e = "buttonStartListen";
this.buttonStartListen.Siz e = new System.Drawing.Size(88, 40);
this.buttonStartListen.Tab Index = 4;
this.buttonStartListen.Tex t = "Start Listening";
this.buttonStartListen.Cli ck += new System.EventHandler(this.B uttonStart ListenClic k);
//
// textBoxIP
//
this.textBoxIP.Location = new System.Drawing.Point(88, 16);
this.textBoxIP.Name = "textBoxIP";
this.textBoxIP.Size = new System.Drawing.Size(120, 20);
this.textBoxIP.TabIndex = 12;
this.textBoxIP.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Port";
//
// buttonStopListen
//
this.buttonStopListen.Back Color = System.Drawing.Color.Red;
this.buttonStopListen.Font = new System.Drawing.Font("Tahom a", 8.25F, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, ((System.Byte)(0)));
this.buttonStopListen.Fore Color = System.Drawing.Color.Yello w;
this.buttonStopListen.Loca tion = new System.Drawing.Point(152, 104);
this.buttonStopListen.Name = "buttonStopListen";
this.buttonStopListen.Size = new System.Drawing.Size(88, 40);
this.buttonStopListen.TabI ndex = 5;
this.buttonStopListen.Text = "Stop Listening";
this.buttonStopListen.Clic k += new System.EventHandler(this.B uttonStopL istenClick );
//
// textBoxMsg
//
this.textBoxMsg.BackColor = System.Drawing.SystemColor s.Control;
this.textBoxMsg.BorderStyl e = System.Windows.Forms.Borde rStyle.Non e;
this.textBoxMsg.ForeColor = System.Drawing.SystemColor s.HotTrack ;
this.textBoxMsg.Location = new System.Drawing.Point(120, 240);
this.textBoxMsg.Name = "textBoxMsg";
this.textBoxMsg.ReadOnly = true;
this.textBoxMsg.Size = new System.Drawing.Size(192, 13);
this.textBoxMsg.TabIndex = 14;
this.textBoxMsg.Text = "None";
//
// textBoxPort
//
this.textBoxPort.Location = new System.Drawing.Point(88, 40);
this.textBoxPort.Name = "textBoxPort";
this.textBoxPort.Size = new System.Drawing.Size(40, 20);
this.textBoxPort.TabIndex = 0;
this.textBoxPort.Text = "8000";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 2;
this.label2.Text = "Server IP";
//
// label3
//
this.label3.Location = new System.Drawing.Point(0, 240);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(112, 16);
this.label3.TabIndex = 13;
this.label3.Text = "Status Message:";
//
// timer1
//
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.t imer1_Tick );
//
// listBox1
//
this.listBox1.AllowDrop = true;
this.listBox1.Location = new System.Drawing.Point(16, 72);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 95);
this.listBox1.TabIndex = 15;
this.listBox1.DragDrop += new System.Windows.Forms.DragE ventHandle r(this.lis tBox1_Drag Drop);
this.listBox1.DragEnter += new System.Windows.Forms.DragE ventHandle r(this.lis tBox1_Drag Enter);
//
// label6
//
this.label6.Location = new System.Drawing.Point(24, 176);
this.label6.Name = "label6";
this.label6.TabIndex = 16;
this.label6.Text = "label6";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 272);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 20);
this.textBox1.TabIndex = 17;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 304);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(304, 20);
this.textBox2.TabIndex = 18;
this.textBox2.Text = "textBox2";
//
// SocketServer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 341);
this.Controls.Add(this.tex tBox2);
this.Controls.Add(this.tex tBox1);
this.Controls.Add(this.tex tBoxMsg);
this.Controls.Add(this.tex tBoxIP);
this.Controls.Add(this.tex tBoxPort);
this.Controls.Add(this.lab el6);
this.Controls.Add(this.lis tBox1);
this.Controls.Add(this.lab el3);
this.Controls.Add(this.but tonClose);
this.Controls.Add(this.but tonStopLis ten);
this.Controls.Add(this.but tonStartLi sten);
this.Controls.Add(this.lab el2);
this.Controls.Add(this.lab el1);
this.Name = "SocketServer";
this.Text = "SocketServer";
this.Load += new System.EventHandler(this.S ocketServe r_Load);
this.ResumeLayout(false);
}
#endregion
void ButtonStartListenClick(obj ect sender, System.EventArgs e)
{
this.timer1.Enabled = true;
try
{
// Check the port value
if(textBoxPort.Text == ""){
MessageBox.Show("Please enter a Port Number");
return;
}
string portStr = textBoxPort.Text;
int port = System.Convert.ToInt32(por tStr);
// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.Inter Network,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint (IPAddress.Any, port);
// Bind to local IP Address...
m_mainSocket.Bind( ipLocal );
// Start listening...
m_mainSocket.Listen (4);
// Create the call back for any client connections...
m_mainSocket.BeginAccept(n ew AsyncCallback (OnClientConnect), null);
UpdateControls(true);
}
catch(SocketException se)
{
MessageBox.Show ( se.Message );
}
}
private void UpdateControls( bool listening )
{
buttonStartListen.Enabled = !listening;
buttonStopListen.Enabled = listening;
}
// This is the call back function, which will be invoked when a client is connected
public void OnClientConnect(IAsyncResu lt asyn)
{
try
{
// Here we complete/end the BeginAccept() asynchronous call
// by calling EndAccept() - which returns the reference to
// a new Socket object
m_workerSocket[m_clientCou nt] = m_mainSocket.EndAccept (asyn);
// Let the worker Socket do the further processing for the
// just connected client
WaitForData(m_workerSocket [m_clientC ount]);
// Now increment the client count
++m_clientCount;
// Display this client connection as a status message on the GUI
String str = String.Format("Client # {0} connected", m_clientCount);
textBoxMsg.Text = str;
// Since the main Socket is now free, it can go back and wait for
// other clients who are attempting to connect
m_mainSocket.BeginAccept(n ew AsyncCallback ( OnClientConnect ),null);
}
catch(ObjectDisposedExcept ion)
{
System.Diagnostics.Debugge r.Log(0,"1 ","\n OnClientConnection: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show ( se.Message );
}
}
public class SocketPacket
{
public System.Net.Sockets.Socket m_currentSocket;
public byte[] dataBuffer = new byte[1];
}
// Start waiting for data from the client
public void WaitForData(System.Net.Soc kets.Socke t soc)
{
try
{
if ( pfnWorkerCallBack == null ){
// Specify the call back function which is to be
// invoked when there is any write activity by the
// connected client
pfnWorkerCallBack = new AsyncCallback (OnDataReceived);
}
SocketPacket theSocPkt = new SocketPacket ();
theSocPkt.m_currentSocket = soc;
// Start receiving any data written by the connected client
// asynchronously
soc .BeginReceive (theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Lengt h,
SocketFlags.None,
pfnWorkerCallBack,
theSocPkt);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
// This the call back function which will be invoked when the socket
// detects any client writing of data on the stream
public void OnDataReceived(IAsyncResul t asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncSt ate ;
int iRx = 0 ;
// Complete the BeginReceive() asynchronous call by EndReceive() method
// which will return the number of characters written to the stream
// by the client
iRx = socketData.m_currentSocket .EndReceiv e (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8. GetDecoder ();
int charLen = d.GetChars(socketData.data Buffer,
0, iRx, chars, 0);
System.String szData = new System.String(chars);
//richTextBoxReceivedMsg.A ppendText( szData);
// Continue the waiting for data on the Socket
WaitForData( socketData.m_currentSocket );
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugge r.Log(0,"1 ","\nOnDat aReceived: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
void ButtonSendMsgClick(object sender, System.EventArgs e)
{
SendData();
// try
// {
// Object objData = richTextBoxSendMsg.Text;
// byte[] byData = System.Text.Encoding.ASCII .GetBytes( objData.To String ());
// for(int i = 0; i < m_clientCount; i++){
// if(m_workerSocket[i] != null){
// if(m_workerSocket[i].Conne cted){
// m_workerSocket[i].Send (byData);
// }
// }
// }
//
// }
// catch(SocketException se)
// {
// MessageBox.Show (se.Message );
// }
}
void SendData()
{
string tempStringToSend="";
int tempCurLine=0;
try
{
//Object objData = richTextBoxSendMsg.Text;
tempCurLine = curLine;
tempStringToSend = (string)linesFromFile[temp CurLine];
Object objData = (object)tempStringToSend;
//Show what line we are on
this.textBox1.Text = tempCurLine.ToString();
//Show what we are sending
this.textBox2.Text = tempStringToSend;
byte[] byData = System.Text.Encoding.ASCII .GetBytes( objData.To String ());
for(int i = 0; i < m_clientCount; i++)
{
if(m_workerSocket[i] != null)
{
if(m_workerSocket[i].Conne cted)
{
m_workerSocket[i].Send (byData);
}
}
}
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
void ButtonStopListenClick(obje ct sender, System.EventArgs e)
{
CloseSockets();
UpdateControls(false);
}
String GetIP()
{
String strHostName = Dns.GetHostName();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostN ame);
// Grab the first IP addresses
String IPStr = "";
foreach(IPAddress ipaddress in iphostentry.AddressList){
IPStr = ipaddress.ToString();
return IPStr;
}
return IPStr;
}
void ButtonCloseClick(object sender, System.EventArgs e)
{
CloseSockets();
Close();
}
void CloseSockets()
{
if(m_mainSocket != null){
m_mainSocket.Close();
}
for(int i = 0; i < m_clientCount; i++){
if(m_workerSocket[i] != null){
m_workerSocket[i].Close();
m_workerSocket[i] = null;
}
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
SendData();
curLine++;
if(curLine > linesFromFile.Count -1)
{
curLine = 0;
}
}
private void listBox1_DragDrop(object sender, System.Windows.Forms.DragE ventArgs e)
{
string[] files = (string[])e.Data.GetData(" FileDrop", false);
foreach (string s in files)
{
//just filename
listBox1.Items.Add(s.Subst ring(1 + s.LastIndexOf(@"\")));
//or fullpathname
// listBox1.Items.Add(s);
// If you want to add the text in the drag file, please read it line by line and add to the list box here.
filereader = new System.IO.StreamReader(s);
// linesFromFile.Add("BEGIN") ;
do
{
linesFromFile.Add(fileread er.ReadLin e());
}
while(filereader.Peek() != -1);
this.label6.Text = "DONE";
}
}
private void listBox1_DragEnter(object sender, System.Windows.Forms.DragE ventArgs e)
{
if (e.Data.GetDataPresent(Dat aFormats.F ileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void SocketServer_Load(object sender, System.EventArgs e)
{
}
}
}
I am reading an entife file into an ArrayList first.......then stepping through the ArrayList line by line and pushing the data to the client.
It seems to me that the entire process should take no longer than 5 or 10 seconds. Instead it is taking minutes.
I cannot for the life of me figure-out where the delay is. My intent is to push the data through as fast as possible.
The data file is 1.2 MB in size.
The timer that sends the data fires every 1 milliseconds.
Here is the Server side code:
using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace DefaultNamespace
{
/// <summary>
/// Description of SocketServer.
/// </summary>
public class SocketServer : System.Windows.Forms.Form
{
private System.Windows.Forms.Label
private System.Windows.Forms.Label
private System.Windows.Forms.TextB
private System.Windows.Forms.TextB
private System.Windows.Forms.Butto
private System.Windows.Forms.Label
private System.Windows.Forms.TextB
private System.Windows.Forms.Butto
private System.Windows.Forms.Butto
//Tom
private System.IO.StreamReader filereader = null;
private ArrayList linesFromFile = new ArrayList();
private int curLine=0;
const int MAX_CLIENTS = 10;
public AsyncCallback pfnWorkerCallBack ;
private Socket m_mainSocket;
private Socket [] m_workerSocket = new Socket[10];
private System.Windows.Forms.Timer
private System.ComponentModel.ICon
private System.Windows.Forms.ListB
private System.Windows.Forms.Label
private System.Windows.Forms.TextB
private System.Windows.Forms.TextB
private int m_clientCount = 0;
public SocketServer()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
// Display the local IP address on the GUI
textBoxIP.Text = GetIP();
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new SocketServer());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Cont
this.buttonClose = new System.Windows.Forms.Butto
this.buttonStartListen = new System.Windows.Forms.Butto
this.textBoxIP = new System.Windows.Forms.TextB
this.label1 = new System.Windows.Forms.Label
this.buttonStopListen = new System.Windows.Forms.Butto
this.textBoxMsg = new System.Windows.Forms.TextB
this.textBoxPort = new System.Windows.Forms.TextB
this.label2 = new System.Windows.Forms.Label
this.label3 = new System.Windows.Forms.Label
this.timer1 = new System.Windows.Forms.Timer
this.listBox1 = new System.Windows.Forms.ListB
this.label6 = new System.Windows.Forms.Label
this.textBox1 = new System.Windows.Forms.TextB
this.textBox2 = new System.Windows.Forms.TextB
this.SuspendLayout();
//
// buttonClose
//
this.buttonClose.Location = new System.Drawing.Point(152, 152);
this.buttonClose.Name = "buttonClose";
this.buttonClose.Size = new System.Drawing.Size(88, 24);
this.buttonClose.TabIndex = 11;
this.buttonClose.Text = "Close";
this.buttonClose.Click += new System.EventHandler(this.B
//
// buttonStartListen
//
this.buttonStartListen.Bac
this.buttonStartListen.Fon
this.buttonStartListen.For
this.buttonStartListen.Loc
this.buttonStartListen.Nam
this.buttonStartListen.Siz
this.buttonStartListen.Tab
this.buttonStartListen.Tex
this.buttonStartListen.Cli
//
// textBoxIP
//
this.textBoxIP.Location = new System.Drawing.Point(88, 16);
this.textBoxIP.Name = "textBoxIP";
this.textBoxIP.Size = new System.Drawing.Size(120, 20);
this.textBoxIP.TabIndex = 12;
this.textBoxIP.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Port";
//
// buttonStopListen
//
this.buttonStopListen.Back
this.buttonStopListen.Font
this.buttonStopListen.Fore
this.buttonStopListen.Loca
this.buttonStopListen.Name
this.buttonStopListen.Size
this.buttonStopListen.TabI
this.buttonStopListen.Text
this.buttonStopListen.Clic
//
// textBoxMsg
//
this.textBoxMsg.BackColor = System.Drawing.SystemColor
this.textBoxMsg.BorderStyl
this.textBoxMsg.ForeColor = System.Drawing.SystemColor
this.textBoxMsg.Location = new System.Drawing.Point(120, 240);
this.textBoxMsg.Name = "textBoxMsg";
this.textBoxMsg.ReadOnly = true;
this.textBoxMsg.Size = new System.Drawing.Size(192, 13);
this.textBoxMsg.TabIndex = 14;
this.textBoxMsg.Text = "None";
//
// textBoxPort
//
this.textBoxPort.Location = new System.Drawing.Point(88, 40);
this.textBoxPort.Name = "textBoxPort";
this.textBoxPort.Size = new System.Drawing.Size(40, 20);
this.textBoxPort.TabIndex = 0;
this.textBoxPort.Text = "8000";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 2;
this.label2.Text = "Server IP";
//
// label3
//
this.label3.Location = new System.Drawing.Point(0, 240);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(112, 16);
this.label3.TabIndex = 13;
this.label3.Text = "Status Message:";
//
// timer1
//
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.t
//
// listBox1
//
this.listBox1.AllowDrop = true;
this.listBox1.Location = new System.Drawing.Point(16, 72);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 95);
this.listBox1.TabIndex = 15;
this.listBox1.DragDrop += new System.Windows.Forms.DragE
this.listBox1.DragEnter += new System.Windows.Forms.DragE
//
// label6
//
this.label6.Location = new System.Drawing.Point(24, 176);
this.label6.Name = "label6";
this.label6.TabIndex = 16;
this.label6.Text = "label6";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 272);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 20);
this.textBox1.TabIndex = 17;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 304);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(304, 20);
this.textBox2.TabIndex = 18;
this.textBox2.Text = "textBox2";
//
// SocketServer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 341);
this.Controls.Add(this.tex
this.Controls.Add(this.tex
this.Controls.Add(this.tex
this.Controls.Add(this.tex
this.Controls.Add(this.tex
this.Controls.Add(this.lab
this.Controls.Add(this.lis
this.Controls.Add(this.lab
this.Controls.Add(this.but
this.Controls.Add(this.but
this.Controls.Add(this.but
this.Controls.Add(this.lab
this.Controls.Add(this.lab
this.Name = "SocketServer";
this.Text = "SocketServer";
this.Load += new System.EventHandler(this.S
this.ResumeLayout(false);
}
#endregion
void ButtonStartListenClick(obj
{
this.timer1.Enabled = true;
try
{
// Check the port value
if(textBoxPort.Text == ""){
MessageBox.Show("Please enter a Port Number");
return;
}
string portStr = textBoxPort.Text;
int port = System.Convert.ToInt32(por
// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.Inter
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint (IPAddress.Any, port);
// Bind to local IP Address...
m_mainSocket.Bind( ipLocal );
// Start listening...
m_mainSocket.Listen (4);
// Create the call back for any client connections...
m_mainSocket.BeginAccept(n
UpdateControls(true);
}
catch(SocketException se)
{
MessageBox.Show ( se.Message );
}
}
private void UpdateControls( bool listening )
{
buttonStartListen.Enabled = !listening;
buttonStopListen.Enabled = listening;
}
// This is the call back function, which will be invoked when a client is connected
public void OnClientConnect(IAsyncResu
{
try
{
// Here we complete/end the BeginAccept() asynchronous call
// by calling EndAccept() - which returns the reference to
// a new Socket object
m_workerSocket[m_clientCou
// Let the worker Socket do the further processing for the
// just connected client
WaitForData(m_workerSocket
// Now increment the client count
++m_clientCount;
// Display this client connection as a status message on the GUI
String str = String.Format("Client # {0} connected", m_clientCount);
textBoxMsg.Text = str;
// Since the main Socket is now free, it can go back and wait for
// other clients who are attempting to connect
m_mainSocket.BeginAccept(n
}
catch(ObjectDisposedExcept
{
System.Diagnostics.Debugge
}
catch(SocketException se)
{
MessageBox.Show ( se.Message );
}
}
public class SocketPacket
{
public System.Net.Sockets.Socket m_currentSocket;
public byte[] dataBuffer = new byte[1];
}
// Start waiting for data from the client
public void WaitForData(System.Net.Soc
{
try
{
if ( pfnWorkerCallBack == null ){
// Specify the call back function which is to be
// invoked when there is any write activity by the
// connected client
pfnWorkerCallBack = new AsyncCallback (OnDataReceived);
}
SocketPacket theSocPkt = new SocketPacket ();
theSocPkt.m_currentSocket = soc;
// Start receiving any data written by the connected client
// asynchronously
soc .BeginReceive (theSocPkt.dataBuffer, 0,
theSocPkt.dataBuffer.Lengt
SocketFlags.None,
pfnWorkerCallBack,
theSocPkt);
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
// This the call back function which will be invoked when the socket
// detects any client writing of data on the stream
public void OnDataReceived(IAsyncResul
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncSt
int iRx = 0 ;
// Complete the BeginReceive() asynchronous call by EndReceive() method
// which will return the number of characters written to the stream
// by the client
iRx = socketData.m_currentSocket
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.
int charLen = d.GetChars(socketData.data
0, iRx, chars, 0);
System.String szData = new System.String(chars);
//richTextBoxReceivedMsg.A
// Continue the waiting for data on the Socket
WaitForData( socketData.m_currentSocket
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugge
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
void ButtonSendMsgClick(object sender, System.EventArgs e)
{
SendData();
// try
// {
// Object objData = richTextBoxSendMsg.Text;
// byte[] byData = System.Text.Encoding.ASCII
// for(int i = 0; i < m_clientCount; i++){
// if(m_workerSocket[i] != null){
// if(m_workerSocket[i].Conne
// m_workerSocket[i].Send (byData);
// }
// }
// }
//
// }
// catch(SocketException se)
// {
// MessageBox.Show (se.Message );
// }
}
void SendData()
{
string tempStringToSend="";
int tempCurLine=0;
try
{
//Object objData = richTextBoxSendMsg.Text;
tempCurLine = curLine;
tempStringToSend = (string)linesFromFile[temp
Object objData = (object)tempStringToSend;
//Show what line we are on
this.textBox1.Text = tempCurLine.ToString();
//Show what we are sending
this.textBox2.Text = tempStringToSend;
byte[] byData = System.Text.Encoding.ASCII
for(int i = 0; i < m_clientCount; i++)
{
if(m_workerSocket[i] != null)
{
if(m_workerSocket[i].Conne
{
m_workerSocket[i].Send (byData);
}
}
}
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
}
}
void ButtonStopListenClick(obje
{
CloseSockets();
UpdateControls(false);
}
String GetIP()
{
String strHostName = Dns.GetHostName();
// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostN
// Grab the first IP addresses
String IPStr = "";
foreach(IPAddress ipaddress in iphostentry.AddressList){
IPStr = ipaddress.ToString();
return IPStr;
}
return IPStr;
}
void ButtonCloseClick(object sender, System.EventArgs e)
{
CloseSockets();
Close();
}
void CloseSockets()
{
if(m_mainSocket != null){
m_mainSocket.Close();
}
for(int i = 0; i < m_clientCount; i++){
if(m_workerSocket[i] != null){
m_workerSocket[i].Close();
m_workerSocket[i] = null;
}
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
SendData();
curLine++;
if(curLine > linesFromFile.Count -1)
{
curLine = 0;
}
}
private void listBox1_DragDrop(object sender, System.Windows.Forms.DragE
{
string[] files = (string[])e.Data.GetData("
foreach (string s in files)
{
//just filename
listBox1.Items.Add(s.Subst
//or fullpathname
// listBox1.Items.Add(s);
// If you want to add the text in the drag file, please read it line by line and add to the list box here.
filereader = new System.IO.StreamReader(s);
// linesFromFile.Add("BEGIN")
do
{
linesFromFile.Add(fileread
}
while(filereader.Peek() != -1);
this.label6.Text = "DONE";
}
}
private void listBox1_DragEnter(object sender, System.Windows.Forms.DragE
{
if (e.Data.GetDataPresent(Dat
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void SocketServer_Load(object sender, System.EventArgs e)
{
}
}
}
ASKER
cookre:
I think your suggestion helps some...but it is still pretty slow!!!!!!!
I think your suggestion helps some...but it is still pretty slow!!!!!!!
You mean we'll have to really LOOK at the code?
Uh, OK.
Uh, OK.
ASKER
You mean we'll have to really LOOK at the code?
Uh, OK.
===============
Yeah....I apologize for that. :)
Uh, OK.
===============
Yeah....I apologize for that. :)
ASKER
I should mention that the read-in of the file INTO the ArrayList is very very FAST...like under 1 second.
It is when I start to push the data out via TCP IP that I am getting the slow-down.
It is when I start to push the data out via TCP IP that I am getting the slow-down.
Unless your lines are close to the size of a packet, you're doing a gazillion sends.
When you expect to do large transfers, the common way is to fill a packet with as much as you can and reassemble the data on the recieving end. Note that this means you have to include sequence info in each packet so the re-assembler isn't fooled by out of sequence packets.
Let's say your lines average 100 bytes each. This means you're doing ten times more sends than if you sent 10 lines per packet.
When you expect to do large transfers, the common way is to fill a packet with as much as you can and reassemble the data on the recieving end. Note that this means you have to include sequence info in each packet so the re-assembler isn't fooled by out of sequence packets.
Let's say your lines average 100 bytes each. This means you're doing ten times more sends than if you sent 10 lines per packet.
ASKER
>>>>Unless your lines are close to the size of a packet, you're doing a gazillion sends.
Yes....the ArrayList contains 100,000 lines plus, and I am sending one line at a time.
>>>>When you expect to do large transfers, the common way is to fill a packet with as much as you can and reassemble the data on the recieving end. Note that this means you have to include sequence info in each packet so the re-assembler isn't fooled by out of sequence packets.
So how should my server code change?
Yes....the ArrayList contains 100,000 lines plus, and I am sending one line at a time.
>>>>When you expect to do large transfers, the common way is to fill a packet with as much as you can and reassemble the data on the recieving end. Note that this means you have to include sequence info in each packet so the re-assembler isn't fooled by out of sequence packets.
So how should my server code change?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
See hat happens if you disable the timer as the first thing in Timer1_Tick(), then re-enable just before leavingL
private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Enabled=false;
SendData();
curLine++;
if(curLine > linesFromFile.Count -1)
{
curLine = 0;
}
timer1.Enabled=true;
}