1 |
|
namespace System.Web.Services.Protocols |
2 |
|
{ |
3 |
|
using System; |
4 |
|
using System.Net; |
5 |
|
using System.Net.Sockets; |
6 |
|
using System.Text; |
7 |
|
|
8 |
|
public class HttpSynchronousProtocol : ITransportSynchronousProtocol |
9 |
|
{ |
10 |
|
protected Socket sock; |
11 |
|
protected Uri source , destination; |
12 |
|
protected ProtocolEncoding encoding; |
13 |
|
protected IPHostEntry info; |
14 |
|
protected Encoding ASCII; |
15 |
|
protected Byte[] receivedData; |
16 |
|
protected String receivedASCII; |
17 |
|
|
18 |
|
public HttpSynchronousProtocol(String src, String dest) : this(new Uri(src), new Uri(dest)) |
19 |
|
{ |
20 |
|
} |
21 |
|
|
22 |
|
public HttpSynchronousProtocol(String dest) : this(new Uri(dest)) |
23 |
|
{ |
24 |
|
|
25 |
|
} |
26 |
|
|
27 |
|
public HttpSynchronousProtocol(Uri src, Uri dest) |
28 |
|
{ |
29 |
|
sock = new Socket(AddressFamily.InterNetwork, |
30 |
|
SocketType.Stream, ProtocolType.Tcp); |
31 |
|
|
32 |
|
source = src; |
33 |
|
destination = dest; |
34 |
|
info = Dns.GetHostByName(dest.ToString()); |
35 |
|
ASCII = Encoding.ASCII; |
36 |
|
receivedData = new Byte[256]; |
37 |
|
} |
38 |
|
|
39 |
|
public HttpSynchronousProtocol(Uri dest) : this(new Uri("127.0.0.1"),dest) |
40 |
|
{ |
41 |
|
|
42 |
|
} |
43 |
|
|
44 |
|
public HttpSynchronousProtocol(IPHostEntry host) |
45 |
|
{ |
46 |
|
sock = new Socket(AddressFamily.InterNetwork, |
47 |
|
SocketType.Stream, ProtocolType.Tcp); |
48 |
|
// source = src; |
49 |
|
// destination = dest; |
50 |
|
info = host; |
51 |
|
ASCII = Encoding.ASCII; |
52 |
|
receivedData = new Byte[256]; |
53 |
|
} |
54 |
|
|
55 |
|
/* Send can pass String.Empty Data */ |
56 |
|
public virtual int Send(String data) |
57 |
|
{ |
58 |
|
if(data == null) |
59 |
|
{ |
60 |
|
// throw exception |
61 |
|
} |
62 |
|
|
63 |
|
IPAddress[] ipaddress = info.AddressList; |
64 |
|
EndPoint ep = new IPEndPoint(ipaddress[0], 80); |
65 |
|
sock.Connect(ep); |
66 |
|
if(sock.Connected) |
67 |
|
{ |
68 |
|
sock.Send(ASCII.GetBytes(data), ASCII.GetBytes(data).Length, 0); |
69 |
|
} |
70 |
|
|
71 |
|
Int32 bytes = sock.Receive(receivedData, receivedData.Length, 0); |
72 |
|
receivedASCII = GetStream(bytes); |
73 |
|
sock.Close(); |
74 |
|
return 0; |
75 |
|
} |
76 |
|
|
77 |
|
internal String GetStream(Int32 bytes) |
78 |
|
{ |
79 |
|
String str = String.Empty; |
80 |
|
|
81 |
|
if(bytes == 0) |
82 |
|
{ |
83 |
|
// throw exception |
84 |
|
} |
85 |
|
|
86 |
|
while(bytes > 0) |
87 |
|
{ |
88 |
|
bytes = sock.Receive(receivedData, receivedData.Length, 0); |
89 |
|
str = str + ASCII.GetString(receivedData, 0, bytes); |
90 |
|
} |
91 |
|
|
92 |
|
return str; |
93 |
|
} |
94 |
|
|
95 |
|
public String Data |
96 |
|
{ |
97 |
|
get |
98 |
|
{ |
99 |
|
return receivedASCII; |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
|
public String Receive() |
104 |
|
{ |
105 |
|
// retrieve data and return String |
106 |
|
return String.Empty; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
public ProtocolEncoding Encoding |
111 |
|
{ |
112 |
|
get |
113 |
|
{ |
114 |
|
return 0; |
115 |
|
} |
116 |
|
set |
117 |
|
{ |
118 |
|
encoding = value; |
119 |
|
} |
120 |
|
|
121 |
|
} |
122 |
|
|
123 |
|
public Uri Source |
124 |
|
{ |
125 |
|
get |
126 |
|
{ |
127 |
|
return source; |
128 |
|
} |
129 |
|
set |
130 |
|
{ |
131 |
|
source = value; |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
public Uri Destination |
136 |
|
{ |
137 |
|
get |
138 |
|
{ |
139 |
|
return destination; |
140 |
|
} |
141 |
|
set |
142 |
|
{ |
143 |
|
destination = value; |
144 |
|
} |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
} |