主题:socket编程出现问题了!
小弟刚学socket编程,搞了个程序(如下),可是编译总出现system.net.sockets.socket并不包含socket的错误信息
我用的是Visual C# 2005编译器,那位高手能给指点迷津?先谢了!
//example
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class Class1
{
public string DoSocketGet(string 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 hostadd=Dns.GetHostEntry(server).AddressList[0];
IPEndPoint EPhost=new IPEndPoint(hostadd,80);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Socket(EPhost);
if(!s.Connected){
strRetPage="无法连接主机!";
return strRetPage;
}
s.Send(ByteGet,ByteGet.Length,SocketFlags.None);
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);
}
return strRetPage;
}
}
我用的是Visual C# 2005编译器,那位高手能给指点迷津?先谢了!
//example
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class Class1
{
public string DoSocketGet(string 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 hostadd=Dns.GetHostEntry(server).AddressList[0];
IPEndPoint EPhost=new IPEndPoint(hostadd,80);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Socket(EPhost);
if(!s.Connected){
strRetPage="无法连接主机!";
return strRetPage;
}
s.Send(ByteGet,ByteGet.Length,SocketFlags.None);
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);
}
return strRetPage;
}
}