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