1 / 25

NETWORKING tworzenie połączeń sieciowych · gniazda UDP, TCP · klasy transportowe

NETWORKING tworzenie połączeń sieciowych · gniazda UDP, TCP · klasy transportowe · klasy protokółów · programowanie asynchroniczne. · gniazda UDP - odbiorca Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

faris
Download Presentation

NETWORKING tworzenie połączeń sieciowych · gniazda UDP, TCP · klasy transportowe

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NETWORKING tworzenie połączeń sieciowych ·gniazda UDP, TCP ·klasy transportowe ·klasy protokółów ·programowanie asynchroniczne

  2. ·gniazda UDP - odbiorca Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint localEP = // adres odbiorcy new IPEndPoint(IPAddress.Any, 5001); listener.Bind(localEP);

  3. IPEndPoint tempRemoteEP = // adres nadawcy new IPEndPoint(IPAddress.Any, 5002); EndPoint trEP = (EndPoint)tempRemoteEP; listener.Poll(-1, SelectMode.SelectRead); listener.ReceiveFrom(buffer, ref trEP);

  4. - nadawca string listener = "localhost"; Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint localEP = // adres nadawcy new IPEndPoint(IPAddress.Any, 5002); client.Bind(localEP);

  5. buffer = Encoding.ASCII.GetBytes(message); ipaddress=Dns.GetHostEntry(listener).AddressList[0]; IPEndPoint remoteEP = // adres odbiorcy new IPEndPoint(ipaddress, 5001); client.SendTo(buffer, 0, buffer.Length, SocketFlags.None, remoteEP); UDPSocket

  6. ·gniazda TCP - odbiorca Socket GetListenerSocket(string host, int port) { Socket s = null, ts; IPEndPoint lep; IPHostEntry iphe = Dns.GetHostEntry(host); foreach(IPAddress ipa in iphe.AddressList) { lep = newIPEndPoint(ipa, port); ts = newSocket(lep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

  7. try { ts.Bind(lep); ts.Listen(2); // max 2 waiting connections s = ts; break; } catch( . . . ) { . . . } return s; }

  8. Socket server = GetListenerSocket("localhost", 5010); Socket s = server.Accept(); byte[] responseData = newbyte[128]; int bytesRead; try { bytesRead = s.Receive(responseData); . . . } catch . . . { ... }

  9. - nadawca Socket GetSocket(string host, int port) { Socket s = null, ts; IPEndPoint ipe; IPHostEntry iphe = ns.GetHostEntry(host); foreach(IPAddress ipa in iphe.AddressList) { ipe = newIPEndPoint(ipa, port); ts = newSocket(ipe.AddressFamily,SocketType.Stream,ProtocolType.Tcp);

  10. try • { ts.Connect(ipe); • s = ts; • break;} • catch ( . . .){ . . .} • } • return s; • } • Socket s = GetSocket("localhost", 5010); • if(s == null) • return; • s.Send(buffer); TCPSocket

  11. ·klasa transportowa UDP UdpClient - odbiorca UdpClient listener = newUdpClient(port); IPEndPoint remoteEp = newIPEndPoint(IPAddress.Any, port); byte[] buffer = listener.Receive(ref remoteEp);

  12. - nadawca UdpClient client = newUdpClient(port); byte[] buffer =Encoding.ASCII.GetBytes(message); client.Send(buffer, buffer.Length, listener, port); client.Close(); UDPClass

  13. ·klasy transportowe TCP TcpListener TcpClient - odbiorca TcpListener listener = newTcpListener(port); listener.Start(); if (listener.Pending()) {TcpClient s = listener.AcceptTcpClient(); NetworkStream stream = s.GetStream(); } // int bytesRead = stream.Read(responseData, 0,responseData.Length); // byte[]

  14. - nadawca TcpClient client = newTcpClient(); client.Connect(listener, port); NetworkStream stream = client.GetStream(); stream.Write(buffer, 0, buffer.Length); // if (stream.DataAvailable) bytesRead = stream.Read(buffer, 0, buffer.Length); TCPClasses

  15. ·przesyłanie z wykorzystaniem protokółu SOAP - odbiorca NetworkStream ns = tcpclient.GetStream(); SoapFormatter channel = newSoapFormatter(); message = (string)channel.Deserialize(ns); - nadawca SoapFormatter channel = newSoapFormatter(); channel.Serialize(tcpclient.GetStream(), message); TCPSOAP

  16. ·klasy protokółów - FTP - klasa WebClient Uri libra = new Uri (@"ftp://man.put.poznan.pl/Nowy.txt"); if (libra.Scheme != Uri.UriSchemeFtp)return; WebClient request = new WebClient(); request.Credentials = new NetworkCredential(name, pass); // request.DownloadFile(libra.ToString(), @"Nowy.txt"); // request.UploadFile(libra.ToString(), @"Nowy.txt"); Get1

  17. - klasy FtpWebRequestFtpWebResponse Uri libra = newUri(@"ftp://man.put.poznan.pl"); FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(libra); request.Credentials = newNetworkCredential(name, pass); request.UseBinary = false; request.Method =WebRequestMethods.Ftp.ListDirectory; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream =response.GetResponseStream(); Get2

  18. ·klasy protokółów - HTTP, HTTPS, FILE - WebClient string address =@"http://www.cs.put.poznan.pl/jkniat/HT.html"; // @"https://poczta.put.poznan.pl" // @"file://D:/HT.html WebClient wc = newWebClient(); // Console.WriteLine(Encoding.ASCII.GetString(wc.DownloadData(address))); // wc.DownloadFile(address, "HTcopy.html"); Get1

  19. - WebRequest WebResponse string address =@"http://www.cs.put.poznan.pl/jkniat/HT.html"; WebRequest request = WebRequest.Create(address); WebResponse response = request.GetResponse(); StreamReader reader =new StreamReader(response.GetResponseStream(), Encoding.ASCII); Console.WriteLine(reader.ReadToEnd()); Get2, HTTPpage

  20. ·programowanie asynchroniczne - TCP Socket Server ManualResetEvent AcceptEvent = newManualResetEvent(false); // ManualResetEvent ReceiveEvent = newManualResetEvent(false); // synchronizacja wątków za pomocą zgłaszania // zdarzeń i oczekiwania na zdarzenia:// .WaitOne() .Set() .Reset()

  21. voidStart() { Socket srv = GetLisSocket("localhost", port); AcceptEvent.Reset(); ReceiveEvent.Reset(); srv.BeginAccept(newAsyncCallback(AcceptCallback), srv); AcceptEvent.WaitOne();// oczekiwanie na sygnał }

  22. void AcceptCallback(IAsyncResult result) {Socket temp = (Socket) result.AsyncState; Socket a = temp.EndAccept(result); asyncData.socket = a; IAsyncResult recres =a.BeginReceive(ad.RecBytes, 0, 256, 0, newAsyncCallback(ReceiveCallback), ad); ReceiveEvent.WaitOne(); // oczekiwanie AcceptEvent.Set(); // sygnał dla Start }

  23. void ReceiveCallback(IAsyncResult result) { int bytes = ad.socket.EndReceive(result); if (bytes > 0){string data =Encoding.ASCII.GetString(ad.RecBytes, 0, bytes); Console.WriteLine("Odebrano: [" + data +"]\n"); } ReceiveEvent.Set(); // sygnał dla AcceptCallback } AsyTCP

  24. - HTTP string address = @"http://www.cs.put.poznan.pl"; WebRequest request =WebRequest.Create(address); AsyncResponseData ad = newAsyncResponseData(request); IAsyncResult responseResult = request.BeginGetResponse (newAsyncCallback(ResponseCallback),ad);

  25. void ResponseCallback(IAsyncResult result) {AsyncResponseData ar = (AsyncResponseData)result.AsyncState; WebRequest request = ar.Request; WebResponse response = request.EndGetResponse(result); Stream stream = response.GetResponseStream(); AsyncReadData ad = newAsyncReadData(stream); IAsyncResult readResult =stream.BeginRead(ad.Buffer, 0,ad.Buffer.Length,newAsyncCallback(ReadCallback),ad); . . .} AsyPage

More Related