[ASP.NET]使用C#开发Socket通讯
2004-11-17 23:47
666 查看
下面的示例显示如何使用 Socket 类向 HTTP 服务器发送数据和接收响应。
[C#]
public string DoSocketGet(string server)
{
//Sets up variables and a string to write to the server
Encoding ASCII = Encoding.ASCII;//此处如果需要传输中文字串,需要设置成Encoding.Default
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;//此处如果需要传输中文字串,需要设置成Encoding.Default
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;
}
相关文章推荐
- [ASP.NET]按键跳转以及按Enter以不同参数提交,及其他感应事件
- [ASP.NET]如何点击按钮弹出新窗口,输入数据后返回并刷新页面?(C#代码)
- [ASP.NET][原创]如何在客户端调用服务端代码
- 我的web+asp攻击
- C#中做的一个windows控件放入ASP.NET页面
- Asp之Request对象(1)
- 使用系统数据源配置IIS+InterDev+ASP步骤
- 浅谈在ASP.NET中数据有效性校验的方法
- asp.net DataGrid 多行编辑出现问题
- Asp.net动态生成html页面
- ASP.NET开发闲谈
- 如何建立VB、ASP等应用程序的OCX图表
- 不知道asp.net 1.1中怎么设置默认button
- Asp.net无法从数库中读取存在数据库中的TextBox中的回车与空格的解决方案!
- 存储过程介绍及asp存储过程的使用
- [ASP.net]从数据库中读取数据自动生成XML
- 在 ASP.NET 中执行 URL 重写
- asp.net Datagrid 资源
- 在ASP文件中调用DLL
- 如何把ASP编写成DLL