[Mono-devel-list] xmlserializer and sockets

ultrakorne ultrakorne at paranoici.org
Thu Dec 2 12:39:47 EST 2004

hi, i have some problems with my client talk to my server...
i am using xmlserializer to serialize object and send them to the other 
side of the connection. I need to send / recive by both client and 
server. client after login waits all the time listening for objects on a 
thread, and sends objects on users events on the main thread. server 
waits connections, start a new thread for each connection and after 
validating login waits for object, and answer with an object to every 
incoming object.

If i send only one packet, all works fine, i tryed many solution but i 
cannot send 2 packets, or send and recive...

here is my code:

LoginPacket is a class with informations

the method run of this class is launched as a thread to serve a client, 
with a socket initializated (and passed as parameter)
now for testing it will only waits for 2 login packets

public class ServeClient {
  	private Socket sock;
   	public ServeClient(Socket s) {
   		this.sock = s;
  	public void Run(){
		NetworkStream networkStream = new NetworkStream(sock);
  		//waiting for login request
		LoginPacket lp;
		this.DeserializeMessage( networkStream, out lp);
		LoginPacket lp2;
		this.DeserializeMessage( networkStream, out lp2);
		Console.WriteLine("Incoming login request: {0}", lp.Nick);
	// this will deserialize the message, and put it in message
	public bool DeserializeMessage(NetworkStream networkStream,out 
LoginPacket message) {
		Byte []buffer=new Byte[500];
		XmlSerializer deserializer=new XmlSerializer(typeof(LoginPacket));
		int count=networkStream.Read(buffer,0,buffer.Length);
		if(count <= 0) {
			return false;
		MemoryStream memoryStream=new MemoryStream(buffer,0,count);
		message= ((LoginPacket)deserializer.Deserialize(memoryStream));
		return true;

// run is launched as a thread, this thread will wait listening after
// login succed. now for testing it will only send 2 login requests
class ListenerThread {
	private TcpClient client; //FIXME : it is no syncronized
	public void Run() {
		client = new TcpClient( "localhost", 6666);;
		LoginPacket login = new LoginPacket( PacketType.LOGIN_REQUEST, "nick", 
		LoginPacket login2 = new LoginPacket( PacketType.LOGIN_REQUEST, 
"nick2", "pass2");


	//send a loginpacket
   	public void Send(LoginPacket message) {
		NetworkStream netWorkStream=null;
		XmlSerializer serializer=new XmlSerializer(message.GetType()); 
//Fixme: metterlo fuori dal metodo
		netWorkStream = client.GetStream();
		Stream stream=(Stream)netWorkStream;

thx if someone can solve my problems.


More information about the Mono-devel-list mailing list