【问题标题】:TCP Server in C#C#中的TCP服务器
【发布时间】:2011-10-31 15:58:18
【问题描述】:

我只是一个初学者套接字编程。我尝试了“程序员 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:每个套接字地址通常只允许使用一次

然后程序关闭。怎么办?

【问题讨论】:

  • “它不起作用” => 什么不起作用?如果您不提供有关问题的详细信息,您尝试过的内容...,人们不会费心阅读您的代码(顺便说一句)
  • “It doesn't work”不是一个很好的问题报告。
  • 我有客户端程序也可以连接到这个服务器程序。实际问题是这个服务器程序没有运行。在第 16-22 行有代码 try { // 创建一个 TCPListener 来接受客户端连接监听器 = new TcpListener(IPAddress.Any, servPort); listener.Start(); } catch (SocketException se) { Console.WriteLine(se.ErrorCode + ": " + se.Message); Environment.Exit(se.ErrorCode);和程序显示错误代码并显示这样的消息 10048:每个套接字地址 通常只允许使用一次,并且程序关闭要做什么
  • 我是新人,所以我犯了错误。但我知道“谁什么都不做就不会出错,谁不犯错就什么都学不到”。
  • @Dinesh - 不用担心,不幸的是这里有一些白痴以让别人看起来很愚蠢而自豪。幸运的是,这里有很多才华横溢的人会竭尽全力帮助您。不要让白痴让你失望并继续插电。

标签: c# network-programming client-server tcplistener socketexception


【解决方案1】:

您在new TcpListener(IPAddress.Any, servPort); 中指定的端口似乎正在使用中,这是不允许的(只有一个程序可以侦听特定端口)。

这可能是因为您有多个正在运行的服务器程序实例,也可能是因为它被另一个程序使用。例如,如果您的计算机上正在运行 Web 服务器,则通常使用端口 80。

尝试选择另一个端口号(例如 10000)。我会避免使用较低的端口号(尤其是低于 1024),因为这些端口号被众所周知的程序(Web 服务器、邮件服务器等 - 请参阅 this wikipedia page 了解更多信息)使用)

【讨论】:

  • @Dinesh:不要忘记为有帮助的答案投票并接受为您提供解决方案的答案。
【解决方案2】:

如果您使用 Visual Studio 2010 编译并运行它(通过调试),您应该根据代码需要设置命令行参数。

如果您已经使用命令行参数运行了程序(通过控制台或在 VS 中设置它们),那么请更详细地解释它为什么不起作用。

要设置命令行参数,请在解决方案资源管理器中右键单击您的项目 -> 属性,然后在“调试”选项卡中的“命令行参数”字段中键入一个表示端口的值。没有空格,只有一个值。

【讨论】:

    猜你喜欢
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2017-01-27
    • 1970-01-01
    • 2020-04-15
    • 2016-09-16
    相关资源
    最近更新 更多