I have an hardware ARM control as UDP server and I am communicating to it via the code below written in C#. PC is UDP Client. Server simply echoes the data.
This works fine without any issue and this is stable.
static void Main(string args)
Int32 PORT = 45555;
for (; i < 15; )
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
IPAddress broadcast = IPAddress.Parse("192.168.0.10");
byte sendbuf = Encoding.ASCII.GetBytes("Shriganesh Damle, Infineon Techonologies India Pvt Ltd, Bangalore");
IPEndPoint ep = new IPEndPoint(broadcast, PORT);
//Creates a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient(PORT);
Console.WriteLine("Message sent to the broadcast address");
//Creates an IPEndPoint to record the IP Address and port number of the sender.
// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks until a message returns on this socket from a remote host.
Byte receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("This is the message you received " +
Console.WriteLine("This message was sent from " +
" on their port number " +
Simple udp client
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define SERVER "192.168.0.10" //ip address of udp server
#define BUFLEN 512 //Max length of buffer
#define PORT 45555 //The port on which to listen for incoming data
struct sockaddr_in si_other;
int s, slen=sizeof(si_other);
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
printf("Failed. Error Code : %d",WSAGetLastError());
if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
printf("socket() failed with error code : %d" , WSAGetLastError());
//setup address structure
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
printf("Enter message : ");
//send the message
if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
printf("sendto() failed with error code : %d" , WSAGetLastError());
//receive a reply and print it
//clear the buffer by filling null, it might have previously received data
//try to receive some data, this is a blocking call
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
printf("recvfrom() failed with error code : %d" , WSAGetLastError());
If you want to receive data on a socket, you need to bind it to one or more local addresses and a local port. Your C# version achieves this via
new UdpClient(PORT) (note the
PORT argument), but your C version does not do anything comparable.
After creating the socket, do
sockaddr_in localAddr; localAddr.sin_family = AF_INET; localAddr.sin_port = htons(PORT); localAddr.sin_addr.S_un.S_addr = INADDR_ANY; bind(s, &localAddr, sizeof(sockaddr_in));
and verify that it returns
0 to indicate success.
Alternatively, the C version would be more parallel to the C# version if you created a second socket for receiving the server response. I don't think it's necessary to do that in C (in Winsock2, to be more precise), but it is possible that using the same socket to both send and receive would require you to bind to a specific local address instead of to