Click here to Skip to main content
15,887,371 members
Home / Discussions / C#
   

C#

 
GeneralRe: Coding error missing reference Pin
uighgg ojgeojge31-Mar-18 2:34
uighgg ojgeojge31-Mar-18 2:34 
GeneralRe: Coding error missing reference Pin
Dave Kreskowiak31-Mar-18 8:36
mveDave Kreskowiak31-Mar-18 8:36 
GeneralRe: Coding error missing reference Pin
Pete O'Hanlon31-Mar-18 10:12
mvePete O'Hanlon31-Mar-18 10:12 
QuestionProblem with Rotating Platforms in Unity 3D Pin
O.G.I.30-Mar-18 8:06
O.G.I.30-Mar-18 8:06 
AnswerRe: Problem with Rotating Platforms in Unity 3D Pin
Eddy Vluggen30-Mar-18 22:48
professionalEddy Vluggen30-Mar-18 22:48 
GeneralRe: Problem with Rotating Platforms in Unity 3D Pin
O.G.I.30-Mar-18 23:51
O.G.I.30-Mar-18 23:51 
GeneralRe: Problem with Rotating Platforms in Unity 3D Pin
Eddy Vluggen31-Mar-18 3:10
professionalEddy Vluggen31-Mar-18 3:10 
QuestionIPv6 based UDP client which can communicate with 3rd Part IPv6 based UDP Server Pin
SKant0330-Mar-18 0:15
SKant0330-Mar-18 0:15 
Hello Members,

I don't know much about C# and .net framework.
I am working on a project where I need to communicate with IPv6 based UDP Server and I have to use NI labVIEW software to implement the GUI and code but NI labVIEW doesn't support IPv6 protocol .

I have implemented the UDP class in C# and created DLL to resolve my problem but I am unable to read the data from server. The send method is working perfect but not able to received data from server.

Below Error Message received during read the data from :
System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
at ENET_API.ENET_UDP.read(String ipaddr, Int32 portno, Int32 length)



Can anyone please guide me why I am getting above error and why I am unable to received the data from serve?

I have attached here my below source code implemented on C Sharp.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace ENET_API
{
public class ENET_UDP
{
private string _IPAddr = "";
public string IPAddr
{
get
{
return _IPAddr;
}
set
{
this._IPAddr = value;
}

}

private bool semaphoreTx = false;

private int _PortAddr = 0;
public int PortAddr
{
get
{
return _PortAddr;
}
set
{
this._PortAddr = value;
}

}

private string[,] _ENET_Transmit;

public string[,] ENET_Transmit
{
get
{
return _ENET_Transmit;
}
set
{
this._ENET_Transmit = value;
}
}

private string _ENET_UDPSend = "";

public string ENET_UDPSend
{
get
{
return _ENET_UDPSend;
}
set
{
this._ENET_UDPSend = value;
}
}

private int _XL_WriteVal = 0;

public int XL_WriteVal
{
get
{
return _XL_WriteVal;
}
set
{
this._XL_WriteVal = value;
}
}

private long _TxLoopElaspsed;

private bool _format = false;
public bool format
{
set
{
this._format = value;
}
get
{
return _format;
}
}

public long TxLoopElaspsed
{
get
{
return _TxLoopElaspsed;
}
set
{
this._TxLoopElaspsed = value;
}
}

private Boolean _stopTx = false;

public Boolean stopTx
{
get
{
return _stopTx;
}
set
{
this._stopTx = value;
}
}

private IPEndPoint LocalEndPoint;

private IPEndPoint RemoteEndPoint;

public UdpClient open()
{
RemoteEndPoint = new IPEndPoint(IPAddress.Parse(_IPAddr), _PortAddr);
LocalEndPoint = new IPEndPoint(IPAddress.IPv6Any, 50021);
System.Net.Sockets.UdpClient sock;
if (Regex.IsMatch(_IPAddr, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") == true)
{
sock = new System.Net.Sockets.UdpClient(AddressFamily.InterNetwork);
}
else
{
sock = new System.Net.Sockets.UdpClient(AddressFamily.InterNetworkV6);
}
sock.ExclusiveAddressUse = false;
sock.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sock.Client.Bind(LocalEndPoint);
sock.Connect(RemoteEndPoint);
return sock;
}

public void close(UdpClient udpsock)
{
udpsock.Close();
}

//public void write(UdpClient udpsock, string Data)
public void write(UdpClient sock, string Data)
{
while (semaphoreTx == true)
{
}
semaphoreTx = true;
if (Data != "")
{

byte[] data;
if (_format != true)
{
data = new byte[Data.Length / 2];
for (int i = 0; i < Data.Length; i += 2)
{
data[i / 2] = Convert.ToByte(Data.Substring(i, 2), 16);
}
}
else
{
data = Encoding.ASCII.GetBytes(Data);
} try
{
//sock.Send(data, data.Length, LocalEndPoint);
sock.Send(data, data.Length);
Data = "";
}
catch
{
}
}
semaphoreTx = false;
}

private void TxLoop(UdpClient sock)
{
var LoopTime = System.Diagnostics.Stopwatch.StartNew();
int j = 1;
while (this._stopTx == false)
{
while (semaphoreTx == true)
{
}
semaphoreTx = true;

if (this._ENET_UDPSend != "")
{
byte[] data;
if (_format != true)
{
data = new byte[this._ENET_UDPSend.Length / 2];
for (int i = 0; i < this._ENET_UDPSend.Length; i += 2)
{
data[i / 2] = Convert.ToByte(this._ENET_UDPSend.Substring(i, 2), 16);
}
}
else
{
data = Encoding.ASCII.GetBytes(this._ENET_UDPSend);
}
try
{
//udpsock.Send(data, data.Length, LocalEndPoint);
sock.Send(data, data.Length);
this._ENET_UDPSend = "";
}
catch //(Exception e)
{

}
}

for (int i = 0; i < this._ENET_Transmit.Length / 4; i++)
{
if ((this._ENET_Transmit.GetValue(i, 0).ToString() == "1") && ((j * 10) % Convert.ToInt32(this._ENET_Transmit.GetValue(i, 1).ToString()) == 0))
{
byte[] data;
if (_format != true)
{
data = new byte[this._ENET_Transmit.GetValue(i, 2).ToString().Length / 2];
for (int k = 0; k < this._ENET_Transmit.GetValue(i, 2).ToString().Length; k += 2)
{
data[k / 2] = Convert.ToByte(this._ENET_Transmit.GetValue(i, 2).ToString().Substring(k, 2), 16);
}
}
else
{
data = Encoding.ASCII.GetBytes(this._ENET_Transmit.GetValue(i, 2).ToString());
}
try
{
//sock.Send(data, data.Length, LocalEndPoint);
sock.Send(data, data.Length);
}
catch
{
}
}

}

semaphoreTx = false;

while (LoopTime.ElapsedMilliseconds < 10)
{

}

j += 1;

if (j > 360000) // 3600 Sec Loop Max Periodicity i.e. 1 Hour
{
j = 1;
}

this._TxLoopElaspsed = LoopTime.ElapsedMilliseconds;
LoopTime.Reset();
LoopTime.Start();
}
LoopTime.Stop();
this._TxLoopElaspsed = 0;
}

public string read(string ipaddr, int portno, int length)
{

IPEndPoint sender;
System.Net.Sockets.UdpClient sock;
if ((ipaddr == "") && (portno == 0))
{
sender = new IPEndPoint(IPAddress.IPv6Any, 0);
}
else if ((ipaddr != "") && (portno == 0))
{
sender = new IPEndPoint(IPAddress.Parse(ipaddr), 0);
}
else if ((ipaddr == "") && (portno != 0))
{
sender = new IPEndPoint(IPAddress.IPv6Any, portno);
}
else
{
sender = new IPEndPoint(IPAddress.Parse(ipaddr), portno);
}

System.Net.Sockets.UdpClient sock;
if (Regex.IsMatch(_IPAddr, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") == true)
{
sock = new System.Net.Sockets.UdpClient(AddressFamily.InterNetwork);
}
else
{
sock = new System.Net.Sockets.UdpClient(AddressFamily.InterNetworkV6);
}

sock.ExclusiveAddressUse = false;
sock.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sock.Client.Bind(LocalEndPoint);
sock.Connect(sender);


int len;
if (length != 0)
{
len = length;
}
else
{
len = sock.Available;
}
string stringData = "";

if (len != 0)
{
var LoopTime = System.Diagnostics.Stopwatch.StartNew();
byte[] data = new byte[len];


int RxTimeout = 2000;
sock.Client.ReceiveTimeout = RxTimeout;

while (LoopTime.ElapsedMilliseconds < RxTimeout)

{
try
{

data = sock.Receive(ref sender);

stringData += BitConverter.ToString(data).Replace("-", "");

}
catch (Exception e)
{
stringData = e.ToString();
}
}
//stringData = "@" + sender.Address.ToString() + "(" + Encoding.ASCII.GetString(data, 0, data.Length) + ")";
LoopTime.Stop();
}
sock.Close();
return stringData;
}

public void StartTx(UdpClient udpsock)
{
this._stopTx = false;
Thread Thd = new Thread(() => TxLoop(udpsock));
Thd.IsBackground = true;
Thd.Start();
}
AnswerRe: IPv6 based UDP client which can communicate with 3rd Part IPv6 based UDP Server Pin
Gerry Schmitz31-Mar-18 6:50
mveGerry Schmitz31-Mar-18 6:50 
QuestionHow to fix this? Please see my code if what is the solution for 'AssetService.InsertPulloutReturnItems(string, string, string, string)': not all code paths return value.. Thanks Pin
arthur tarrayo29-Mar-18 21:05
arthur tarrayo29-Mar-18 21:05 
AnswerRe: How to fix this? Please see my code if what is the solution for 'AssetService.InsertPulloutReturnItems(string, string, string, string)': not all code paths return value.. Thanks Pin
OriginalGriff29-Mar-18 21:23
mveOriginalGriff29-Mar-18 21:23 
QuestionBuilding custom controls from common wpf controls Pin
Dwayne Barsotta29-Mar-18 2:37
Dwayne Barsotta29-Mar-18 2:37 
AnswerRe: Building custom controls from common wpf controls Pin
Gerry Schmitz29-Mar-18 6:43
mveGerry Schmitz29-Mar-18 6:43 
GeneralRe: Building custom controls from common wpf controls Pin
BillWoodruff30-Mar-18 4:14
professionalBillWoodruff30-Mar-18 4:14 
GeneralRe: Building custom controls from common wpf controls Pin
Gerry Schmitz30-Mar-18 4:45
mveGerry Schmitz30-Mar-18 4:45 
GeneralRe: Building custom controls from common wpf controls Pin
BillWoodruff30-Mar-18 19:55
professionalBillWoodruff30-Mar-18 19:55 
AnswerRe: Building custom controls from common wpf controls Pin
Mycroft Holmes29-Mar-18 15:19
professionalMycroft Holmes29-Mar-18 15:19 
AnswerRe: Building custom controls from common wpf controls Pin
BillWoodruff30-Mar-18 4:05
professionalBillWoodruff30-Mar-18 4:05 
QuestionWpf frameworks or styles Pin
Dwayne Barsotta29-Mar-18 2:24
Dwayne Barsotta29-Mar-18 2:24 
AnswerRe: Wpf frameworks or styles Pin
Pete O'Hanlon29-Mar-18 2:52
mvePete O'Hanlon29-Mar-18 2:52 
AnswerRe: Wpf frameworks or styles Pin
Gerry Schmitz29-Mar-18 6:55
mveGerry Schmitz29-Mar-18 6:55 
QuestionHow to print a recording Pin
ago248628-Mar-18 23:31
ago248628-Mar-18 23:31 
AnswerRe: How to print a recording Pin
OriginalGriff28-Mar-18 23:36
mveOriginalGriff28-Mar-18 23:36 
GeneralRe: How to print a recording Pin
ago248628-Mar-18 23:41
ago248628-Mar-18 23:41 
GeneralRe: How to print a recording Pin
OriginalGriff28-Mar-18 23:49
mveOriginalGriff28-Mar-18 23:49 

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.