服务器启动方法抛出异常,端口已在使用?

时间:2016-05-28 21:06:12

标签: c# server client port

我正在创建一个服务器 - 客户端应用程序,服务器和客户端可以相互通信。 当我在服务器上调用start方法时,我收到一条错误信息:

Only one usage of each socket address (protocol/network address/port) is normally permitted

这是Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace NetworkingTest {
    class Program {
        static void Main(string[] args) {

            bool readLine = true;
            string input = "";

            while (true) {
                if (readLine == true) {
                    input = Console.ReadLine();
                }

                if (input == "server") {
                    Server server = new Server(IPAddress.Any, 12346);
                    readLine = false;
                }

                if (input == "client") {
                    Client client = new Client(IPAddress.Parse("myipv4"), 12346);
                    readLine = false;
                }
            }

        }
    }
}

这是Server.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace NetworkingTest {
    class Server {
        public TcpListener server;

        public Server (IPAddress ip, int port) {
            server = new TcpListener(ip, port);
            server.Start();

            Thread serverRunThread = new Thread(new ThreadStart(RunServer));

            serverRunThread.Start();
        }

        void RunServer () {
            while (true) {
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Client connected!");

                Thread serverHandlerThread = new Thread(new ParameterizedThreadStart(HandleClient));
                serverHandlerThread.Start(client);
            }
        }

        void HandleClient(object c) {
            TcpClient client = (TcpClient)c;
            NetworkStream stream = client.GetStream();

            int i;
            string data = null;
            byte[] bytes = new byte[256];

            while((i = stream.Read(bytes, 0, bytes.Length)) != 0)  {   
              data = Encoding.ASCII.GetString(bytes, 0, i);
              Console.WriteLine("Received: " + data);       
            }

            stream.Close();
            client.Close();
        }
    }
}

这是Client.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace NetworkingTest {
    class Client {
        public TcpClient client;

        public Client (IPAddress ip, int port) {
            client = new TcpClient();
            client.Connect(ip, port);

            Thread thread = new Thread(new ThreadStart(ConnectToServer));
            thread.Start();
        }

        void ConnectToServer () {
            while (true) {
                string input = Console.ReadLine();

                if (input == "exit" || input == "quit" || input == "close") {
                    break;
                } else {
                    SendMessageToServer(input);
                }
            }
        }

        void SendMessageToServer (string message) {
            NetworkStream stream = client.GetStream();
            byte[] bytes = Encoding.ASCII.GetBytes(message);
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
        }
    }
}

编辑: Program.cs中存在问题,在循环时修复:

        while (readLine == true) {
            input = Console.ReadLine();

            if (input == "server") {
                Server server = new Server(IPAddress.Any, 12346);
                readLine = false;
            }

            if (input == "client") {
                Client client = new Client(IPAddress.Parse("10.0.0.5"), 12346);
                readLine = false;
            }
        }

1 个答案:

答案 0 :(得分:1)

由于while(true)循环中的错误,会发生这种情况。首先,您向用户询问Console.ReadLine()行。如果input是"服务器"你启动你的服务器,然后你去你的while循环的开头,你的readLine变量是假的,input仍然是"服务器",所以它创建第二个服务器(基本上你有无限循环)。由于此端口上已有一个服务器 - 第二次尝试失败并显示您看到的错误。

要修复,删除while循环,或者更正确地执行此操作,如下所示:

static void Main(string[] args) {
    while (true) {
        Console.WriteLine("Type \"server\" to start server, type \"client\" to start client, type \"exit\" to exit");
        string input = Console.ReadLine();

        if (input == "server") {
            Server server = new Server(IPAddress.Any, 12346);
        }
        else if (input == "client") {
            Client client = new Client(IPAddress.Parse("myipv4"), 12346);
        }
        else if (input == "exit")
            return;
    }
}