Pracuję nad projektem serwer/klient. Klient będzie pytał serwer o informacje, a serwer wyśle je z powrotem do klienta.C# wysłać obiekt odbierany przez sieć?
Informacje mogą być ciągiem znaków, cyframi, tablicami, listami, listami lub innymi obiektami. Rozwiązanie, które do tej pory znalazłem, polega na serializacji obiektu (danych) i wysłaniu go, a następnie przetworzeniu w postaci szeregowej.
Oto kod serwera:
public void RunServer(string SrvIP,int SrvPort)
{
try
{
var ipAd = IPAddress.Parse(SrvIP);
/* Initializes the Listener */
if (ipAd != null)
{
var myList = new TcpListener(ipAd, SrvPort);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port "+SrvPort+"...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
while (true)
{
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
var 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]));
string cmd = Encoding.ASCII.GetString(b);
if (cmd.Contains("CLOSE-CONNECTION"))
break;
var asen = new ASCIIEncoding();
// sending text
s.Send(asen.GetBytes("The string was received by the server."));
// the line bove to be modified to send serialized object?
Console.WriteLine("\nSent Acknowledgement");
s.Close();
Console.ReadLine();
}
/* clean up */
myList.Stop();
}
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
tutaj jest kod klienta, który powinien zwrócić obiekt
public object runClient(string SrvIP, int SrvPort)
{
object obj = null;
try
{
var tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect(SrvIP, SrvPort);
// use the ipaddress as in the server program
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
var str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
var asen = new ASCIIEncoding();
if (str != null)
{
var ba = asen.GetBytes(str);
Console.WriteLine("Transmitting.....");
stm.Write(ba, 0, ba.Length);
}
var bb = new byte[2000];
var k = stm.Read(bb, 0, bb.Length);
string data = null;
for (var i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));
//convert to object code ??????
Console.ReadLine();
tcpclnt.Close();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
return obj;
}
muszę wiedzieć dobrą serialize/serialize i jak do włączenia jej do rozwiązanie powyżej.
Dzięki przydzieleniu :-) dobrze Jestem nowy w tym wszystkim Nie dbam o to, co do zrobienia, aby osiągnąć i dowiedzieć się, co muszę wszystko czego potrzebujesz to służyć/rozwiązanie klient, który pomoże mi wysłać wartości/obiekty do serwera i uzyskać wyniki/odpowiedzi z serwera , takie jak wysłanie polecenia (ciąg) "getProcesses" , a serwer odeśle tablicę lub obiekt (Process [] processlist = Process.GetProcesses();) back do klienta jest to trudne? jaki jest najlepszy sposób na zrobienie tego? –
, ale jak użyć kodu z moim kodem? –
Polecam przeczytać jeden z dostępnych poradników "Pierwsze kroki w WCF", na przykład: http://msdn.microsoft.com/en-us/library/ms734712 (VS.100).aspx http://weblogs.asp.net/ralfw/archive/2007/04/14/a-truely-simple-example-to-get-started-with-wcf.aspx http://msdn.microsoft.com/en-us /library/ms730144.aspx –