我只是一个初学者学生套接字编程。我尝试了“C#程序员实用指南中的 TCP/IP 套接字”pdf 书中的一个简单代码,但它不起作用。我在 Visual Studio 2010 中编译它。请帮助我有什么问题和这是完整的代码
using System; // For Console, Int32, ArgumentException, Environment
using System.Net; // For IPAddress
using System.Net.Sockets; // For TcpListener, TcpClient
class TcpEchoServer {
private const int BUFSIZE = 32; // Size of receive buffer
static void Main(string[] args) {
if (args.Length > 1) // Test for correct # of args
throw new ArgumentException("Parameters: [<Port>]");
int servPort = (args.Length == 1) ? Int32.Parse(args[0]): 7;
TcpListener listener = null;
try {
// Create a TCPListener to accept client connections
listener = new TcpListener(IPAddress.Any, servPort);
listener.Start();
} catch (SocketException se) {
Console.WriteLine(se.ErrorCode + ": " + se.Message);
Environment.Exit(se.ErrorCode);
}
byte[] rcvBuffer = new byte[BUFSIZE]; // Receive buffer
int bytesRcvd; // Received byte count
for (;;) { // Run forever, accepting and servicing connections
TcpClient client = null;
NetworkStream netStream = null;
try {
client = listener.AcceptTcpClient(); // Get client connection
netStream = client.GetStream();
Console.Write("Handling client - ");
// Receive until client closes connection, indicated by 0 return value
int totalBytesEchoed = 0;
while ((bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length)) > 0) {
netStream.Write(rcvBuffer, 0, bytesRcvd);
totalBytesEchoed += bytesRcvd;
}
Console.WriteLine("echoed {0} bytes.", totalBytesEchoed);
// Close the stream and socket. We are done with this client!
netStream.Close();
client.Close();
} catch (Exception e) {
Console.WriteLine(e.Message);
netStream.Close();
}
}
}
}
来自评论:
我有客户端程序也可以连接到这个服务器程序。实际问题是这个服务器程序没有运行。在第 16-22 行有代码 try
{ // Create a TCPListener to accept client connections
listener = new TcpListener(IPAddress.Any, servPort);
listener.Start();
}
catch (SocketException se)
{
Console.WriteLine(se.ErrorCode + ": " + se.Message);
Environment.Exit(se.ErrorCode);
}
和程序显示错误代码并显示这样的消息
10048:每个套接字地址通常只允许使用一次
和程序关闭。该怎么办?