假设,我有以下用C#编写的服务器和客户端应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace SocketsServerStarter
{
class Program
{
static void Main(string[] args)
{
// create a socket to
Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipaddr = IPAddress.Any;
// end-point is a combination of ip adress and port
IPEndPoint ipep = new IPEndPoint(ipaddr, 23000);
try
{
// bind the socket to the ip end-point
listenerSocket.Bind(ipep);
// start listening.
// can handle 5 clients at a time.
listenerSocket.Listen(5);
Console.WriteLine("About to accept incoming connection.");
// block the application for synchronous operation
Socket client = listenerSocket.Accept();
Console.WriteLine("Client connected. " + client.ToString() + " - IP End Point: " + client.RemoteEndPoint.ToString());
byte[] buff = new byte[128];
int numberOfReceivedBytes = 0;
while (true)
{
numberOfReceivedBytes = client.Receive(buff);
Console.WriteLine("Number of received bytes: " + numberOfReceivedBytes);
Console.WriteLine("Data sent by client is: " + buff);
string receivedText = Encoding.ASCII.GetString(buff, 0, numberOfReceivedBytes);
Console.WriteLine("Data sent by client is: " + receivedText);
client.Send(buff);
if (receivedText == "x")
{
break;
}
Array.Clear(buff, 0, buff.Length);
numberOfReceivedBytes = 0;
}
}
catch(Exception excp)
{
Console.WriteLine(excp.ToString());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace SocketClientStarter
{
class Program
{
static void Main(string[] args)
{
Socket client = null;
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipaddr = null;
try
{
Console.WriteLine("Please Type a Valid Server IP Address and Press Enter: ");
string strIPAddress = Console.ReadLine();
Console.WriteLine("Please Supply a Valid Port Number 0 - 65535 and Press Enter: ");
string strPortInput = Console.ReadLine();
int nPortInput = 0;
if (strIPAddress == " ") strIPAddress = "127.0.0.1";
if (strPortInput == " ") strPortInput = "23000";
if (!IPAddress.TryParse(strIPAddress, out ipaddr))
{
Console.WriteLine("Invalid server IP supplied.");
return;
}
if (!int.TryParse(strPortInput.Trim(), out nPortInput))
{
Console.WriteLine("Invalid port number supplied, return.");
return;
}
if (nPortInput <= 0 || nPortInput > 65535)
{
Console.WriteLine("Port number must be between 0 and 65535.");
return;
}
System.Console.WriteLine(string.Format("IPAddress: {0} - Port: {1}", ipaddr.ToString(), nPortInput));
client.Connect(ipaddr, nPortInput);
Console.WriteLine("Connected to the server, type text and press enter to send it to the srever, type <EXIT> to close.");
string inputCommand = string.Empty;
while(true)
{
inputCommand = Console.ReadLine();
if(inputCommand.Equals("<EXIT>"))
{
break;
}
byte[] buffSend = Encoding.ASCII.GetBytes(inputCommand);
client.Send(buffSend);
byte[] buffReceived = new byte[128];
int nRecv = client.Receive(buffReceived);
Console.WriteLine("Data received: {0}", Encoding.ASCII.GetString(buffReceived, 0, nRecv));
}
}
catch (Exception excp)
{
Console.WriteLine(excp.ToString());
}
finally
{
if (client != null)
{
if (client.Connected)
{
client.Shutdown(SocketShutdown.Both);
}
client.Close();
client.Dispose();
}
}
Console.WriteLine("Press a key to exit...");
Console.ReadKey();
}
}
}
通常,要测试它们,某人需要做的是同时运行两个程序并观察它们的交互。
我想单独测试它们。即无需同时运行它们。
我该怎么做?
可以使用telnet吗?如果是,怎么办?