Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Send Data to remote machine using IPV6/IPV4 based address

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
29 Apr 2014CPOL1 min read 17.8K   615   10   4
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

Image 1

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.

Image 2

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

Image 3

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

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIf clients and server will not on same network. i mean not on LAN network.. Pin
Tariq Mehmood28-Nov-14 2:25
Tariq Mehmood28-Nov-14 2:25 
AnswerRe: If clients and server will not on same network. i mean not on LAN network.. Pin
munagalasantosh25-May-15 4:10
munagalasantosh25-May-15 4:10 
Questionwhile i am trying to Use your sample,i am getting Error Pin
Tariq Mehmood28-Nov-14 1:36
Tariq Mehmood28-Nov-14 1:36 
AnswerRe: while i am trying to Use your sample,i am getting Error Pin
munagalasantosh25-May-15 4:12
munagalasantosh25-May-15 4:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.