簡體   English   中英

TcpClient:在未連接的套接字上不允許操作 - C# 4.5

[英]TcpClient: Operation not allowed on non-connected Sockets - C# 4.5

為什么以下代碼會給出以下異常?

The operation not allowed on non-connected sockets.

這個鏈接說:

您必須先Connect()才能獲得NetworkStream

文檔通常非常適合這種東西。 GetStream幫助中的異常下,您將看到:

InvalidOperationException : TcpClient未連接到遠程主機。

在這里,我的客戶也在做同樣的事情。

那么,出了什么問題?

在此處輸入圖片說明

客戶端代碼:

using System;
using System.Net.Sockets;
using System.IO;

namespace WhoIsClientNamespace
{
    public class WhoIsClient
    {
        static void Main()
        {
            Console.Title = "Client";
            string kbInput;
            try
            {
                TcpClient client = new TcpClient();
                client.Connect("localHost", 43);
                Console.WriteLine("Client started...");
                Console.WriteLine("Enter command to the server!");
                Console.WriteLine(" # ");

                while (true)
                {
                    kbInput = Console.ReadLine();
                    Console.WriteLine(kbInput);

                    if ("exit" == kbInput)
                    {
                        Console.WriteLine("while loop exited!");
                        break;
                    }

                    string[] args = kbInput.Split(' ');

                    if (args.Length < 1)
                    {
                        Console.WriteLine("Provide more than one Args");
                    }
                    else if (args.Length == 1)
                    {
                        StreamWriter sw = new StreamWriter(client.GetStream());
                        StreamReader sr = new StreamReader(client.GetStream());
                        string str = kbInput;
                        sw.WriteLine(kbInput);
                        sw.Flush();
                        Console.WriteLine(kbInput[0] + " is " + sr.ReadToEnd());
                        return;
                    }
                    else if (args.Length > 1)
                    {
                        StreamWriter sw = new StreamWriter(client.GetStream());
                        StreamReader sr = new StreamReader(client.GetStream());
                        string str = kbInput;
                        sw.WriteLine(str);
                        sw.Flush();
                        //Console.WriteLine(kbInput[0] + " location changed to be " + args[1]);
                        sw.Close();
                    }
                    else
                    {
                        Console.WriteLine("Invalid args ");
                    }
                }                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("Client exits!");
        }
    }
}

服務器代碼:

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;

namespace WhoIsServerNamespace
{
    public class WhoIsServer
    {
        static Dictionary<string, string> 
            _locationsDictionary = new Dictionary<String, String>();
        static void Main(string[] args)
        {
            Console.Title = "Server";
            runServer();
        }
        static void runServer()
        {
            TcpListener listener;
            Socket connection;
            NetworkStream socketStream;
            try
            {
                listener = new TcpListener(IPAddress.Any, 43);
                listener.Start();

                Console.WriteLine("server started listening");

                while (true)
                {
                    connection = listener.AcceptSocket();
                    socketStream = new NetworkStream(connection);
                    Console.WriteLine("Connection Received");
                    doRequest(socketStream);
                    socketStream.Close();
                    connection.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception:" + e.ToString());
            }
        }

        static void doRequest(NetworkStream socketStream)
        {
            try
            {
                StreamWriter sw = new StreamWriter(socketStream);
                StreamReader sr = new StreamReader(socketStream);
                String strClientInput = sr.ReadLine();
                Console.WriteLine("Respond Received:" + strClientInput);
                String[] clientInputsSplitted = strClientInput.Split(new char[] { ' ' }, 2);
                String name;
                String location;

                if (clientInputsSplitted.Length < 1)
                {
                    Console.WriteLine("Too little words was inputted");
                }
                else if (clientInputsSplitted.Length == 1)
                {/* when we receive only one command-string from client,
                    that is considered as the name of the server.
                    So, the server returns its corresponding location. */
                    name = clientInputsSplitted[0];//obtain the name from the client-command-string.
                    if (_locationsDictionary.ContainsKey(name))//Is the [name] in the dictionary?
                    {
                        Console.WriteLine(name + "'s location is :"+ _locationsDictionary[name]);
                        sw.WriteLine(_locationsDictionary[name]);//return location to the client.
                    }
                    else
                    {
                        Console.WriteLine("Error: [name] not found in the dictionary");
                        sw.WriteLine("Error: [name] not found in the dictionary");
                    }
                    sw.Flush();
                }
                else if (clientInputsSplitted.Length == 2)
                {/* when we receive two command-string from client,
                    that is considered as the (name, location) pair of the server.
                    So, the server returns its corresponding location. */
                    name = clientInputsSplitted[0];
                    location = clientInputsSplitted[1];

                    if (_locationsDictionary.ContainsKey(name))
                    {
                        _locationsDictionary[name] = location;
                        Console.WriteLine("(" + name + "," + location + ") updated");
                        sw.WriteLine("("+name +","+ location +") updated");
                    }
                    else
                    {
                        _locationsDictionary.Add(name, location);
                        Console.WriteLine("(" + name + "," + location + ") added");
                        sw.WriteLine("(" + name + "," + location + ") added");
                    }
                    sw.Flush();
                }

                if (strClientInput.Contains("-h0"))
                {
                    Console.WriteLine("Is                ok");
                    sw.WriteLine("Is ok");
                }
                else if (strClientInput.Contains("-h9"))
                {
                    Console.WriteLine("Hey you're progressing");
                    sw.WriteLine("Hey you're progressing");
                }
                else if (strClientInput.Contains("-h1"))
                {
                    Console.WriteLine("We're Done");
                    sw.WriteLine("We're Done");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong");
                Console.WriteLine(e.Message);
            }
        }
    }
}

看起來您的服務器端實現在doRequest之后關閉了套接字,因此對於客戶端,您需要為每個輸入創建新的 TcpClient 實例。 或者重寫服務器端代碼以保持套接字打開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM