Foros del Web » Programación para mayores de 30 ;) » .NET »

Cliente-Servidor en C#

Estas en el tema de Cliente-Servidor en C# en el foro de .NET en Foros del Web. Buenas a todos. Estoy realizando un programa Cliente-Servidor en C# y la verdad que es la primera vez que programo en este lenguaje. He conseguido ...
  #1 (permalink)  
Antiguo 06/06/2013, 10:24
 
Fecha de Ingreso: junio-2013
Mensajes: 2
Antigüedad: 10 años, 10 meses
Puntos: 0
Cliente-Servidor en C#

Buenas a todos.
Estoy realizando un programa Cliente-Servidor en C# y la verdad que es la primera vez que programo en este lenguaje. He conseguido hacer una conexión entre un cliente y un servidor pero no sé como hacer para conectar dos clientes al servidor. Si pudieseis modificar el código o una explicación no muy complicada me serviría de gran ayuda.
Muchas gracias.


Servidor
Código C#:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. namespace Server
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Console.WriteLine("Server Application");
  15. try
  16. {
  17. IPAddress ipAd = IPAddress.Any;
  18. // use local m/c IP address, and
  19. // use the same in the client
  20.  
  21. /* Initializes the Listener */
  22. TcpListener myList = new TcpListener(ipAd, 8001);
  23.  
  24. /* Start Listeneting at the specified port */
  25. myList.Start();
  26.  
  27. Console.WriteLine("The server is running at port 8001...");
  28. Console.WriteLine("The local End point is :" +
  29. myList.LocalEndpoint);
  30. Console.WriteLine("Waiting for a connection.....");
  31.  
  32. Socket s = myList.AcceptSocket();
  33. Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  34.  
  35. byte[] b = new byte[100];
  36. int k = s.Receive(b);
  37. Console.WriteLine("Recieved...");
  38. for (int i = 0; i < k; i++)
  39. Console.Write(Convert.ToChar(b[i]));
  40.  
  41. ASCIIEncoding asen = new ASCIIEncoding();
  42. s.Send(asen.GetBytes("The string was recieved by the server."));
  43. Console.WriteLine("\nSent Acknowledgement");
  44. /* clean up */
  45. s.Close();
  46. myList.Stop();
  47. }
  48. catch (Exception e) {
  49. Console.WriteLine("Error..... " + e.StackTrace);
  50. }
  51.  
  52. Console.ReadKey();
  53. }
  54. }
  55. }

Cliente

Código C#:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.IO;
  9. namespace Client
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Console.WriteLine("Client Application");
  16. try
  17. {
  18. TcpClient tcpclnt = new TcpClient();
  19. Console.WriteLine("Connecting.....");
  20.  
  21. tcpclnt.Connect("127.0.0.1", 8001);
  22. // use the ipaddress as in the server program
  23.  
  24. Console.WriteLine("Connected");
  25. Console.Write("Enter the string to be transmitted : ");
  26.  
  27. String str = Console.ReadLine();
  28. Stream stm = tcpclnt.GetStream();
  29.  
  30. ASCIIEncoding asen = new ASCIIEncoding();
  31. byte[] ba = asen.GetBytes(str);
  32. Console.WriteLine("Transmitting.....");
  33.  
  34. stm.Write(ba, 0, ba.Length);
  35.  
  36. byte[] bb = new byte[100];
  37. int k = stm.Read(bb, 0, 100);
  38.  
  39. for (int i = 0; i < k; i++)
  40. Console.Write(Convert.ToChar(bb[i]));
  41.  
  42. tcpclnt.Close();
  43. }
  44.  
  45. catch (Exception e)
  46. {
  47. Console.WriteLine("Error..... " + e.StackTrace);
  48. }
  49.  
  50. Console.ReadKey();
  51. }
  52. }
  53. }

Última edición por razpeitia; 10/06/2013 a las 14:48
  #2 (permalink)  
Antiguo 07/06/2013, 16:10
 
Fecha de Ingreso: enero-2012
Ubicación: Buenos Aires
Mensajes: 745
Antigüedad: 12 años, 3 meses
Puntos: 35
Respuesta: Cliente-Servidor en C#

Hola. C# es acá, esto es C/C++.


Etiquetas: c-c++, int, programa, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:03.