使用C#开发Socket通讯
2015-03-22 00:38
155 查看
下面的示例显示如何使用 Socket 类向 HTTP 服务器发送数据和接收响应。
[C#]
public string DoSocketGet(string server)
{
//Sets up variables and a string to write to the server
Encoding ASCII = Encoding.ASCII;
string Get = "GET / HTTP/1.1/r/nHost: " + server +
"/r/nConnection: Close/r/n/r/n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;
// IPAddress and IPEndPoint represent the endpoint that will
// receive the request.
// Get the first IPAddress in the list using DNS.
IPAddress hostadd = Dns.Resolve(server).AddressList[0];
IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
//Creates the Socket for sending data over TCP.
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
// Connects to the host using IPEndPoint.
s.Connect(EPhost);
if (!s.Connected)
{
strRetPage = "Unable to connect to host";
return strRetPage;
}
// Sends the GET text to the host.
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
// Receives the page, looping until all bytes are received
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = "Default HTML page on " + server + ":/r/n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}
//如果想立即关闭连接则调用 s.Close();
return strRetPage;
}
[C#]
public string DoSocketGet(string server)
{
//Sets up variables and a string to write to the server
Encoding ASCII = Encoding.ASCII;
string Get = "GET / HTTP/1.1/r/nHost: " + server +
"/r/nConnection: Close/r/n/r/n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;
// IPAddress and IPEndPoint represent the endpoint that will
// receive the request.
// Get the first IPAddress in the list using DNS.
IPAddress hostadd = Dns.Resolve(server).AddressList[0];
IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
//Creates the Socket for sending data over TCP.
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
// Connects to the host using IPEndPoint.
s.Connect(EPhost);
if (!s.Connected)
{
strRetPage = "Unable to connect to host";
return strRetPage;
}
// Sends the GET text to the host.
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
// Receives the page, looping until all bytes are received
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = "Default HTML page on " + server + ":/r/n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}
//如果想立即关闭连接则调用 s.Close();
return strRetPage;
}
相关文章推荐
- 使用C#开发Socket通讯
- [ASP.NET]使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发一个简单的P2P应用
- 圣殿祭司:ASP.NET 2.0 开发详解——使用C#——全国购书地址一览表
- 用C#开发Windows服务监控系统使用
- 使用C#和Excel进行报表开发(二)-操作统计图(Chart)
- 使用C#开发SmartPhone程序入门
- 使用C#和Excel进行报表开发(五)-操作单元格边框和颜色
- 为什么使用C#开发游戏?
- 使用C#进行基于PI的开发(二)--常用对象的学习
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)
- 使用C#开发Socket通讯
- (引用)使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表
- 70-316 使用Microsoft Visual C# .NET and Microsoft Visual Studio .NET 开发及实现基于Windows的应用程序 考点整理
- 使用C#进行基于PI的开发(三)——应用PISDK和PIAPI从PI数据库读取数据
- 使用C#开发一个简单的P2P应用