2013-02-26 4 views
2

Я создаю приложение чата с двумя различными консольными приложениями, используя соединение с каналом TCP, но когда я запускаю консоль клиента или консоль сервера, я получаю «TCP уже зарегистрирован»C# visual studio TCP уже зарегистрирован

У меня есть 4 проекта кода.

  1. Интерфейс
  2. RemoteObject
  3. Client
  4. Сервер

Вот код интерфейса:

using System; 

public interface IRemoteObject 
{ 
    void GetData(string myString);   

} 

Вот код RemoteObject:

using System; 

public class RemoteObject : MarshalByRefObject,IRemoteObject 
{ 
    //Server Method 
    public RemoteObject() 
    { 
     Console.WriteLine("Server text");     
    } 

    //Client Method 
    public void GetData(string myString) 
    { 
     Console.WriteLine(myString); 
    } 
} 

Вот код клиента:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.Remoting; 
using System.Runtime.Remoting.Channels; 
using System.Runtime.Remoting.Channels.Tcp; 
using System.Threading; 

namespace ClientConsole 
{ 
    public class Client 
    { 
     public static void Main() 
     { 

      ThreadStart client1 = new ThreadStart(ClientSide); 
      Thread client2 = new Thread(client1); 
      ThreadStart server1 = new ThreadStart(ServerSide); 
      Thread server2 = new Thread(server1); 
      client2.Start(); 
      server2.Start();   

     } 
     public static void ClientSide() 
     { 
      Console.WriteLine("ClientSide...."); 
      TcpChannel ch2 = new TcpChannel(); 
      ChannelServices.RegisterChannel(ch2, true); 

      IRemoteObject objRemoteRef = (IRemoteObject)Activator.GetObject(typeof(IRemoteObject), "tcp://127.0.0.1:2233/M"); 

      while (true) 
      { 
       string x = Console.ReadLine(); 
       objRemoteRef.GetData(x); 

      } 

     } 

     public static void ServerSide() 
     { 
      Console.WriteLine("ServerSide...."); 
      TcpChannel ch2 = new TcpChannel(2233); 
      ChannelServices.RegisterChannel(ch2, true); 

      RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "M", WellKnownObjectMode.Singleton); 

     } 

    } 

} 

Вот код сервера:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime; 
using System.Runtime.Remoting; 
using System.Runtime.Remoting.Channels; 
using System.Runtime.Remoting.Channels.Tcp; 
using System.Threading; 


namespace ServerConsole 
{ 
    public class Server 
    { 
     public static void Main() 
     { 
      ThreadStart server1 = new ThreadStart(ServerSide); 
      Thread server2 = new Thread(server1); 
      ThreadStart client1 = new ThreadStart(ClientSide); 
      Thread client2 = new Thread(client1); 
      server2.Start(); 
      client2.Start(); 


     } 

     public static void ServerSide() 
     { 
      Console.WriteLine("ServerSide...."); 
      TcpChannel ch2 = new TcpChannel(); 
      ChannelServices.RegisterChannel(ch2, true); 

      IRemoteObject objRemoteRef = (IRemoteObject)Activator.GetObject(typeof(IRemoteObject), "tcp://127.0.0.1:2333/M"); 

      while (true) 
      { 
       string x = Console.ReadLine(); 
       objRemoteRef.GetData(x); 

      } 
     } 

     public static void ClientSide() 
     { 
      Console.WriteLine("ClientSide...."); 
      TcpChannel ch1 = new TcpChannel(2233); 
      ChannelServices.RegisterChannel(ch1, true); 

      RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "M", WellKnownObjectMode.Singleton); 

      Console.ReadLine(); 



     } 
    } 
} 

Пожалуйста, помогите, я довольно новый здесь, и я не уверен, что я написал пост правильно, пожалуйста пойми.

Благодаря

ответ

2

Попробуйте это:

Клиент

public class Client 
{ 
    public static void Main(string[] args) 
    { 

     ThreadStart client1 = new ThreadStart(ClientSide); 
     Thread client2 = new Thread(client1); 
     ThreadStart server1 = new ThreadStart(ServerSide); 
     Thread server2 = new Thread(server1); 
     client2.Start(); 
     server2.Start(); 
    } 
    public static void ClientSide() 
    { 
     Console.WriteLine("ClientSide...."); 
     TcpChannel ch2 = (TcpChannel)Helper.GetChannel(2333, true); 
     ChannelServices.RegisterChannel(ch2, false); 
     IRemoteObject objRemoteRef = (IRemoteObject)Activator.GetObject(
      typeof(IRemoteObject), "tcp://127.0.0.1:2233/M"); 
     while (true) 
     { 
      string x = Console.ReadLine(); 
      objRemoteRef.GetData(x); 
     } 
    } 
    public static void ServerSide() 
    { 
     Console.WriteLine("ServerSide...."); 
     TcpChannel ch2 = (TcpChannel)Helper.GetChannel(0, true); 
     ChannelServices.RegisterChannel(ch2, false); 
     RemotingConfiguration.RegisterWellKnownServiceType(
      typeof(RemoteObject), "M", WellKnownObjectMode.Singleton); 
    } 
} 

Сервер:

public class Server 
{ 
    public static void Main() 
    { 
     ThreadStart server1 = new ThreadStart(ServerSide); 
     Thread server2 = new Thread(server1); 
     ThreadStart client1 = new ThreadStart(ClientSide); 
     Thread client2 = new Thread(client1); 
     server2.Start(); 
     client2.Start(); 
    } 
    public static void ServerSide() 
    { 
     Console.WriteLine("ServerSide...."); 
     TcpChannel ch2 = (TcpChannel)Helper.GetChannel(2233, true); 
     ChannelServices.RegisterChannel(ch2, false); 
     IRemoteObject objRemoteRef = (IRemoteObject)Activator.GetObject(
      typeof(IRemoteObject), "tcp://127.0.0.1:2333/M"); 
     while (true) 
     { 
      string x = Console.ReadLine(); 
      objRemoteRef.GetData(x); 
     } 
    } 
    public static void ClientSide() 
    { 
     Console.WriteLine("ClientSide...."); 
     TcpChannel ch1 = (TcpChannel)Helper.GetChannel(0, true); 
     ChannelServices.RegisterChannel(ch1, false); 
     RemotingConfiguration.RegisterWellKnownServiceType(
      typeof(RemoteObject), "M", WellKnownObjectMode.Singleton); 
     Console.ReadLine(); 
    } 
} 

Helper класс

public class Helper 
{ 
    public static IChannel GetChannel(int tcpPort, bool isSecure) 
    { 
     BinaryServerFormatterSinkProvider serverProv = 
      new BinaryServerFormatterSinkProvider(); 
     serverProv.TypeFilterLevel = TypeFilterLevel.Full; 
     IDictionary propBag = new Hashtable(); 
     propBag["port"] = tcpPort; 
     propBag["typeFilterLevel"] = TypeFilterLevel.Full; 
     propBag["name"] = Guid.NewGuid().ToString(); 
     if (isSecure) 
     { 
      propBag["secure"] = isSecure; 
      propBag["impersonate"] = false; 
     } 
     return new TcpChannel(
      propBag, null, serverProv); 
    } 
} 
+0

Спасибо за помощь – user2061311

+0

Добро пожаловать! –