使用C#开发Socket通讯
2007-05-09 12:37
120 查看
下面的示例显示如何使用 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通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- [ASP.NET]使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 如何使用C#进行Visio二次开发-总结版
- 使用Assembly来开发c#程序
- 京东价格监控软件开发技术探讨三:使用C#模拟京东登录
- 使用C#作为Cocos2dx引擎脚本语言进行游戏开发——C#脚本简介(一)
- 【项目实例】使用C#开发纽曼USB来电通来电弹屏客户端小结
- 使用C#开发一个简单的P2P应用
- 使用C#开发COM+组件
- C#开发工具使用说明
- C# 开发和使用中的23个技巧
- 使用C#和Excel进行报表开发(七)-设置单元格的显示格式
- 如何使用C#开发“类ActiveX组件”
- 使用C#开发ActiveX控件全攻略(2)
- C#开发微信门户及应用-使用地理位置扩展相关应用
- 在 Windows Server 2003 SP2 下部署使用 C# 开发的 MMC 控制台程序