Monthly Archives: September 2012

Accessing network machine by name in VMWare Player

For each customer I have a separate VMWare virtual development machine. This allows me to separate all environments, as for each of them I have other software and frameworks installed, and mixing these may cause compatibility issues. And if something happens with my laptop, I don’t loose my virtual machines and can simply use them on another laptop.

I was having one issue though: from within a virtual machine I could not access the internal network, for example, pinging a machine in the network resulted in an error:

vm1

First solution

VMWare Player’s network adapter was configured to use NAT; and internet connection was working fine. Then I looked up the IP address of the network machine, and tried to ping that one:

vm2

And this worked. So it meant that it could not resolve the machine name.

To solve this, I added the mapping between IP address and machine name in the hosts file:

vm3

After that, I could access the machine by its name without a problem:

vm4

Second solution

I was not very happy with the first solution. But then I found that without these entries added to the hosts file, I could still access the machine if I used its name suffixed with the domain extension:

vm5

So, a better approach would be to somehow make the virtual machine automatically add the domain suffix with each call.

Indeed, if I did a ipconfig /all, it showed that DNS suffix Search List was localdomain:

vm8

To change this, go to ‘Control Panel\Network and Internet\Network and Sharing Center’, and click the connection. Then click properties, select the ‘Internet Protocol Version 4’, and click properties. Click the advanced button. In the DNS tab, add the required DNS suffix:

vm6

Now an ipconfig /all reveals that the DNS Suffix Search List now is the actual domain:

vm9

So now, if we ping the machine with just its name, it works:

vm7

 

Third solution

And then it seems there is yet another way of solving this. Right click ‘Start/Computer’ and click properties. Click ‘Change settings’ next to the computer name, and then click the ‘Change’ button, then the ‘More’ button, and fill in the Primary DNS Suffic of this computer:

vm10

It has the same effect, but also changes your default domain.

But, unfortunately…

…after a reboot, it stopped working again. With NAT mode I had internet connection but network did not function properly, with bridged mode network worked but I did not have internet connection. I tried every solution that I found on the net but none of them worked. So I decided to give VirtualBox a try… and there everything worked immediately, out of the box! See my post about that.

Communicating through sockets

A Socket is an end-point of a bidirectional communication link between two applications running on the same network. So basically it means you can use it to allow applications to communicate with each other. You have a server, that listens for incoming connections, and clients that make a connection to the server. Then, data can be transferred between the clients and the server.

First create a console application and call it SocketServer. The main looks like:

class Program
{
private static System.Net.Sockets.Socket _socket;
private static AsyncCallback _dataTransferCallBack;
private const int MaxLengthOfPendingConnectionsQueue = 10;
private const short SocketPort = 7761;



static void Main(string[] args)
{
_socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
var endPoint = new IPEndPoint(IPAddress.Any, SocketPort);
_socket.Bind(endPoint);
_socket.Listen(MaxLengthOfPendingConnectionsQueue);
// Begins an asynchronous operation to accept an incoming
// connection attempt.
_socket.BeginAccept(OnConnectRequest, null);
Console.WriteLine("Server started, listening...");
Console.ReadLine();
// Clean up socket.
_socket.Close();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

The socket is created, and bound to an endpoint at a specific port number. Then the BeginAccept operation makes the application wait for an incoming connection request (asynchronously). If a connection request comes in, then the OnConnectRequest operation is executed:

 
public static void OnConnectRequest(IAsyncResult asyn)
{
try
{
// EndAccept completes a call to BeginAccept.
// It returns a new Socket that can be used to send data
// to and receive data from the remote host.
var dataTransferSocket = _socket.EndAccept(asyn);
WaitForData(dataTransferSocket);
_socket.BeginAccept(OnConnectRequest, null);
}
catch (ObjectDisposedException)
{
Console.WriteLine("OnConnectRequest: Socket has been closed.");
}
catch (SocketException ex)
{
Console.WriteLine(
string.Format("Something fishy happened: {0}", ex.Message));
throw;
}
}

In this operation we first call the WaitForData operation, and then call the BeginAccept operation again to continue listening to incoming connection requests of other clients. The WaitForData starts the actual receive of data: if the connected client sends data, the OnDataReceived callback operation is executed:

public static void WaitForData(System.Net.Sockets.Socket dataTransferSocket)
{
try
{
if (_dataTransferCallBack == null)
{ _dataTransferCallBack = OnDataReceived; }
var socketPacket = new SocketPacket(dataTransferSocket);
// Now start listening for data.
dataTransferSocket.BeginReceive(socketPacket.DataBuffer, 0,
socketPacket.DataBuffer.Length, SocketFlags.None,
_dataTransferCallBack, socketPacket);
}
catch (SocketException ex)
{
Console.WriteLine(
string.Format("Something fishy happened: {0}", ex.Message));
throw;
}
}
 
Where SocketPacket is like:
 
public class SocketPacket
{
public System.Net.Sockets.Socket Socket { get; set; }
public byte[] DataBuffer { get; set; }

public SocketPacket(System.Net.Sockets.Socket socket)
{
Socket = socket;
DataBuffer = new byte[1024];
}
}

In the OnDataReceived operation we check if there is data, if there is, we keep calling the WaitForData again to get the next data. If there is no data anymore, we stop the transfer and close the socket:
 
public static void OnDataReceived(IAsyncResult asyn)
{
try
{
var socketPacket = (SocketPacket)asyn.AsyncState;
int numberOfBytesReceived = socketPacket.Socket.EndReceive(asyn);

if (numberOfBytesReceived <= 0)
{
Console.WriteLine("Client {0}, disconnected",
socketPacket.Socket.RemoteEndPoint);
socketPacket.Socket.Close();
return;
}

var receivedData = Encoding.ASCII.GetString(socketPacket.DataBuffer,
0, numberOfBytesReceived);
Console.WriteLine("{0}", receivedData);
WaitForData(socketPacket.Socket);
}
catch (ObjectDisposedException)
{
Console.WriteLine("OnConnectRequest: Socket has been closed.");
throw;
}
catch (SocketException ex)
{
Console.WriteLine(
string.Format("Something fishy happened: {0}", ex.Message));
throw;
}
}

Now what’s left is to write the client side code to connect to the server and send messages. Create a new console application and call it SocketClient. For the client, I’m going to write a SocketPublisher that contains all the logic to connect and send the messages, which is used by the client. This is the code:
 
public interface ISocketPublisher
{
void SendMessage(string message);
}

public class SocketPublisher : ISocketPublisher, IDisposable
{
private System.Net.Sockets.Socket _socket;
private readonly string _remoteAddress;
private readonly short _remotePortNumber;

public SocketPublisher(string remoteAddress,
short remotePortNumber)
{
_remoteAddress = remoteAddress;
_remotePortNumber = remotePortNumber;
Connect();
}

public void SendMessage(string message)
{
var messageBytes =
System.Text.Encoding.ASCII.GetBytes(message);
_socket.Send(messageBytes);
}

public void Dispose()
{
Disconnect();
}

private void Disconnect()
{
if (_socket != null)
{
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
}
}

private void Connect()
{
if ((_socket == null) || (!_socket.Connected))
{
var remoteIpAddress =
System.Net.IPAddress.Parse(_remoteAddress);
var remoteEndPoint = new System.Net.IPEndPoint(
remoteIpAddress, _remotePortNumber);
_socket = new System.Net.Sockets.Socket(
AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(remoteEndPoint);
}
}
}

And in the Main you can then send a message as follows:
 
class Program
{
private const string SocketAddress = "127.0.0.1";
private const short SocketPort = 7761;

static void Main(string[] args)
{
using (var socketPublisher =
new SocketPublisher(SocketAddress, SocketPort))
{
socketPublisher.SendMessage("Hello world!");
}
}
}

Now start the server, then start the client, and you should see the message coming through at the server:

socket

The server can also send messages back to the clients; if you want to do that you need to keep track of connected clients sockets and then you can send messages back.
 
 
 

Hiding a user in Windows 7

Today I setup a virtual machine with Windows 7, Sql Server 2008 R2 and Visual Studio 2010. During installation of Sql Server, I had to specify a user account for the sql server and sql server agent services, . So I created a new user account (SQLSERVER), and used that one. Of course, this user account now shows up at the login screen of windows:

win1

So I wanted to hide this account, and it was actually quite easy to do. Go to ‘Administrative Tools/Local Security Policy’. In ‘Security Settings’, click on ‘Local Policies’ and then on ’User Rights Assignment’. Find the policy for “Deny log on locally’ , double click it and add the user you want to exclude to the list:

win2

If you now log in again, you should not see the user anymore:

win3

Trails – Find your way!

Today I discovered a nice app for the iPad/iPhone, called trails, that helps me find the fastest route to work (and back). It records the route I take, with time and distance, which allows me to compare them to alternatives I tried out.

The nice thing is that it works also without internet connection, because it caches the maps once it is connected. And you can also export and import maps, and much more… for more information, look here.

So how does it work… click the ‘+’ button to add a new track, enter a name and click ‘Save’:

1

Now you’re ready to start recording, just click the ‘Start recording’ button:

2

Start driving, walking or whatever you are doing. The app will track where you go. If you’re finished, click the ‘Stop recording’ button:

3

And then you see an overview of the route you took, elapsed time, distance, and even the ascent and descent:

4

And of course you can also use it to track your hikes, bike trips or jogs!

Outlook 2010 profiles

I use Outlook 2010 as an email client, in which I have configured one POP account (my Dunatis email account) and a MAPI account (my hotmail account). Recently I also added an exchange account (my account at a customer).

Unfortunately I ran into a number of issues. At the customer’s side I could not send emails using my Dunatis account and at home I could not send any mail because it was trying to connect to the exchange server – which I cannot access at home.

To solve this I had to create two different profiles:

  • Home profile: contains Dunatis and hotmail accounts
  • Business profile: contains customer account

To create profiles, go to control panel / User Accounts and Family Safety / Mail:

mail1

Click ‘Show Profiles’. Now you can add the profiles you need, and add the needed email accounts to each profile. Finally, make sure ‘Prompt for a profile to be used’ is checked:

mail2

Click OK. If you now start Outlook, you can select the correct profile and everything works as a charm Smile