Sending serialized objects over the network

Morpheus

Well-known member
Joined
Jan 18, 2002
Messages
62
Im trying to send an serialized object over the network and it doesnt work. Im uncertain if it is the sending part och the recieving part that doesnt work but I think it is the sending part.

The code:
C#:
	public class Server
	{

		private NetworkStream socketStream;
		private BinaryWriter writer;
        private BinaryReader reader;		
		private Thread readThread;
		private Socket connection;
		private int port;

		public Server(int port)
		{
			this.port = port;
			readThread = new Thread(new ThreadStart( RunServer));
			readThread.Start();
		}

		public Server()
		{
		}

		
		public void RunServer()
		{
			TcpListener listener;
			RequestHandler rh = new RequestHandler();
			int counter = 1;

			
			try
			{
				
				IPAddress localaddr = IPAddress.Parse("127.0.0.1");
				listener = new TcpListener(localaddr, port);
				string message;

				
				listener.Start();
				Console.WriteLine("Server is running on port " + port);

				
				while(true)
				{
					Console.WriteLine("Waiting for incomming connections..");
					
					
					connection = listener.AcceptSocket();
					Console.WriteLine("Connection recieved from " + connection.RemoteEndPoint.ToString());

					
					socketStream = new NetworkStream(connection);

			
					writer = new BinaryWriter(socketStream);
					reader = new BinaryReader(socketStream);
					Console.WriteLine("Sending welcomming message..");

					
					writer.Write(" SERVER>>> Success ");
				
					while(connection.Connected)
					{
						
						rh.Operation( socketStream);
						
					}
					

					writer.Close();
					reader.Close();	
					socketStream.Close();

					
				}
			}

What Im trying to do is sending "socketStream" to another method in another class that serialize the object and send it using socketStream. Can I do so?

The recieve part look like this:
C#:
Customer cust = new Customer();
NetworkStream output;
output = client.GetStream();				
cust = (Customer)(bf.Deserialize(output));

I get a null value on cust.

The network communication works fine, I can send and recieve strings.

//Robin
 
Back
Top