Informatica
Ingineria programariide laboratorINGINERIA PROGRAMARII LUCRARE DE LABORATOR ObiectiveInsusirea modului de lucru cu reteaua utilizand C#. Obtinerea si afisarea hostname-ului si a adresei IP folosind C#. Insusirea modului de lucru cu cu socket-uri sincrone si asincrone. 1. Obtinerea si afisarea hostname-ului si a adresei IP folosind C#. 2.1Studiati si testati urmatorul cod sursa C#: using System; using System.Net; namespace HostnameAndIP public static string getIP(string hostname) public static void Main() } 2. Insusirea modului de lucru cu cu socket-uri sincrone 2.1Socketr-ul server. Studiati si testati urmatorul cod sursa C#: using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketServer } //Afisarea datelor primite Console.WriteLine('Text primit : ', clientData); // Si trimiterea acesteia inapoi byte[] msg = Encoding.ASCII.GetBytes(clientData); handler.Send(msg); handler.Shutdown(SocketShutdown.Both); handler.Close(); } catch (Exception e) Console.WriteLine('nApasati ENTER pentru a continua'); Console.Read(); } public static int Main(String[] args) 2.2Socketr-ul client. Studiati si testati urmatorul cod sursa C#: using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketClient public static void StartClient() { // Buffer pentru datele care vin dinspre server byte[] buffer = new byte[1024]; try { // Stabilirea EndPoint-ului pentru client IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress,1234); // // Crearea socket-ului TCP/IP Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Conectare la server try { sender.Connect(remoteEndPoint); Console.WriteLine('Socket connected to ', sender.RemoteEndPoint.ToString()); byte[] msg = Encoding.ASCII.GetBytes('Acesta este un test<EOF>'); // Trimiterea datelor int bytesSent = sender.Send(msg); // Primirea raspunsului de la server int bytesReceptionati = sender.Receive(buffer); Console.WriteLine('Echoed test = ', Encoding.ASCII.GetString(buffer, 0, bytesReceptionati)); // Release the socket. sender.Shutdown(SocketShutdown.Both);
sender.Close(); } catch (ArgumentNullException ane) { Console.WriteLine('ArgumentNullException : ',ane.ToString()); } catch (SocketException se) { Console.WriteLine('SocketException : ',se.ToString()); } catch (Exception e) { Console.WriteLine('Unexpected exception : ', e.ToString()); } catch (Exception e) } public static int Main(String[] args) 2.3Modificati cele doua programe astfel incat: transmisia datelor sa fie efectuata pe portul 65535; datele trimise de la nivelul socket-ului client sa fie citite de la tastatura; socket-ul server sa trimita inapoi sirul de caractere format din majuscule; comunicatia sa se termine la introducerea textului "EXIT" la nivelul clientului. 3. Insusirea modului de lucru cu cu socket-uri asincrone 3.1Socketr-ul server. Studiati si testati urmatorul cod sursa C#: using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; public class StateObject public class SocketServerAsincron public static void StartListening() } catch (Exception e) Console.WriteLine('nApasati ENTER pentru a continua'); Console.Read(); } public static void AcceptCallback(IAsyncResult ar) public static void ReadCallback(IAsyncResult ar) { String content = String.Empty; StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { state.sb.Append(Encoding.ASCII.GetString( state.buffer, 0, bytesRead)); content = state.sb.ToString(); if (content.IndexOf('<GATA>') > -1) Console.WriteLine('Am citit octeti. n Date primite : ', content.Length, content); Send(handler, content); else } } private static void Send(Socket handler, String data) private static void SendCallback(IAsyncResult ar) { try { Socket handler = (Socket)ar.AsyncState; int bytesSent = handler.EndSend(ar); Console.WriteLine('Sent bytes to client.', bytesSent); handler.Shutdown(SocketShutdown.Both); handler.Close(); } catch (Exception e) } public static int Main(String[] args) 3.2Socketr-ul client. Studiati si testati urmatorul cod sursa C#: using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.Text; public class StateObject public class AsynchronousClient { private const int port = 1234; private static ManualResetEvent connectDone = new ManualResetEvent(false); private static ManualResetEvent sendDone = new ManualResetEvent(false); private static ManualResetEvent receiveDone = new ManualResetEvent(false); private static String response = String.Empty; private static void StartClient() { try { IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client); connectDone.WaitOne(); // Trimitere date de test Send(client,'This is a test<GATA>'); sendDone.WaitOne(); Receive(client); receiveDone.WaitOne(); Console.WriteLine('Raspuns primit de la server : ', response); client.Shutdown(SocketShutdown.Both); client.Close(); } catch (Exception e) } private static void ConnectCallback(IAsyncResult ar) { try { Socket client = (Socket) ar.AsyncState; client.EndConnect(ar); Console.WriteLine('Socket conectat la ', client.RemoteEndPoint.ToString()); connectDone.Set(); } catch (Exception e) } private static void Receive(Socket client) catch (Exception e) } private static void ReceiveCallback( IAsyncResult ar ) else receiveDone.Set(); } catch (Exception e) } private static void Send(Socket client, String data) private static void SendCallback(IAsyncResult ar) { try { Socket client = (Socket) ar.AsyncState; int bytesSent = client.EndSend(ar); Console.WriteLine('Am trimis octeti spre server.', bytesSent); sendDone.Set(); } catch (Exception e) } public static int Main(String[] args) 3.3Explicati necesitatea functiilor: La nivelul serverului: public static void StartListening() public static void AcceptCallback(IAsyncResult ar) public static void ReadCallback(IAsyncResult ar) private static void Send(Socket handler, String data) private static void SendCallback(IAsyncResult ar) La nivelul clientului:
3.4Modificati cele doua programe astfel incat: transmisia datelor sa fie efectuata pe portul 2345; datele trimise de la nivelul socket-ului client sa fie citite de la tastatura; socket-ul server sa trimita inapoi doar lungimea textului primit; comunicatia sa se termine la introducerea textului "EOF" la nivelul clientului. 3.5 Testati aplicatiile modificate lansand in executie un singur server si clienti multipli. Teme:1. Creati o aplicatie C# de tip server socket asincron si una de tip client socket asincron cu ajutorul carora doi utilizatori aflati pe computere diferite sa poata conversa. Pentru dezvoltarea interfetelor grafice puteti urma exemplele:
|