Socket.Send - Sending message to the same process

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 receiver is running within Process1.

If I move the receiver into Process2 then its fast.

Please can someone explain.

Thanks
 
Hello, BuddyWork!

B> Could someone please explain why the Socket.Send is slow to send to
B> the same
B> process it sending from. Eg. Process1 calls Socket.Send which sends
B> to the
B> same IP address and port, the receiver is running within Process1.

B> If I move the receiver into Process2 then its fast.

'slow' and 'fast' are relative notions.
Can you post code sample where you're observing this
'slow' Send?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Vadym Stetsyak said:
Hello, BuddyWork!

B> Could someone please explain why the Socket.Send is slow to send to
B> the same
B> process it sending from. Eg. Process1 calls Socket.Send which sends
B> to the
B> same IP address and port, the receiver is running within Process1.

B> If I move the receiver into Process2 then its fast.

'slow' and 'fast' are relative notions.
Can you post code sample where you're observing this
'slow' Send?

I basically have two threads. One sending 1000 2048kb message over the
Socket. The socket has been created by the following code. Please note the IP
address is my local PC.
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.IP);

I then have the other thread receiving the message from the same port and
the IP address again is my local PC.

TcpListerner listener = new TcpListener(myAddress, 7001);
TcpClient client = listener.AcceptTcpClient();
client.ReceiveBufferSize = 32768;
NetworkStream ns = client.GetStream();
ns.Read(...)

Thanks,
 
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top