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 08/06/2013, 02:19
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace Server
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Server Application");
try
{
IPAddress ipAd = IPAddress.Any;
// use local m/c IP address, and
// use the same in the client

/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8001);

/* Start Listeneting at the specified port */
myList.Start();

Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");

Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));

ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}

Console.ReadKey();
}
}
}


Cliente

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Client Application");
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");

tcpclnt.Connect("127.0.0.1", 8001);
// use the ipaddress as in the server program

Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");

String str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();

ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
Console.WriteLine("Transmitting.....");

stm.Write(ba, 0, ba.Length);

byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);

for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));

tcpclnt.Close();
}

catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}

Console.ReadKey();
}
}
}
  #2 (permalink)  
Antiguo 08/06/2013, 05:58
 
Fecha de Ingreso: noviembre-2011
Ubicación: Huancayo
Mensajes: 70
Antigüedad: 12 años, 5 meses
Puntos: 6
Información Cliente-Servidor en C#

Hola,
otra alternativa a tu codigo es usar una conexión como :

Código PHP:

http
://www.connectionstrings.com/ 

Saludos
__________________
________________
CompuInicio.com
Iniciando Bien . . .

Etiquetas: Ninguno
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 16:52.