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
- IPV6
- IPV4
- 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.
- first textbox for IP address
- second text box for Port
- for message.
- 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
{
IPEndPoint ipeh = new IPEndPoint(IPAddress.Parse(ipaddress), Convert.ToInt32(port));
Socket connection;
if (isIPv6Address) {
connection = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
}
else
{ connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
connection.Connect(ipeh);
byte[] messageinbytes = System.Text.ASCIIEncoding.ASCII.GetBytes(msg);
connection.Send(messageinbytes);
Thread.Sleep(600);
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.