📄 ch8_13.cs
字号:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class CH8_13
{
public static void SendRequest( string server, int port, string cmd )
{
Encoding ASCII = Encoding.ASCII;
Byte[] ByteGet = ASCII.GetBytes(cmd);
Byte[] RecvBytes = new Byte[256];
IPAddress hostadd = DNS.Resolve(server);
IPEndPoint EPhost = new IPEndPoint(hostadd, port);
Socket s = new Socket(AddressFamily.AfINet, SocketType.SockStream,
ProtocolType.ProtTCP );
// Connect to host using IPEndPoint
if (s.Connect(EPhost) != 0)
{
Console.WriteLine("Unable to connect to host!");
return;
}
// Sent the command to the host
s.Send(ByteGet, ByteGet.Length, 0);
// Receive the response
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
string strRetPage = ASCII.GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
if ( bytes > 0 )
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}
Console.WriteLine("Received: {0}", strRetPage );
}
public static void Main(string[] args)
{
if ( args.Length < 3 )
{
Console.WriteLine("Usage: ch8_13 server port cmd");
return;
}
// Get the server name from the arguments
string server = args[0];
// Get the port id from the arguments
int port = args[1].ToInt16();
SendRequest( server, port, args[2] );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -