Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Send Data to remote machine using IPV6/IPV4 based address

0.00/5 (No votes)
29 Apr 2014 1  
send data to other system using two different ways, one way using IPV6 and other way is IPV4.

Introduction

Receive IPV6 or IPV4 based Data Using C# in this article I explained how to receive IP6 or IP4 data. Now i am going to explain how to send data using IPv6 or IPv4 address.

IPV4 address is a 32 bit numeric address is written in decimal ad four numbers separated by dot. for example : 1.67.87.33.

IPV6 address is a 128 bit written in hexadecimal and separated by colon. for example : fe80:cft7:b16:2817:b24a:1dd7

TO Know more about IPv6 and IPv4. Please refer following Links

  1. IPV6
  2. IPV4
  3. IPv6 vs IPv

Below screen explains about how to send data using IPV4 address

In our sample i am using port 95 to send and receive data between machines. To send data i developed a WPF application. Main screen contains three text box's and one button.

  1. first textbox for IP address
  2. second text box for Port
  3. for message.
  4. button used to accept text boxs and send data to remove machine.

Below screen explain about send data via IPV6 address.

Below is output screen of receiving TCP based data reference(Receive IPV6 or IPV4 based Data Using C#)

Using the code

Below code is the main login to send data to remove machines using IPV6 or IPV4 based data. I used System.Scocket.System.Net.Sockets

 
  try
            {
                //Preparing/Converting String based ipaddress+port to  Class IPEndPoint
                
                IPEndPoint ipeh = new IPEndPoint(IPAddress.Parse(ipaddress), Convert.ToInt32(port));

                Socket connection;
                if (isIPv6Address) // USED IPV6 Address to send data
                {
                    //Creating object to socket based of type of the IP address used.
                    connection = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);

                }
                else
                {// USED IPV4 Address to send data
                    //Creating object to socket based of type of the IP address used.
                    connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                }
                //Establish a connection with remove machine
                connection.Connect(ipeh);
                //Convert string based message into bytes format.
                byte[] messageinbytes = System.Text.ASCIIEncoding.ASCII.GetBytes(msg);
                //Send bytes over the network.
                connection.Send(messageinbytes);
                Thread.Sleep(600);
                //Close the scocket connection.
                connection.Close();

            }
            catch (Exception ex)
            { 
                throw (ex);
            }

In the above code i used AddressFamily.InterNetwork parameter while create Socket. AddressFamily.InterNetwork means IPv4 based address. where AddressFamily.InterNetworkV6 means IPv6 based address.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here