[Mono-list] Can someone analyze this code and tell me what happens
Leonel Florin Selles
leonel06013 at cfg.jovenclub.cu
Mon Apr 4 16:03:45 EDT 2011
I have created two applications that connect on the same machine and sent
an object, I used Socket to connect, but when the client sends data to the
server, and the server revived the data and try to Deserialize is when it
get me out with one exception.
here is the client and server, the object that they use to communicate and
the class to serialize and deserialize
dataStruct
-----------------------------------------
using System;
namespace server
{
public class dataStruct
{
public dataStruct ()
{
}
public String clientName;
public Int32 clientPid;
public String host;
public String bd;
public String user;
public String pass;
public String usersCheck;
public Boolean systemTryIcon;
public String adminpass;
public enum signals
{
ConfFileExist,
ConfFileEmpty,
SocketServerNoStart,
ClientMalCerrado,
ClientBienCerrado,
TmServiceExist,
TmServiseNoExist,
CerrarSesion,
SendMeConfData,
SaveThisConfData,
ApagerEquipo,
ReiniciarEquipo,
ObjetoBdNoInicia,
BindToSocketBrouk,
ConectarBdImposible,
SocketNoListen,
ConsultaFallo,
ConsultaOK,
ExtractConsultaFallo,
ExtractConsultaOk,
PCNoExiste
}
public signals orden;
}
}
------------------------------------------------------
Serialice
-----------------------------------------------------
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace server
{
public class serializacion
{
public serializacion ()
{
}
public static byte[] Serialize (Object objeto)
{
BinaryFormatter formater = new BinaryFormatter ();
MemoryStream mem = new MemoryStream ();
formater.Serialize (mem, objeto);
return mem.GetBuffer ();
}
public static System.Object DeSerialize (byte[] listaByte)
{
BinaryFormatter formater = new BinaryFormatter ();
MemoryStream mem = new MemoryStream ();
mem.Write (listaByte, 0, listaByte.Length);
mem.Seek (0, 0);
return formater.Deserialize (mem);
}
}
}
----------------------------------------------------------------
client
------------------------------------
using System;
using System.Text;
using System.IO;
using System.Net.Sockets;
namespace cliente
{
class MainClass
{
public static void Main (string[] args)
{
byte[] sendData;
byte[] recvData;
Socket clientSocket = new Socket (AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.IP);
clientSocket.Connect ("localhost", 4069);
dataStruct structura = new dataStruct ();
structura.clientName = "Pepe";
structura.clientPid = 1234;
structura.host = "localhost";
structura.bd = "Tmaquina";
structura.user = "TmaquinaLoco";
structura.pass = "EstoNoCuadra";
structura.usersCheck = "estudiante,florin";
structura.systemTryIcon = true;
structura.adminpass = "pepeloco";
structura.orden = dataStruct.signals.ApagerEquipo;
sendData = serializacion.Serialize (structura);
clientSocket.Send (sendData, 0, sendData.Length, SocketFlags.None);
}
}
}
--------------------------------------------------
Server
---------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Text;
namespace server
{
class MainClass
{
public static void Main (string[] args)
{
byte[] sendData;
byte[] rcvData;
int puerto = 4069;
Socket socketServer = new Socket (AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.IP);
Socket socketClient;
socketServer.Bind (new IPEndPoint (IPAddress.Any, puerto));
socketServer.Listen (100);
socketClient = socketServer.Accept ();
rcvData = new byte[4000];
socketClient.Receive (rcvData, 0, rcvData.Length, SocketFlags.None);
dataStruct datos = new dataStruct ();
datos = (dataStruct)serializacion.DeSerialize (rcvData);
Console.Read ();
}
}
}
----------------------------------------------------------------
helpppppppppppppppppppp
More information about the Mono-list
mailing list