Newbie error on socket.....

R

roberto

Hi,well i'm trying to listen from a network card the traffic incoming.
I tried about hundred times :-( in different ways after reading many
previous posts...but i'm always getting errors.
This is my last piece of code NOT working:


private void button1_Click(object sender, System.EventArgs e)
{
Socket mysock = new
Socket(AddressFamily.InterNetwork,SocketType.Dgram,
ProtocolType.Udp);
IPEndPoint iep = new
IPEndPoint(IPAddress.Parse("239.0.4.17"),9000);//This is port i listen
from
// mysock.Bind(iep);->>>>>>>>ERROR IF i Bind


mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembership,
new MulticastOption(IPAddress.Parse("192.168.238.238")));//--->>>>>>>This
is ip of my sat card

byte[] mydata = new Byte[1024];
int n = mysock.Receive
(mydata,mydata.Length,SocketFlags.None);->>>>>HERE Exception 10022
Invalid argument so never tested next lines.....

String mystring = System.Text.Encoding.ASCII.GetString(mydata);
TextBox1.AppendText(mystring);

}
What I'd like to do is to read incoming datagrams from that port only
by that network card,parse them to extract data i want and show them.
Thanks to anyone who could help me.
 
B

BMermuys

Hi,
inline

roberto said:
Hi,well i'm trying to listen from a network card the traffic incoming.
I tried about hundred times :-( in different ways after reading many
previous posts...but i'm always getting errors.
This is my last piece of code NOT working:


private void button1_Click(object sender, System.EventArgs e)
{
Socket mysock = new Socket( AddressFamily.InterNetwork,SocketType.Dgram,
ProtocolType.Udp );
int mcastPort = 9000;
IPAddress mcastIP = IPAddress.Parse( "239.0.4.17" );
IPAddress localIP = IPAddress.Parse( "192.168.238.238" );
EndPoint localEnd = (EndPoint)new IPEndPoint( localIP, mcastPort );

mysock.Bind( localEnd ); // always bind to a local ip/port

mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembership,
new MulticastOption(mcastIP, localIP) );
byte[] mydata = new Byte[1024];
int n = mysock.Receive (mydata,mydata.Length,SocketFlags.None);->>>>>HERE Exception 10022
Invalid argument so never tested next lines.....

String mystring = System.Text.Encoding.ASCII.GetString(mydata);
TextBox1.AppendText(mystring);

}

HTH,
greetings


What I'd like to do is to read incoming datagrams from that port only
by that network card,parse them to extract data i want and show them.
Thanks to anyone who could help me.
 
R

robert0

Hi,
inline

roberto said:
Hi,well i'm trying to listen from a network card the traffic incoming.
I tried about hundred times :-( in different ways after reading many
previous posts...but i'm always getting errors.
This is my last piece of code NOT working:


private void button1_Click(object sender, System.EventArgs e)
{
Socket mysock = new Socket( AddressFamily.InterNetwork,SocketType.Dgram,
ProtocolType.Udp );
int mcastPort = 9000;
IPAddress mcastIP = IPAddress.Parse( "239.0.4.17" );
IPAddress localIP = IPAddress.Parse( "192.168.238.238" );
EndPoint localEnd = (EndPoint)new IPEndPoint( localIP, mcastPort );

mysock.Bind( localEnd ); // always bind to a local ip/port

mysock.SetSocketOption(
SocketOptionLevel.IP,SocketOptionName.AddMembership,
new MulticastOption(mcastIP, localIP) );
byte[] mydata = new Byte[1024];
int n = mysock.Receive
(mydata,mydata.Length,SocketFlags.None);->>>>>HERE
Exception 10022
Invalid argument so never tested next lines.....

String mystring = System.Text.Encoding.ASCII.GetString(mydata);
TextBox1.AppendText(mystring);

}

HTH,
greetings


What I'd like to do is to read incoming datagrams from that port only
by that network card,parse them to extract data i want and show them.
Thanks to anyone who could help me.


Thanks a lot , now code is receiving but i'm gettting another problem to
show data...if i run the code in console mode everything is fine , i can see
the data on screen. If i run the application in windows mode(c# application
as i use borland) nothing appears, it seems mystring is void.
If i try
textBox1.appendText("Begin Message");
textbox1.appendText(mystring)";
textbox1.appendText("End Message");
all i can see in my textbox is....Begin MessageEnd Message.

is there something i miss in using?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;

Thanks again for your useful help.
 

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

Top