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

C#

 
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 
GeneralRe: Sockets in C# Pin
mrithula817-Mar-09 1:07
mrithula817-Mar-09 1:07 
Hi The following code gives me the output:
Waiting for client...
hai
Waiting for client2.....
hai
Message received from...
hello
Sent ackn....

hai
Message received from...
server
Sent ackn....
I want it to be like:
Waiting for client...
hai
Message received from...
hello
Sent ackn....
Waiting for client 2...
I tried to change but im getting"the existing connection was forcibly closed by the remote host"..Please help me with this
//client
public 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);
               //sock.Bind(ipep);
               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
               //sock.SendTo(data, ipep);//sent hello

               listBox6.Items.Add("Sent...");
               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);

           }
           //SendMessage1();
       }

       public void SendMessage1()
       {
       //same as above
            }
//server
public UdpServer()
        {
            try
            {
                startServer = new Thread(new ThreadStart(start_server));
                startServer.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                startServer.Abort();
            }

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

 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;
                     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);
                    }                    
                }        
        }
   public static void start_server2()
        {
          //same as above
}

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 
GeneralRe: how to cause current application to close upon arrival of new updated application ? Pin
Navneet Hegde15-Mar-09 0:13
Navneet Hegde15-Mar-09 0:13 

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.