2012-11-03 6 views
1

클라이언트와 서버의 두 프로세스가 있습니다. 이것은 나의 클라이언트 프로세스입니다 : 이 follws과 같습니다 -tcp를 사용하는 동일한 컴퓨터에서 프로세스 간 통신

[Serializable ] 
public class retobj 
{ 
    public int a; 

} 
class client 
{ 
    static void Main(string[] args) 
    { 
     TcpClient client = new TcpClient(); 
     client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5005)); 
     Console.WriteLine("Connected."); 
     retobj ob = new retobj(); 
     ob.a = 90; 
     BinaryFormatter bf = new BinaryFormatter(); 
     NetworkStream ns = client.GetStream(); 
     bf.Serialize(ns, ob); 
     Console.WriteLine("Data sent."); 
     Console.ReadLine(); 
     ns.Close(); 
     client.Close(); 
    } 
} 

그리고 이것은 내 서버 프로세스입니다

[Serializable] 
public class retobj 
{ 
    public int a; 

} 
class server 
{ 
    static void Main(string[] args) 
    { 
     TcpListener listener = new TcpListener(IPAddress.Any, 5005); 
     listener.Start(); 
     Console.WriteLine("Server started."); 
     Socket client = listener.AcceptSocket(); 
     Console.WriteLine("Accepted client {0}.\n", client.RemoteEndPoint); 
     List<string> l = null; 
     retobj j = null; 
     using (NetworkStream ns = new NetworkStream(client)) 
     { 
      BinaryFormatter bf = new BinaryFormatter(); 
      j = (retobj)bf.Deserialize(ns); 
     } 
     //if (l != null) 
     // foreach (var item in l) 
     //  Console.WriteLine(item); 
     Console.WriteLine(j.a); 
     Console.ReadLine(); 
     client.Close(); 
     listener.Stop(); 
    } 

그러나 같은 오류가 있습니다 : 서버 프로세스에서 오류 : 없음 어셈블리 'ConsoleApplication45, 버전 = 1.0.0.0, Culture = neutral, PublicKeyToken = null'을 찾으십시오.

답변

2

BinaryFormatter을 사용하여 개체를 serialize하면 해당 개체의 출처에 대한 정보가 포함됩니다. 서버에서 역 직렬화 할 때 해당 정보를 읽고 클라이언트 어셈블리에서 retobj 버전을 찾습니다. 따라서이 오류가 발생합니다. 서버에있는 것과 동일하지 않습니다.

해당 클래스를 클래스 라이브러리 프로젝트로 이동하고 클라이언트와 서버 모두에서 해당 프로젝트를 참조하십시오. 두 장이 필요하지 않습니다.

다른 방법으로는 어셈블리 정보를 포함하지 않는 DataContractSerializer과 같은 대체 포맷터를 사용하는 것입니다.

+0

매우 매우 Thanx bro awesome solution ... – yuthub