2010-07-03 14:25:53 UTC
// To compile, run "csc AreaClient.cs"
// To run, first run "AreaServer" according to the instruction in file "AreaServer.cs"
// in another console window, then run "AreaClient"
using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
public class AreaClient {
public static void Main(String[] args) {
String serverURL = "localhost";
if (args.Length > 0)
serverURL = args[0]; // Take server URL from the command-line argument
try {
// Creates a socket connecting to the server's IP address and port number.
TcpClient connectToServer = new TcpClient();
connectToServer.Connect(serverURL, 8000);
// Get NetworkStream associated with TcpClient
NetworkStream stream = connectToServer.GetStream();
// Create objects or writing and reading across stream
BinaryWriter writer = new BinaryWriter(stream); // For writing to server
BinaryReader reader = new BinaryReader(stream); // for reading from server
// Continuously send radius and receive area from the server
while (true) {
// Read a radius from an input dialog
Console.Write("Please enter a radius: ");
String s = Console.ReadLine();
double radius;
try {
radius = Double.Parse(s);
}
catch (Exception) {
Console.WriteLine("Input parsing error");
break;
}
// Send the radius to the server
writer.Write(radius);
// Get area from the server
double area = reader.ReadDouble();
// Print area on the console
Console.WriteLine("Area received from the server is " + area);
}
}
catch (IOException e) {
Console.WriteLine(e.Message);
}
}
}