Note that there are some explanatory texts on larger screens.

plurals
  1. POClient / Server multi threaded using TCP Listener and Streamreader / writer
    text
    copied!<p>I've been asked to write a Client / Server application in C#, and have been given half-completed code to get started.</p> <p>It uses the <code>ThreadPool</code>, and <code>TcpListener</code> with <code>StreamReader</code> and <code>StreamWriter</code> classes for data transfer.The client has a working directory, which is fixed. The server has one too, but can change.</p> <p>Basically, you type in an option on the client using a switch statement, and the server processes that option and sends it back to the client.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.IO; using System.Security.Cryptography; namespace Client { /// &lt;summary&gt; /// Remote File Service client /// &lt;/summary&gt; class Client { /// &lt;summary&gt; /// Static entry point to client code /// &lt;/summary&gt; /// &lt;param name="args"&gt;unused&lt;/param&gt; static void Main(string[] args) { string dir = @"c:\st12_13d1_files"; try { Directory.SetCurrentDirectory(dir); } catch (DirectoryNotFoundException e) { Console.WriteLine("The specified directory does not exist. {0}", e); } try { using (TcpClient client = new TcpClient("localhost", 50012)) using (NetworkStream stream = client.GetStream()) using (StreamReader reader = new StreamReader(stream)) using (StreamWriter writer = new StreamWriter(stream)) { writer.AutoFlush = true; string option; do { Console.WriteLine("-Menu-\n"); Console.WriteLine("1-Print out file names-\n"); Console.WriteLine("2-Current Working Directory-\n"); Console.WriteLine("3-Files Present on the Server-\n"); Console.WriteLine("4- List of Subdirectory Names-\n"); Console.WriteLine("5- Change Server Directory Location-\n"); Console.WriteLine("6-Copy the contents of a file specified-\n"); Console.WriteLine("7-Close-\n"); Console.WriteLine("Please enter your choice: "); option = Console.ReadLine(); switch (option) { case "1": case "one": Console.WriteLine("You have selected Print out file names: "); break; } using System; using System.Net; using System.Net.Sockets; using System.IO; using System.Text; using System.Threading; using System.Security.Cryptography; namespace Server { /// &lt;summary&gt; /// Remote File Service main functionality /// &lt;/summary&gt; public class ServerMainline { public ServerMainline() { /* * *** TO DO - your code goes here *** * record initial current working directory value in a global variable */ string serv = (@"..\.."); try { //Set the current directory. Directory.SetCurrentDirectory(serv); } catch (DirectoryNotFoundException e) { Console.WriteLine("The specified directory does not exist. {0}", e); } TcpListener server = null; try { ThreadPool.SetMaxThreads(50, 50); server = new TcpListener(IPAddress.Any, 50012); server.Start(); while (true) { Console.WriteLine("Waiting for a new Client..."); TcpClient client = server.AcceptTcpClient(); ThreadPool.QueueUserWorkItem(serviceClient, client); } } catch (Exception e) { Console.WriteLine("Server mainline: SocketException: {0}", e); } finally { server.Stop(); server.Server.Close(); } } /// &lt;summary&gt; /// method to run to handle each client on separate thread /// &lt;/summary&gt; void serviceClient(object clientObject) { Thread currentThread = Thread.CurrentThread; int threadID = currentThread.GetHashCode(); try { using (TcpClient client = (TcpClient)clientObject) using (NetworkStream stream = client.GetStream()) using (StreamReader reader = new StreamReader(stream)) using (StreamWriter writer = new StreamWriter(stream)) { writer.AutoFlush = true; Console.WriteLine("Connected with a client, threadID: {0}", threadID); string option = reader.ReadLine(); while (option != "7") switch (option) { case "1": case "one": string[] myfiles = Directory.GetFiles(@"c:\st12_13d1_files"); Console.WriteLine("File Names {0}",Directory.GetFiles (@"c:\st12_13d1_files")); foreach (string file in myfiles) { //Console.WriteLine(file); Console.WriteLine(Path.GetFileName(file)); writer.WriteLine(myfiles); } break; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload