Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to execute Class without Main? Pin
Christian Graus14-Mar-09 8:18
protectorChristian Graus14-Mar-09 8:18 
AnswerRe: How to execute Class without Main? Pin
PIEBALDconsult14-Mar-09 15:00
mvePIEBALDconsult14-Mar-09 15:00 
QuestionSockets in C# Pin
mrithula814-Mar-09 4:42
mrithula814-Mar-09 4:42 
AnswerRe: Sockets in C# Pin
Luc Pattyn14-Mar-09 5:01
sitebuilderLuc Pattyn14-Mar-09 5:01 
AnswerRe: Sockets in C# [modified] Pin
Member 349379914-Mar-09 7:02
Member 349379914-Mar-09 7:02 
GeneralRe: Sockets in C# Pin
mrithula814-Mar-09 20:21
mrithula814-Mar-09 20:21 
QuestionRe: Sockets in C# Pin
Jimmanuel15-Mar-09 1:14
Jimmanuel15-Mar-09 1:14 
GeneralRe: Sockets in C# [modified] Pin
mrithula814-Mar-09 22:06
mrithula814-Mar-09 22:06 
Hi,
I used multithreading concept so the client is able to communicate with any number of servers.Using the same concept i tried to call the 2nd server after 10 secs.When it comes to the client the 2nd time it gives"Invalid OperationException Cross-thread operation not valid: Control 'listBox6' accessed from a thread other than the thread it was created on."
//server
 public UdpServer()
        {
            try
            {
                Thread startServer = new Thread(new ThreadStart(start_server));
                startServer.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
                    // onReceive.Start();

            try
            {
                Thread.Sleep(10000);
  startServer2 = new Thread(new hreadStart(start_server2));
               
                startServer2.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }
public static void start_server()
        {
               
                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);

                Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                newsock.Bind(ipep);
                Console.WriteLine("Waiting for a client...");

                while (true)
                {
                    try
                    {
                        //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
                        EndPoint tmpRemote = (EndPoint)ipep;
                        // EndPoint tmpRemote = (sender);

                        byte[] data = new byte[1024];
                        Console.WriteLine("hai");

                        int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
                        Console.WriteLine("gfgjfk");


                        Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
                        Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received  

                        data = new byte[1024];
                        string ss = "Welcome to the Server";
                        data = Encoding.ASCII.GetBytes(ss);
                        newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote);


                        Console.WriteLine("\nSent Acknowledgement");

                    }
                    catch (SocketException e)
                    {
                        Console.WriteLine(e.Message);
                    }


                    //start_server();
                    //startServer.Start();
                   
                }        

        }

        public static void start_server2()
        {

         
            IPEndPoint ipep1 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10002);

            Socket newsock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            newsock1.Bind(ipep1);
            Console.WriteLine("Waiting for a client2...");

            while (true)
            {
                try
                {
                     EndPoint tmpRemote1 = (EndPoint)ipep1;
                    byte[] data = new byte[1024];
                    Console.WriteLine("hai");

                    int recv = newsock1.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote1);
                    Console.WriteLine("gfgjfk");
                    Console.WriteLine("Message received from {0}:", tmpRemote1.ToString());
                    Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received  

                    data = new byte[1024];
                    string ss = "Welcome to the Server";
                    data = Encoding.ASCII.GetBytes(ss);
                    newsock1.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote1);


                    Console.WriteLine("\nSent Acknowledgement");

                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.Message);
                }              
            }

        }
//client
private void SendMessage()
        {
           
            try
            {
               
                listBox6.Items.Add("Connecting....");
                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
                //IPEndPoint ipep = new IPEndPoint();
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
              listBox6.Items.Add("Connected");

                byte[] data = new byte[1024];
                String snd = "hello";
                data = Encoding.ASCII.GetBytes(snd);
                listBox6.Items.Add("Transmitting...");
                sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep);//sent hello                
               
                listBox6.Items.Add("Sent...");

                //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
                EndPoint tmpRemote = (EndPoint)ipep;

                listBox6.Items.Add("Message received from {0}:");
                listBox6.Items.Add(ipep.ToString());

                data = new byte[1024];
                int recv = sock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);

                String zz=Encoding.ASCII.GetString(data, 0, recv);
                listBox6.Items.Add(zz);
                if (zz == "Welcome to the Server")
                {
                    lb9.Text = "Active";
                    lb9.BackColor = Color.Green;
                }
                else{
                    lb9.Text = "Inactive";
                    lb9.BackColor=Color.Red;
                }            
            }

            catch (SocketException e)
            {
                //Console.WriteLine("Error..... " + e.StackTrace);
                MessageBox.Show(e.Message);

            }
           
        }
private void SendMessage1()
        {
           
            try
            {
               
                listBox6.Items.Add("Connecting....");
                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10002);
//same as above
}}
I am calling the SendMessage1 in a timer event

[code] private void tim_Elapsed(object sender,ElapsedEventArgs e)
{
SendMessage1();
}
Please help me with this

modified on Sunday, March 15, 2009 4:13 AM

GeneralRe: Sockets in C# Pin
mrithula817-Mar-09 1:07
mrithula817-Mar-09 1:07 
GeneralRe: Sockets in C# Pin
mrithula817-Mar-09 4:04
mrithula817-Mar-09 4:04 
QuestionHow to align the control in the centre. Pin
Pankaj Nikam14-Mar-09 4:21
professionalPankaj Nikam14-Mar-09 4:21 
AnswerRe: How to align the control in the centre. Pin
Xmen Real 14-Mar-09 5:51
professional Xmen Real 14-Mar-09 5:51 
GeneralRe: How to align the control in the centre. Pin
Pankaj Nikam14-Mar-09 7:52
professionalPankaj Nikam14-Mar-09 7:52 
AnswerRe: How to align the control in the centre. Pin
Luis Alonso Ramos14-Mar-09 9:47
Luis Alonso Ramos14-Mar-09 9:47 
QuestionMaximize and not hide taskbar Pin
David Muir14-Mar-09 3:35
David Muir14-Mar-09 3:35 
AnswerRe: Maximize and not hide taskbar Pin
PIEBALDconsult14-Mar-09 4:18
mvePIEBALDconsult14-Mar-09 4:18 
GeneralRe: Maximize and not hide taskbar Pin
David Muir14-Mar-09 4:25
David Muir14-Mar-09 4:25 
GeneralRe: Maximize and not hide taskbar Pin
PIEBALDconsult14-Mar-09 4:51
mvePIEBALDconsult14-Mar-09 4:51 
GeneralRe: Maximize and not hide taskbar Pin
Luc Pattyn14-Mar-09 4:59
sitebuilderLuc Pattyn14-Mar-09 4:59 
AnswerRe: Maximize and not hide taskbar Pin
Luc Pattyn14-Mar-09 4:24
sitebuilderLuc Pattyn14-Mar-09 4:24 
GeneralRe: Maximize and not hide taskbar Pin
David Muir14-Mar-09 4:37
David Muir14-Mar-09 4:37 
AnswerRe: Maximize and not hide taskbar Pin
Enquiren12-Sep-09 22:05
Enquiren12-Sep-09 22:05 
Questionhow to cause current application to close upon arrival of new updated application ? Pin
shabya14-Mar-09 3:14
shabya14-Mar-09 3:14 
AnswerRe: how to cause current application to close upon arrival of new updated application ? Pin
Navneet Hegde14-Mar-09 8:59
Navneet Hegde14-Mar-09 8:59 
GeneralRe: how to cause current application to close upon arrival of new updated application ? Pin
shabya14-Mar-09 9:31
shabya14-Mar-09 9:31 

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.