473,289 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Socket.Send is slow when sending within same process

There is two examples of code.

Example 1. Send and Receive within the same process. Put this code in a
console app called SendAndReceive and run the code.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
SocketReceive sr = new SocketReceive();
Thread tsr = new Thread(new ThreadStart(sr.Process));
tsr.IsBackground = true;
tsr.Start();

SocketSend ss = new SocketSend();
Thread tss = new Thread(new ThreadStart(ss.Process));
tss.IsBackground = true;
tss.Start();

while (tsr.IsAlive || tss.IsAlive)
{
Thread.Sleep(100);
}
}
}

class SocketSend
{
public void Process()
{
// Register the socket
IPEndPoint endPoint = new
IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], 7001);

Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.IP);
try
{
int max = 600000;
socket.Connect(endPoint);
long start = start = DateTime.Now.Ticks; ;
long time1 = 0;
int completed = 0;
for (int i = 0; i <= max; i++)
{
if ((i % 1000) == 0)
Console.WriteLine("Sending {0}", i);
SendLiteMessage(socket, i);
completed++;
time1 = DateTime.Now.Ticks;
if (((time1 - start) / 10000) 1000)
{
Console.WriteLine("Sent {0} in {1}(ms)", completed,
((time1 - start) / 10000));
start = DateTime.Now.Ticks;
completed = 0;
}
}
}

catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}

private void SendLiteMessage(Socket socket, int count)
{
byte[] bas = new byte[2048];
bas[0] = (byte)count;

long start = start = DateTime.Now.Ticks; ;
socket.Send(bas, SocketFlags.None);
long time1 = DateTime.Now.Ticks;
//Console.WriteLine("Socket Send took {0}", (time1 - start));
}

}

class SocketReceive
{
private object ReceiveMessageLite(NetworkStream ns, int count)
{
byte[] bas = new byte[2048];
int read = ns.Read(bas, 0, bas.Length);
return bas;
}

public void Process()
{
IPEndPoint endPoint = new
IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], 7001);
try
{
Console.WriteLine("Waiting for Sender...");
TcpListener listener = new TcpListener(endPoint.Address,
7001);

listener.Start();
TcpClient client = listener.AcceptTcpClient();
client.ReceiveBufferSize = 32768;
NetworkStream ns = client.GetStream();
long start = start = DateTime.Now.Ticks; ;
long time1 = 0;
int completed = 0;
for (int i = 0; i < 600000; i++)
{
if ((i % 1000) == 0)
Console.WriteLine("Receiving {0}", i);

object obj = ReceiveMessageLite(ns, i);
completed++;
time1 = DateTime.Now.Ticks;
if (((time1 - start) / 10000) 1000)
{
Console.WriteLine("Received {0} in {1}(ms)",
completed, ((time1 - start) / 10000));
start = DateTime.Now.Ticks;
completed = 0;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
}
}
}
}

Example 2. Put this code in a console app called Receive
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
SocketReceive sr = new SocketReceive();
Thread tsr = new Thread(new ThreadStart(sr.Process));
tsr.IsBackground = true;
tsr.Start();

while (tsr.IsAlive)
{
Thread.Sleep(100);
}
}
}

class SocketReceive
{
private object ReceiveMessageLite(NetworkStream ns, int count)
{
byte[] bas = new byte[2048];
int read = ns.Read(bas, 0, bas.Length);
return bas;
}

public void Process()
{
IPEndPoint endPoint = new
IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], 7001);
try
{
Console.WriteLine("Waiting for Sender...");
TcpListener listener = new TcpListener(endPoint.Address,
7001);

listener.Start();
TcpClient client = listener.AcceptTcpClient();
client.ReceiveBufferSize = 32768;
NetworkStream ns = client.GetStream();
long start = start = DateTime.Now.Ticks; ;
long time1 = 0;
int completed = 0;
for (int i = 0; i < 600000; i++)
{
if ((i % 1000) == 0)
Console.WriteLine("Receiving {0}", i);

object obj = ReceiveMessageLite(ns, i);
completed++;
time1 = DateTime.Now.Ticks;
if (((time1 - start) / 10000) 1000)
{
Console.WriteLine("Received {0} in {1}(ms)",
completed, ((time1 - start) / 10000));
start = DateTime.Now.Ticks;
completed = 0;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
}
}
}
}
Example 2. Put this code in a console app called Send.
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
SocketSend ss = new SocketSend();
Thread tss = new Thread(new ThreadStart(ss.Process));
tss.IsBackground = true;
tss.Start();

while (tss.IsAlive)
{
Thread.Sleep(100);
}
}
}

class SocketSend
{
public void Process()
{
// Register the socket
IPEndPoint endPoint = new
IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], 7001);

Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.IP);
try
{
int max = 600000;
socket.Connect(endPoint);
long start = start = DateTime.Now.Ticks; ;
long time1 = 0;
int completed = 0;
for (int i = 0; i <= max; i++)
{
if ((i % 1000) == 0)
Console.WriteLine("Sending {0}", i);
SendLiteMessage(socket, i);
completed++;
time1 = DateTime.Now.Ticks;
if (((time1 - start) / 10000) 1000)
{
Console.WriteLine("Sent {0} in {1}(ms)", completed,
((time1 - start) / 10000));
start = DateTime.Now.Ticks;
completed = 0;
}
}
}

catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}

private void SendLiteMessage(Socket socket, int count)
{
byte[] bas = new byte[2048];
bas[0] = (byte)count;

long start = start = DateTime.Now.Ticks; ;
socket.Send(bas, SocketFlags.None);
long time1 = DateTime.Now.Ticks;
//Console.WriteLine("Socket Send took {0}", (time1 - start));
}

}

}

Run Receive and then Send. Compare the received about with Example 1 you
will see example 2 will be quicker.

Let me know your outcome.

Thanks,
Oct 6 '06 #1
0 3113

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
5
by: kuba bogaczewicz | last post by:
Hello all, for my school project I have to write a small peer-2-peer application using Sockets, and I've chosen C# for the task. I've been doing some research on the topic, and I would really...
2
by: Nuno Magalhaes | last post by:
How to check for a closed http socket without losing any data? (MSDN says to send something to server). Somewhere in my code (after sending the http header in plain text) I'm doing a loop with:...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
2
by: linuxfedora | last post by:
What is the best way to write the following application: The Main GUI thread will try to send packet to other side in the network, it could be very slow network or very fast network. How can i...
2
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.