473,785 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Raising an event after Socket.Receive call

Hi

I listen on a port, when data is received I raise an event
(OnMessageRecei ved) in the while loop as follows:

private void WaitForConnecti on() {
TcpListener listener = new TcpListener(IPA ddress.Any, 1234);
Stream data = null;
Socket socket = null;

listener.Start( );
socket = listener.Accept Socket();

try {
while(true) {
byte[] buffer = new byte[this.m_buffer];
int byteSize = 0;
byteSize = socket.Receive( buffer);
data = new MemoryStream(bu ffer, 0, byteSize);

OnMessageReceiv ed(this, new MessageReceived EventArgs(data) );
}
}
catch {
throw;
}
finally {
if(data != null) data.Close();
if(socket != null) socket.Close();
}
}

The delegate assigned to the OnMessageReceiv ed doesn't receive the event
notification, no exception is thrown in the WaitForConnecti on method. Does
anyone have any suggestions as to why?

Craig
Nov 16 '05 #1
2 10121
can you post a working code.

"Craig" <Cr***@discussi ons.microsoft.c om> wrote in message
news:3C******** *************** ***********@mic rosoft.com...
Hi

I listen on a port, when data is received I raise an event
(OnMessageRecei ved) in the while loop as follows:

private void WaitForConnecti on() {
TcpListener listener = new TcpListener(IPA ddress.Any, 1234);
Stream data = null;
Socket socket = null;

listener.Start( );
socket = listener.Accept Socket();

try {
while(true) {
byte[] buffer = new byte[this.m_buffer];
int byteSize = 0;
byteSize = socket.Receive( buffer);
data = new MemoryStream(bu ffer, 0, byteSize);

OnMessageReceiv ed(this, new MessageReceived EventArgs(data) );
}
}
catch {
throw;
}
finally {
if(data != null) data.Close();
if(socket != null) socket.Close();
}
}

The delegate assigned to the OnMessageReceiv ed doesn't receive the event
notification, no exception is thrown in the WaitForConnecti on method.
Does
anyone have any suggestions as to why?

Craig

Nov 16 '05 #2
Hi Ashish

Here is a sample you can work with:

// Project 1
public class Watcher {
public static void Main() {
try {
PortWatcher watcher = new PortWatcher();
watcher.Message Received += new
MessageReceived EventHandler(Pr ocessMessage);
}
catch(Exception exception) {
Console.Write(e xception.Messag e);
}
}

public static void ProcessMessage( object sender, MessageReceived EventArgs
e) {
StreamReader reader = new System.IO.Strea mReader(e.Data) ;
Console.Write(r eader.ReadToEnd ());
}
}
// Project 2
public delegate void MessageReceived EventHandler(ob ject sender,
MessageReceived EventArgs e);

public class PortWatcher {
private event EventHandler Initialize;

private PortWatcher() {
Initialize += new EventHandler(On Initialize);
OnIntialize(thi s, new EventArgs());
}

private void WaitForConnecti on() {
TcpListener listener = new TcpListener(IPA ddress.Any, 1234);
Stream data = null;
Socket socket = null;

listener.Start( );
socket = listener.Accept Socket();

try {
while(true) {
byte[] buffer = new byte[this.m_buffer];
int byteSize = 0;
byteSize = socket.Receive( buffer);
data = new MemoryStream(bu ffer, 0, byteSize);
OnMessageReceiv ed(this, new MessageReceived EventArgs(data) );
}
catch {
throw;
}
finally {
if(data != null) data.Close();
if(socket != null) socket.Close();
}
}

public void OnInitialize(ob ject sender, EventArgs e) {
WaitForConnecti on();
}

public void OnMessageReceiv d(object sender, MessageReceived EventArgs e) {
if(this.Message Received != null) {
this.MessageRec eived(this, e);
}
}
}

public class MessageReceived EventArgs : EventArgs {
Stream m_data = null;

public MessageReceived EventArgs(Strea m data) {
this.m_data = data;
}

public Stream Data {
get {
return m_data;
}
}
}

"Ashish Das" wrote:
can you post a working code.

"Craig" <Cr***@discussi ons.microsoft.c om> wrote in message
news:3C******** *************** ***********@mic rosoft.com...
Hi

I listen on a port, when data is received I raise an event
(OnMessageRecei ved) in the while loop as follows:

private void WaitForConnecti on() {
TcpListener listener = new TcpListener(IPA ddress.Any, 1234);
Stream data = null;
Socket socket = null;

listener.Start( );
socket = listener.Accept Socket();

try {
while(true) {
byte[] buffer = new byte[this.m_buffer];
int byteSize = 0;
byteSize = socket.Receive( buffer);
data = new MemoryStream(bu ffer, 0, byteSize);

OnMessageReceiv ed(this, new MessageReceived EventArgs(data) );
}
}
catch {
throw;
}
finally {
if(data != null) data.Close();
if(socket != null) socket.Close();
}
}

The delegate assigned to the OnMessageReceiv ed doesn't receive the event
notification, no exception is thrown in the WaitForConnecti on method.
Does
anyone have any suggestions as to why?

Craig


Nov 16 '05 #3

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

Similar topics

8
6088
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
3
5134
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library. When i try to connect to a server that doesnt exist it raises a "Connection forcibly rejected by the resmote host" SocketException.
4
18135
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.
6
2883
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form,...
5
11726
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of 1024 with all values set to Null arrive. Other than examine a block of 1024 to see if the entire block is null, is there any other way to determine if , say a chat message "Hi Charlie" has been received completely?
2
4158
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send I get the last chunk of data and am not able to retrieve it anymore cause the socket will be dead. Loop: { socket.Receive <----------- data arrives
2
15334
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with TCP socket. This propery is also listed as 'new in .net 2.0'. 1) is the socket.RecieveTimeout...
2
1389
by: =?Utf-8?B?Um9i?= | last post by:
I'm new to event processing in vs 2005 - what I'm trying to achieve is broadly a network simulation of two workstations sending packets to each other . I have defined a class 'Workstation' and I was hoping to define events where in instance A (or workstation) I could raise an event 'Send' (which takes in the destination instance B (or workstation) and the Packet to send). The event would then trigger a 'Receive' event in instance B to...
22
1416
by: Oriane | last post by:
Hi there, I have to write an algorithm in .NET/C# which perform some task but has to be stopped after an intreval of dT seconds. Something like: dT = 30sec; for (;;) { t0 = GetCurrentTime (); for (i=0;...) { perform some operations;
0
9480
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
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
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
9950
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
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
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
3
2879
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.