bytesSendStr=Encoding.ASCII.GetBytes(sendStr);
try
{
//向主机发送请求
socket.Send(bytesSendStr,bytesSendStr.Length,0);
}
catch(Exception ce)
{
MessageBox.Show("发送错误:"+ce.Message,"提示信息
,MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
}
接收的时候重新编码就可以了!
try
{
//向主机发送请求
socket.Send(bytesSendStr,bytesSendStr.Length,0);
}
catch(Exception ce)
{
MessageBox.Show("发送错误:"+ce.Message,"提示信息
,MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
}
接收的时候重新编码就可以了!
解决方案 »
- 小弟新学C#,有个示例始终调试失败,自己也检查不出来原因,现把代码发上来,求大家指点
- 在线等关于net文件下载无需activx插件的方法
- 推荐几个学习C#类的几个例子
- 高分求教:如何使一个DataTable的Columns成为另外一个table的具体内容
- 请问Web service在vs2005下发布生成dll文件问题?
- 动态填充在DataGrid模板列中的RadioButtonList控件
- Console.WriteLine 这句是怎么用呀,有没有全面的资料,关于c#函数,或语句用法?
- SendKeys.SendWai向QQ密码框输入密码
- 请问大家,如何操作和读取数据库的字段。(OleDbDataAdapter 连接)
- 我想问问C#和asp+
- 想找一个c#版本的关于项目管理的东东,请lr赐教
- 有人用.net做过视频传输,流媒体方面的东西么,进来帮帮忙
{
//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;
}
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;public class mySocket
{
private static Socket connectSocket(string server, int port)
{
Socket s = null;
IPHostEntry iphe = null;
try
{
// Get host related information.
iphe = Dns.Resolve(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception to be thrown if the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress ipad in iphe.AddressList)
{
IPEndPoint ipe = new IPEndPoint(ipad, port); Socket tmpS =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp); tmpS.Connect(ipe); if(tmpS.Connected)
{
s = tmpS;
break;
}
else
continue;
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
return s; }
// This method requests the home page content for the passed server.
// It displays the number of bytes received and the page content.
private static string socketSendReceive(string server, int port)
{
//Set up variables and 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; // Create a socket connection with the specified server and port.
Socket s = connectSocket(server, port); if (s == null)
return ("Connection failed");
// Send request to the server.
s.Send(ByteGet, ByteGet.Length, 0);
// Receive the server home page content.
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
// Read the first 256 bytes.
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}
return strRetPage;
}
public static void Main(string[] args)
{
string host;
int port = 80; if (args.Length == 0)
// If no server name is passed as argument to this program, use the current
// host name as default.
host = Dns.GetHostName();
else
host = args[0];
string result = socketSendReceive(host, port);
Console.WriteLine(result);
}
}