Raising an event after Socket.Receive call

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

Guest

Hi

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

private void WaitForConnection() {
TcpListener listener = new TcpListener(IPAddress.Any, 1234);
Stream data = null;
Socket socket = null;

listener.Start();
socket = listener.AcceptSocket();

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

OnMessageReceived(this, new MessageReceivedEventArgs(data));
}
}
catch {
throw;
}
finally {
if(data != null) data.Close();
if(socket != null) socket.Close();
}
}

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

Craig
 
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.MessageReceived += new
MessageReceivedEventHandler(ProcessMessage);
}
catch(Exception exception) {
Console.Write(exception.Message);
}
}

public static void ProcessMessage(object sender, MessageReceivedEventArgs
e) {
StreamReader reader = new System.IO.StreamReader(e.Data);
Console.Write(reader.ReadToEnd());
}
}


// Project 2
public delegate void MessageReceivedEventHandler(object sender,
MessageReceivedEventArgs e);

public class PortWatcher {
private event EventHandler Initialize;

private PortWatcher() {
Initialize += new EventHandler(OnInitialize);
OnIntialize(this, new EventArgs());
}

private void WaitForConnection() {
TcpListener listener = new TcpListener(IPAddress.Any, 1234);
Stream data = null;
Socket socket = null;

listener.Start();
socket = listener.AcceptSocket();

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

public void OnInitialize(object sender, EventArgs e) {
WaitForConnection();
}

public void OnMessageReceivd(object sender, MessageReceivedEventArgs e) {
if(this.MessageReceived != null) {
this.MessageReceived(this, e);
}
}
}

public class MessageReceivedEventArgs : EventArgs {
Stream m_data = null;

public MessageReceivedEventArgs(Stream data) {
this.m_data = data;
}

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

Ashish Das said:
can you post a working code.



Craig said:
Hi

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

private void WaitForConnection() {
TcpListener listener = new TcpListener(IPAddress.Any, 1234);
Stream data = null;
Socket socket = null;

listener.Start();
socket = listener.AcceptSocket();

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

OnMessageReceived(this, new MessageReceivedEventArgs(data));
}
}
catch {
throw;
}
finally {
if(data != null) data.Close();
if(socket != null) socket.Close();
}
}

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

Craig
 
Back
Top