I'm trying to develop client server application which can transfer Files and Strings from Client to Server. I'm new to TCP, sockets and server transactions.
Client side is written in Java and Server Side is Written in C#
what I'm trying to do in Server(C#) , get all the bytes and cast it to an Object, because I'm sending all the Objects from client side in Object type. then i wanted to cast to File or String
but in this line of code I'm getting an error
Object myObject = (Object)binForm.Deserialize(memStream);
Server side:-
private void ClientHandler(object client)
{
int bytesRead = 0;
byte[] buffer = new byte[32];
tcpClient = (TcpClient)client;
clientStream = tcpClient.GetStream();
Console.WriteLine("Client Handler Started!");
while (true)
{
bytesRead = 0;
try
{
Console.WriteLine("Server waiting for commands\n");
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
while ((bytesRead = tcpClient.Client.Receive(buffer)) > 0)
{
Console.WriteLine("bytes received :- " + bytesRead);
memStream.Write(buffer, 0, bytesRead);
}
try
{
memStream.Position = 0;
Object myObject = (Object)binForm.Deserialize(memStream);
// After this cast it to String or File
}
catch(Exception exp)
{
Console.WriteLine(exp.ToString());
}
}
catch
{
//a socket error has occured
Console.WriteLine("a socket error has occured!!!");
break;
}
}