protected void Page_Load(object sender, EventArgs e) { // IPAddress serverIP = IPAddress.Parse("192.168.0.5"); // Server IP IPAddress serverIP = IPAddress.Broadcast; // Server IP port = 10005 // Server port IPEndPoint ipEndPoint = new IPEndPoint(serverIP, port); string response = SendMessageToServer("hello server, this is Sai Teja", ipEndPoint); // Send the message to the server Response.Write(response); } private string SendMessageToServer(string message, IPEndPoint serverAddress) { string serverResponse = string.Empty; // The variable which we will use to store the server response using (UdpClient client = new UdpClient()) { byte[] data = Encoding.UTF8.GetBytes(message); // Convert our message to a byte array client.Send(data, data.Length, serverAddress); // Send the date to the server serverResponse = Encoding.UTF8.GetString(client.Receive(ref serverAddress)); // Retrieve the response from server as byte array and convert it to string } return serverResponse; }
int port = 10009; UdpClient udpListener = new UdpClient(port); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Broadcast, port); byte[] receivedBytes = udpListener.Receive(ref ipEndPoint); // Receive the information from the client as byte array string clientMessage = Encoding.UTF8.GetString(receivedBytes); // Convert the message to a string byte[] response = Encoding.UTF8.GetBytes("Hello client, this is the server"); // Convert the reponse we want to send to the client to byte array udpListener.Send(response, response.Length, ipEndPoint);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)