Jia En

Jia En

  • NA
  • 30
  • 0

Need help in my project

Jan 1 2006 9:16 AM
Help.. My server program cant sent data to my client.. Someone pls save me.. Thankz... =================================================================================== Server Program =================================================================================== using System; using System.Threading; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; namespace Server_Console { /// /// Server Console in Linux Machine for Computer Games Addiction /// Averter Project. A menu will be displayed for the administrator /// to select the particular action to be done. /// class Server { /// /// The main entry point for the application. /// /*Variables Declaration and Initialization*/ public static TcpListener server; public static TcpClient client; /*Number of clients connected to the server*/ public static int number, clientNo = 1; /*Buffer for reading data*/ public static Byte[] bytes; public static string data = null; public static string myString; /*Create a network stream*/ public static NetworkStream stream; /* Application running flag */ public static bool bActive = true; /*For command*/ public static int command; /*******************************************/ [STAThread] static void Main(string[] args) { try { // Set the TcpListener on port 13000. Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); Console.Write("Waiting for a connection... "); // Enter the listening loop. while(true) { // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. client = server.AcceptTcpClient(); Console.WriteLine("Connected " + clientNo + "!"); // Get a stream object for reading and writing stream = client.GetStream(); /* Create data buffer */ bytes = new byte[client.ReceiveBufferSize]; //Create a new thread for the connection Thread t = new Thread(new ThreadStart(Server.run)); t.Start(); number = clientNo; //Increment the clientNO clientNo++; } } catch(SocketException e) { Console.WriteLine("SocketException: {0}", e); } } //end main public static void run() { try { /*Display Menu on the console window*/ Console.WriteLine("==================================="); Console.WriteLine("Main Menu"); Console.WriteLine("1. Display Client's Processes"); Console.WriteLine("2. Kill a Process"); Console.WriteLine("3. Detail of Selected Process"); Console.WriteLine("4. Shutdown Client's Computer"); Console.WriteLine("5. Execute VNC"); Console.WriteLine("==================================="); Console.WriteLine(""); Console.WriteLine("Enter an option: "); /*Read user input*/ command = Console.Read(); try { while(true) { Console.WriteLine("Reading Data From the socket.."); Server.readData(); if (myString.Equals("close")) { client.Close(); Console.WriteLine("Client " + number + "close it's socket connection.."); break; } else { //Console.Write(myString); Console.WriteLine("Sending Data Back the socket.."); Server.sendData(); break; } }//End While } catch(Exception ex) { Console.WriteLine("Please enter an option.."); } } catch (ObjectDisposedException oex) { Console.WriteLine("Error Occured: " + oex.StackTrace); } }//end run() /* Thread responsible for "remote input" */ public static void readData() { Console.WriteLine("Listening..."); while(bActive) { /* Reading data from socket (stores the length of data) */ int lData = stream.Read(bytes, 0, client.ReceiveBufferSize); /* String conversion (to be displayed on console) */ myString = Encoding.ASCII.GetString(bytes); /* Trimming data to needed length, because TcpClient buffer is 8kb long */ /* and we don't need that load of data to be displayed at all times */ /* (this could be done better for sure) */ myString = myString.Substring(0, lData); /* Display message */ Console.WriteLine(myString); break; } } /* Thread responsible for "local input" */ private static void sendData() { Console.WriteLine("Sending..."); while(bActive) { /* Simple prompt */ Console.Write("> "); /* Reading message/command from console */ myString = Console.ReadLine() + "\n"; /* Sending the data */ stream.Write(Encoding.ASCII.GetBytes( myString.ToCharArray()), 0, myString.Length); break; } } } } ================================================================================== Client Program.. ================================================================================== using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; namespace Client { /// /// Summary description for Class1. /// class Client { /// /// The main entry point for the application. /// public static TcpClient client; public static Byte[] data; //Create a network stream public static NetworkStream stream; /* Application running flag */ public static bool bActive = true; public static string message, myString = "Testing 123"; [STAThread] static void Main(string[] args) { try { message = "Testing 123"; // Create a TcpClient. // Note, for this client to work you need to have a TcpServer // connected to the same address as specified by the server, port // combination. Int32 port = 13000; client = new TcpClient("127.0.0.1", port); // Translate the passed message into ASCII and store it as a Byte array. data = System.Text.Encoding.ASCII.GetBytes(message); // Get a client stream for reading and writing. // Stream stream = client.GetStream(); stream = client.GetStream(); //Send Data sendData(); //Read Data readData(); // Close everything. myString = "close"; data = System.Text.Encoding.ASCII.GetBytes(myString); stream.Write(data, 0, data.Length); Console.WriteLine("Sent: {0}", myString); client.Close(); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } Console.WriteLine("\n Press Enter to continue..."); Console.Read(); }//end main /* Responsible for "remote input" */ public static void readData() { Console.WriteLine("Listening..."); while(bActive) { // Receive the TcpServer.response. // Buffer to store the response bytes. data = new Byte[256]; // String to store the response ASCII representation. String responseData = String.Empty; // Read the first batch of the TcpServer response bytes. /* Reading data from socket (stores the length of data) */ Int32 bytes = stream.Read(data, 0, data.Length); /* String conversion (to be displayed on console) */ responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); /* Trimming data to needed length, because TcpClient buffer is 8kb long */ /* and we don't need that load of data to be displayed at all times */ /* (this could be done better for sure) */ //myString = myString.Substring(0, lData); /* Display message */ Console.WriteLine("Received: {0}", responseData); break; } } /* Responsible for "local input" */ private static void sendData() { Console.WriteLine("Sending..."); while(bActive) { /* Simple prompt */ //Console.Write("> "); /* Reading message/command from console */ //myString = Console.ReadLine() + "\n"; /* Sending the data */ //stream.Write(Encoding.ASCII.GetBytes( // myString.ToCharArray()), 0, myString.Length); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); Console.WriteLine("Sent: {0}", message); break; } } } } ===================================================================================