Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code in client.cs
C#
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;
}


code in server.cs
C#
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);
Posted
Updated 28-Oct-13 2:47am
v3
Comments
ZurdoDev 28-Oct-13 8:43am    
Write code to listen for something specific from the server.
teja sai ravi 28-Oct-13 8:50am    
thanks for ur reply...
iam listening from server but if any updates done in server i am not updating automatically when i refresh my client page iam then only iam getting new updates
ZurdoDev 28-Oct-13 8:56am    
I don't understand.
teja sai ravi 30-Oct-13 0:34am    
hi sir,
how can i push message by server if any updation is done on server side to client
ZurdoDev 30-Oct-13 7:13am    
You control both the server and client code, right? So, what's the issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900