473,721 Members | 3,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket.Send - Sending message to the same process

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

Oct 5 '06 #1
3 4308
Hello, BuddyWork!

BCould someone please explain why the Socket.Send is slow to send to
Bthe same
Bprocess it sending from. Eg. Process1 calls Socket.Send which sends
Bto the
Bsame IP address and port, the receiver is running within Process1.

BIf 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
Oct 5 '06 #2


"Vadym Stetsyak" wrote:
Hello, BuddyWork!

BCould someone please explain why the Socket.Send is slow to send to
Bthe same
Bprocess it sending from. Eg. Process1 calls Socket.Send which sends
Bto the
Bsame IP address and port, the receiver is running within Process1.

BIf 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
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(AddressF amily.InterNetw ork, SocketType.Stre am,
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(myA ddress, 7001);
TcpClient client = listener.Accept TcpClient();
client.ReceiveB ufferSize = 32768;
NetworkStream ns = client.GetStrea m();
ns.Read(...)

Thanks,

Oct 5 '06 #3
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.Collecti ons.Generic;
using System.Net;
using System.Net.Sock ets;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.Text;
using System.Threadin g;

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

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

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

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

Socket socket = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am,
ProtocolType.IP );
try
{
int max = 600000;
socket.Connect( endPoint);
long start = start = DateTime.Now.Ti cks; ;
long time1 = 0;
int completed = 0;
for (int i = 0; i <= max; i++)
{
if ((i % 1000) == 0)
Console.WriteLi ne("Sending {0}", i);
SendLiteMessage (socket, i);
completed++;
time1 = DateTime.Now.Ti cks;
if (((time1 - start) / 10000) 1000)
{
Console.WriteLi ne("Sent {0} in {1}(ms)", completed,
((time1 - start) / 10000));
start = DateTime.Now.Ti cks;
completed = 0;
}
}
}

catch (Exception ex)
{
Console.WriteLi ne(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.Ti cks; ;
socket.Send(bas , SocketFlags.Non e);
long time1 = DateTime.Now.Ti cks;
//Console.WriteLi ne("Socket Send took {0}", (time1 - start));
}

}

class SocketReceive
{
private object ReceiveMessageL ite(NetworkStre am 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. GetHostAddresse s(Dns.GetHostNa me())[0], 7001);
try
{
Console.WriteLi ne("Waiting for Sender...");
TcpListener listener = new TcpListener(end Point.Address,
7001);

listener.Start( );
TcpClient client = listener.Accept TcpClient();
client.ReceiveB ufferSize = 32768;
NetworkStream ns = client.GetStrea m();
long start = start = DateTime.Now.Ti cks; ;
long time1 = 0;
int completed = 0;
for (int i = 0; i < 600000; i++)
{
if ((i % 1000) == 0)
Console.WriteLi ne("Receiving {0}", i);

object obj = ReceiveMessageL ite(ns, i);
completed++;
time1 = DateTime.Now.Ti cks;
if (((time1 - start) / 10000) 1000)
{
Console.WriteLi ne("Received {0} in {1}(ms)",
completed, ((time1 - start) / 10000));
start = DateTime.Now.Ti cks;
completed = 0;
}
}
}
catch (Exception ex)
{
Console.WriteLi ne(ex.Message);
}
finally
{
}
}
}
}

Example 2. Put this code in a console app called Receive
using System;
using System.Collecti ons.Generic;
using System.Net;
using System.Net.Sock ets;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.Text;
using System.Threadin g;

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

while (tsr.IsAlive)
{
Thread.Sleep(10 0);
}
}
}

class SocketReceive
{
private object ReceiveMessageL ite(NetworkStre am 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. GetHostAddresse s(Dns.GetHostNa me())[0], 7001);
try
{
Console.WriteLi ne("Waiting for Sender...");
TcpListener listener = new TcpListener(end Point.Address,
7001);

listener.Start( );
TcpClient client = listener.Accept TcpClient();
client.ReceiveB ufferSize = 32768;
NetworkStream ns = client.GetStrea m();
long start = start = DateTime.Now.Ti cks; ;
long time1 = 0;
int completed = 0;
for (int i = 0; i < 600000; i++)
{
if ((i % 1000) == 0)
Console.WriteLi ne("Receiving {0}", i);

object obj = ReceiveMessageL ite(ns, i);
completed++;
time1 = DateTime.Now.Ti cks;
if (((time1 - start) / 10000) 1000)
{
Console.WriteLi ne("Received {0} in {1}(ms)",
completed, ((time1 - start) / 10000));
start = DateTime.Now.Ti cks;
completed = 0;
}
}
}
catch (Exception ex)
{
Console.WriteLi ne(ex.Message);
}
finally
{
}
}
}
}
Example 2. Put this code in a console app called Send.
using System;
using System.Collecti ons.Generic;
using System.Net;
using System.Net.Sock ets;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.Text;
using System.Threadin g;

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

while (tss.IsAlive)
{
Thread.Sleep(10 0);
}
}
}

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

Socket socket = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am,
ProtocolType.IP );
try
{
int max = 600000;
socket.Connect( endPoint);
long start = start = DateTime.Now.Ti cks; ;
long time1 = 0;
int completed = 0;
for (int i = 0; i <= max; i++)
{
if ((i % 1000) == 0)
Console.WriteLi ne("Sending {0}", i);
SendLiteMessage (socket, i);
completed++;
time1 = DateTime.Now.Ti cks;
if (((time1 - start) / 10000) 1000)
{
Console.WriteLi ne("Sent {0} in {1}(ms)", completed,
((time1 - start) / 10000));
start = DateTime.Now.Ti cks;
completed = 0;
}
}
}

catch (Exception ex)
{
Console.WriteLi ne(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.Ti cks; ;
socket.Send(bas , SocketFlags.Non e);
long time1 = DateTime.Now.Ti cks;
//Console.WriteLi ne("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,

"Vadym Stetsyak" wrote:
Hello, BuddyWork!

BI basically have two threads. One sending 1000 2048kb message over
Bthe
BSocket. The socket has been created by the following code. Please
Bnote the IP
Baddress is my local PC.
BSocket socket = new Socket(AddressF amily.InterNetw ork,
BSocketType.Str eam,
BProtocolType.I P);

BI then have the other thread receiving the message from the same port
Band
Bthe IP address again is my local PC.

BTcpListerner listener = new TcpListener(myA ddress, 7001);
BTcpClient client = listener.Accept TcpClient();
Bclient.Receive BufferSize = 32768;
BNetworkStream ns = client.GetStrea m();
Bns.Read(...)

Since you're using TCP why aren't you crearing
client with ProtocolType.IP . Instead you should use ProtocolType.Tc p

The code sample above doesn't demonstrate how are you actially reading data,
what are buffer sizes on send and receive.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot
Oct 5 '06 #4

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

Similar topics

4
18129
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 as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
5
4492
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 appreciate if someone checked if I got all things right. I am going to use asynchronous connections, and here comes some of my doubts: -> I want to send each message in a scheme: 4 bytes with the size, then actual message. According to MSDN I got...
9
3601
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so that its buffer is bigger than this. I did this expecting the data to be read in one pass.
14
25267
by: DaTurk | last post by:
I am makeing a Multicast server client setup and was wondering what the difference is between Socket.Connect, and Socket.Bind. It may be a stupid question, but I was just curious. Because I tried a test, and I can't bind two sockets to the same port/ip but I can connect with one socket, and bind with another to the same ip/port. Thanks
10
4033
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can anyone please help me with some sample codings and guidance? Can you also suggest some other news group available for socket programming in VB.Net?
0
3166
by: Buddy Home | last post by:
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;
8
4582
by: panko | last post by:
Hello, I can't manage with asynchronous socket communication. :( I wrote a class CSocket.cs. This class is taking care of sending strings to LED display. This display is actually communicating via serial port and serial/ethernet converter (MOXA NE-4100T) with TCP server. So communication is in that way: MyApplication(TCP client)-----------------(TCPServer)MOXA(serial)---(serial)LED Display
2
3671
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 smoothly without any errors but,sometimes I get the following error. "An existing connection was forcibly closed by the remote host." Why is this happening when i am not closing the client program? Could someone guide me in this issue
0
8736
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,...
0
9227
jinu1996
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...
1
9145
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,...
0
9081
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5992
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3206
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
2
2590
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2143
bsmnconsultancy
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...

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.