Monthly Archives: May 2007

A simple Remoting example in C#

After looking in vain for an easy example to understand the basics of remoting, I decided to write one myself. I found one or two useful articles, but they had syntax errors and left a lot for the reader to fill in. My example needs no tweaking and can be used as is. For simplicity, I use only one machine. The server and the client reside on the same machine.

Problem statement:

TicketServer holds information about the ticket status of a movie theater. A client needs to know the status of a ticket. Establish a connection between the client and the server so that client gets the information needed.

Solution:

A method called GetTicketStatus is defined in the server space. This method returns the status of the ticket. The server publishes this method which can be used by any client. The server listens to port 9998 over TCP. The client invokes the published method and gets the ticket status.

Implementation:

An interface MovieTicketInterface is defined which contains the GetMovieTicket method signature. This interface is implemented by MovieTicket class. The method GetMovieTicket is also implemented.

The server TicketServer registers the MovieTicket class as a remoting service. It listens to the port 9998 and waits for communication from any client.

The client creates an object of type MovieTicketInterface as a remoting object. As a part of this step, the communication between the server and the client over TCP on port number 9998 is established. It then invokes the method GetTicketStatus and gets the status.

Source code:

Server part:

1. Create a Console Application named TicketServer.
2. Add System.Runtime.Remoting as a reference to the project.
3. Replace the existing code in Class.cs with the following code and build the project.

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

class Program
{
static void Main(string[] args)
{
TicketServer();
}

static void TicketServer()
{
Console.WriteLine("Ticket Server started...");

TcpChannel tcpChannel = new TcpChannel(9998);
ChannelServices.RegisterChannel(tcpChannel);

Type commonInterfaceType = Type.GetType("MovieTicket");

RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType,
"MovieTicketBooking", WellKnownObjectMode.SingleCall);

System.Console.WriteLine("Press ENTER to quitnn");
System.Console.ReadLine();

}

}

public interface MovieTicketInterface
{
string GetTicketStatus(string stringToPrint);
}

public class MovieTicket : MarshalByRefObject, MovieTicketInterface
{
public string GetTicketStatus(string stringToPrint)
{
string returnStatus = "Ticket Confirmed";
Console.WriteLine("Enquiry for {0}", stringToPrint);
Console.WriteLine("Sending back status: {0}", returnStatus);

return returnStatus;
}
}

Client side:

1. Create a Console Application named Client.
2. Add System.Runtime.Remoting and Server.exe [See Note 1] as references to the project.
3. Replace the existing code in Class.cs with the following code and build the project.


using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

class MyClient
{
public static void Main()
{
TcpChannel tcpChannel = new TcpChannel();
ChannelServices.RegisterChannel(tcpChannel);

Type requiredType = typeof(MovieTicketInterface);

MovieTicketInterface remoteObject = (MovieTicketInterface)Activator.GetObject(requiredType,
"tcp://localhost:9998/MovieTicketBooking");

Console.WriteLine(remoteObject.GetTicketStatus("Ticket No: 3344"));
}
}

Execution:

1. Execute Server.exe
2. Execute Client.exe

You will see the appropriate messages on the client side and the remote side.

Note 1: If you are using Visual Studio 2003, you cannot add a reference to an exe file. Make a copy of Server.exe, rename it to Server.dll and this can be added as a reference in your client project.

Don’t stop… let the music play

As a new-job gift, my husband presented me with a nifty, black, 80GB, fifth generation iPod. Being a music fanatic, me and my husband think that it’s worth all the money we spent on it.

The mouse wheel is as good as it was claimed to be. Just slide your finger over the glossy surface and iPod is at your service. It’s heavier than I thought. It’s black skin shines and it’s steel body is cold to the touch. It can play movies, it can store photographs… more than what I asked for. I was happy with just the music part.

I can access songs based on playlists, album, composer etc. Shuffle option is of course there. It comes pre-loaded with a few games. One of them is this cool game where a random song is picked and a part of it (not necessarily from the start) is played. You need to guess the song from the four options given. Probably boring for some, but for me, it sure is engaging. I could play this for ever.

What I really hate about this product is that it doesn’t have a stop button. Nope, I can’t stop playing a song. I can pause or start some other song, but I can’t stop. It’s official, it’s there on the site: iPod doesn’t have a stop button. I guess Apple wants to keep the music flowing. Don’t stop… let the music play.

It has gapless playback feature, which means the iPod will play music back to back with no gap in between. When switching from one song to the other, you will have absolutely no lapse. This does support the previous ‘Let the music flow’ philosphy, doesn’t it?

All in all, my satisfaction meter shows thumbs up for this new addition in my kitty. Bring on the music.

Disclaimer: This is not intended to be a review of iPod. This is just me drooling over my new found joy (or toy?).

.NET Remoting, Marshalling, yada yada

A search for basics on .NET remoting threw up these really useful articles. Bookmark them, you never know when you might need it.

Introduction to .NET Remoting

Copying, Cloning, and Marshalling in .NET

The question I hate the most

is “Tell me about yourself”.

I have been attending interviews recently and the last four interviews started with this exact question. Why, oh why, should every interview start with this question? Do you think this is a good ice-breaker? You are wrong. You can’t hurl this kind of a vague question at the interviewee and expect to make him/her feel comfortable. And what answer do you expect anyway? You want to know when my milk teeth fell off or how I hated my professor’s paunch or how much toothpaste do I put on my brush or what is my favorite color? If you want to know about my school life, ask me ‘Tell me about your education’. Or if you want to know about my previous companies, then ask that. Be specific. ‘Tell me about yourself’ is such a vague question, the moment I hear it, I start thinking about all those things which you wouldn’t want to know. The next time I am asked this, I am going to scream. Really loud.

Interviewers, please stop asking this question. Please.

Follow

Get every new post delivered to your Inbox.