473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Quastion about SetSocketOption & MulticastOption

i creat a group & send a massage 1 line before the method "Recieve" wait for a massage by creating a thread. This code works Perfectly. i just want to know why shouldnt I add this line in the send method too for joining the socket to the group before I try send something to the group.

Expand|Select|Wrap|Line Numbers
  1. server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, MulticastOption);
  2.  
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Threading;
  6. class MAIN
  7. {
  8.     private static MulticastOption CreateGroup()
  9.     {
  10.         return new MulticastOption(IPAddress.Parse("224.100.0.1"));
  11.     }
  12.     private static void Receive(MulticastOption MulticastOption)
  13.     {
  14.         Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  15.         IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
  16.         EndPoint ep = (EndPoint)iep;
  17.         sock.Bind(iep);
  18.         sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, MulticastOption);
  19.         //
  20.         byte[] data = new byte[1024];
  21.         new Thread(new ThreadStart(Send)).Start();
  22.         int recv = sock.ReceiveFrom(data, ref ep);
  23.         String stringData = Encoding.ASCII.GetString(data, 0, recv);
  24.         Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
  25.         sock.Close();
  26.     }
  27.     private static void Send()
  28.     {
  29.         Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  30.         IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050);
  31.  
  32.  
  33.         byte[] data = Encoding.ASCII.GetBytes("This is a test message");
  34.         server.SendTo(data, iep);
  35.         server.Close();
  36.     }
  37.     public static void Main(String[] args)
  38.     {
  39.         Receive(CreateGroup());
  40.         Console.ReadKey();
  41.     }
  42. }
Jun 20 '11 #1
1 2791
Plater
7,872 Expert 4TB
Are you runnin the code on the same PC?
If you notice the option is .IP and not .Socket, which means that all sockets you create will be joined. (Pretty sure on that, I could be wrong though).
So the first time it is called, all your future sockets will be joined.
Jun 20 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Nathan Kerr | last post by:
I have been working with KB Article 812404 "HOW TO: Write Pluggable Protocol to Supprot FTP in Managed Classes by Using Visual Bassic.NET". I have modified the code to run on a Pocket PC. In the...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
2
by: Wencheng Magiya | last post by:
By setting socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAlive, 1 ) can tell the socket to use keep-alives. But anybody know how to set the timeout for the keep-alive...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
3
by: Ryan Liu | last post by:
Hi, I use Server: Use an endless thread to lisiten to clients requests: while(true) { TcpClient client = myListener.AcceptTcpClient();
0
by: morgan22 | last post by:
Hi I'm trying to build a sniffer-like application and I'm using this code : Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw,...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
1
by: =?Utf-8?B?aXdkdTE1?= | last post by:
hi, im trying to send a broadcast out to my network using a UDP socket. heres how i initialize it. Dim soc As New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Udp) i get...
1
by: santoo | last post by:
Hi Guys, I am using Socket for UDP communication in C#. My application send receive data using Socket. While sending data, I am sending data on same port where I am receiving it, Other server...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.