Click here to Skip to main content
15,891,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am newbie in dot net architecture or c# and I want to develop c# application in which udp socket will receive data from other client & store data in file at starting of application and same process should follow same task on button click event in same application!!
Please give any idea for implementing such scenario.
I write below code on formLoad event of application
C#
public void Form1_Load(object sender, EventArgs e)
        {
            IPNetworking getip = new IPNetworking();
            
            try
            {
                CheckForIllegalCrossThreadCalls = false;
                serverSocket = new Socket(AddressFamily.InterNetwork,
                            SocketType.Dgram,ProtocolType.Udp);

                string ipaddr = getip.GetIP4Address();

                IPEndPoint endpt = new IPEndPoint(IPAddress.Parse(ipaddr), 21845);
                
                serverSocket.Bind(endpt);
               

                IPEndPoint ipesender = new IPEndPoint(IPAddress.Parse(ipaddr),0);
                EndPoint epsender = (EndPoint)ipesender;
                serverSocket.BeginReceiveFrom(byteData,0,byteData.Length,SocketFlags.None,
                                                ref epsender, new AsyncCallback(OnReceive),epsender);

                   File.WriteAllBytes("data.txt", byteData);
               
            }
            catch (Exception exce)
            {
                MessageBox.Show(exce.Message, "Server UDP", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
Posted
Updated 7-Nov-13 22:50pm
v3
Comments
Richard MacCutchan 19-Oct-13 8:53am    
If you use UDP then tyou need to include information in each datagram that tells what its sequence number is. UDP datagrams are not guaranteed to be delivered, and if delivered may be received in random order.
Venkat Raghvan 21-Oct-13 3:26am    
ok!!
Richard MacCutchan 8-Nov-13 6:33am    
You have edited your original question, but there is no indication of what extra information you want. Please add details of any supplementary questions you have.

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