)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Socket (Ia.Cl.Model)

Public general use code classes and xml files that we've compiled and used over the years:

Search Engine Optimization (SEO) support class.

    1: using System;
    2: using System.Configuration;
    3: using System.Net;
    4: using System.Net.Sockets;
    5: using System.Text;
    6:  
    7: namespace Ia.Cl.Model
    8: {
    9:     ////////////////////////////////////////////////////////////////////////////
   10:  
   11:     /// <summary publish="true">
   12:     /// Search Engine Optimization (SEO) support class.
   13:     /// </summary>
   14:     /// <remarks> 
   15:     /// Copyright © 2019-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   16:     ///
   17:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   18:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   19:     ///
   20:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   21:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   22:     /// 
   23:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   24:     /// 
   25:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   26:     /// </remarks> 
   27:     public abstract class Socket
   28:     {
   29:         ////////////////////////////////////////////////////////////////////////////
   30:  
   31:         /// <summary>
   32:         ///
   33:         /// </summary>
   34:         public Socket() { }
   35:  
   36:         ////////////////////////////////////////////////////////////////////////////
   37:  
   38:         /// <summary>
   39:         ///
   40:         /// </summary>
   41:         public static System.Net.Sockets.Socket Connect(string server, int port)
   42:         {
   43:             System.Net.Sockets.Socket s = null;
   44:             IPHostEntry hostEntry = null;
   45:  
   46:             // Get host related information.
   47:             hostEntry = Dns.GetHostEntry(server);
   48:  
   49:             // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
   50:             // an exception that occurs when the host IP Address is not compatible with the address family
   51:             // (typical in the IPv6 case).
   52:             foreach (IPAddress address in hostEntry.AddressList)
   53:             {
   54:                 IPEndPoint ipe = new IPEndPoint(address, port);
   55:                 System.Net.Sockets.Socket tempSocket = new System.Net.Sockets.Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
   56:  
   57:                 tempSocket.Connect(ipe);
   58:  
   59:                 if (tempSocket.Connected)
   60:                 {
   61:                     s = tempSocket;
   62:                     break;
   63:                 }
   64:                 else
   65:                 {
   66:                     continue;
   67:                 }
   68:             }
   69:             return s;
   70:         }
   71:  
   72:         ////////////////////////////////////////////////////////////////////////////
   73:  
   74:         /// <summary>
   75:         ///
   76:         /// </summary>
   77:         public static string SendReceive(string server, int port)
   78:         {
   79:             string request = "GET / HTTP/1.1\r\nHost: " + server +
   80:                 "\r\nConnection: Close\r\n\r\n";
   81:             Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
   82:             Byte[] bytesReceived = new Byte[256];
   83:             string page = "";
   84:  
   85:             // Create a socket connection with the specified server and port.
   86:             using (System.Net.Sockets.Socket s = Connect(server, port))
   87:             {
   88:  
   89:                 if (s == null)
   90:                     return ("Connection failed");
   91:  
   92:                 // Send request to the server.
   93:                 s.Send(bytesSent, bytesSent.Length, 0);
   94:  
   95:                 // Receive the server home page content.
   96:                 int bytes = 0;
   97:                 page = "Default HTML page on " + server + ":\r\n";
   98:  
   99:                 // The following will block until the page is transmitted.
  100:                 do
  101:                 {
  102:                     bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
  103:                     page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
  104:                 }
  105:                 while (bytes > 0);
  106:             }
  107:  
  108:             return page;
  109:         }
  110:  
  111:         ////////////////////////////////////////////////////////////////////////////
  112:         ////////////////////////////////////////////////////////////////////////////
  113:     }
  114: }