Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据从客户端发送到服务器起作用,但从服务器发送到客户端不起作用_C#_Sockets_Chat_Tcpclient - Fatal编程技术网

C# 将数据从客户端发送到服务器起作用,但从服务器发送到客户端不起作用

C# 将数据从客户端发送到服务器起作用,但从服务器发送到客户端不起作用,c#,sockets,chat,tcpclient,C#,Sockets,Chat,Tcpclient,我知道对网络流执行Flush操作没有任何作用 这就是为什么我定义了StreamWriter,它将网络流作为构造器参数,然后我试图向客户发送s信函,告诉他连接成功 private StreamReader srReceiver; private StreamWriter swSender; // Occures when a new client is accepted private void AcceptClient() { srReceiver = new System.IO.St

我知道对网络流执行
Flush
操作没有任何作用

这就是为什么我定义了
StreamWriter
,它将网络流作为构造器参数,然后我试图向客户发送
s
信函,告诉他连接成功

private StreamReader srReceiver;
private StreamWriter swSender;

// Occures when a new client is accepted
private void AcceptClient()
{
    srReceiver = new System.IO.StreamReader(TcpClient.GetStream());
    swSender = new System.IO.StreamWriter(TcpClient.GetStream());

    if (TcpClient.Available > 0)
    {
            char [] buff0 = new char[TcpClient.Available];

            // Read the account information from the client
            srReceiver.Read(buff0, 0, TcpClient.Available);

            sUserNumber = new string(buff0);

            // We got a response from the client
            if (!string.IsNullOrEmpty(sUserNumber))
            {
                OraEntities Ent = new OraEntities();

                var user = Ent.USERS.FirstOrDefault(usr => usr.USER_NUMBER == sUserNumber);

                // Check user validity
                if (user == null)
                {
                    Console.WriteLine("No user has this number: " + sUserNumber);
                    return;
                }
                else
                {
                    // Add the user to Active Users List and start listening for messages from him
                    Server.AddUser(this, sUserNumber);

                    // S means connected successfully
                    swSender.WriteLine("S");
                    swSender.Flush();

                    // Start the new tread that hosts the listener
                    thrReceive = new Thread(HandleInput);
                    thrReceive.Start();
                }
            }
            else
            {
                CloseConnection();
                return;
            }
    }
}
但是没有任何东西在线路上发送(没有运行时异常)


我遗漏了什么?

这是我的测试代码,显示它正在工作:

服务器:

namespace SocketTestServer
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener lis = new TcpListener(IPAddress.Parse("127.0.0.1"), 2509);
            lis.Start();
            lis.BeginAcceptTcpClient(new AsyncCallback(acceptClient), lis);
            Console.ReadKey();
        }

        private static void acceptClient(IAsyncResult ar)
        {
            TcpListener lis = ar.AsyncState as TcpListener;
            using (TcpClient cli = lis.EndAcceptTcpClient(ar))
            {
                using (NetworkStream ns = cli.GetStream())
                {
                    byte[] toSend = Encoding.ASCII.GetBytes("S\r\n");
                    ns.Write(toSend, 0, toSend.Length);
                    Console.WriteLine("Client connected");
                }
            }
        }
    }
}
客户:

namespace SocketTestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (TcpClient client = new TcpClient())
            {
                client.Connect(IPAddress.Parse("127.0.0.1"), 2509);
                using (NetworkStream ns = client.GetStream())
                {
                    StreamReader sr = new StreamReader(ns);
                    Console.WriteLine(sr.ReadLine());
                    Console.ReadLine();
                }
            }

        }
    }
}
下面的屏幕截图显示正在工作:


这是我的测试代码,显示它正在工作:

服务器:

namespace SocketTestServer
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener lis = new TcpListener(IPAddress.Parse("127.0.0.1"), 2509);
            lis.Start();
            lis.BeginAcceptTcpClient(new AsyncCallback(acceptClient), lis);
            Console.ReadKey();
        }

        private static void acceptClient(IAsyncResult ar)
        {
            TcpListener lis = ar.AsyncState as TcpListener;
            using (TcpClient cli = lis.EndAcceptTcpClient(ar))
            {
                using (NetworkStream ns = cli.GetStream())
                {
                    byte[] toSend = Encoding.ASCII.GetBytes("S\r\n");
                    ns.Write(toSend, 0, toSend.Length);
                    Console.WriteLine("Client connected");
                }
            }
        }
    }
}
客户:

namespace SocketTestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (TcpClient client = new TcpClient())
            {
                client.Connect(IPAddress.Parse("127.0.0.1"), 2509);
                using (NetworkStream ns = client.GetStream())
                {
                    StreamReader sr = new StreamReader(ns);
                    Console.WriteLine(sr.ReadLine());
                    Console.ReadLine();
                }
            }

        }
    }
}
下面的屏幕截图显示正在工作:


我昨天遇到了类似的问题。以下是问题

这是最后一个答案,我无法接受,因为我需要等待2天


希望这会有所帮助

我昨天遇到了类似的问题。以下是问题

这是最后一个答案,我无法接受,因为我需要等待2天


希望这将有所帮助

尝试在每行之后添加一个CRLF(\r\n):swSender.WriteLine(“S\r\n”);在AcceptClient中使用围绕代码的“using(Stream s=TcpClient.GetStream()”,然后直接写入流,而不是使用流编写器,这可能是值得的。@DarrenDavies尝试过,但不起作用。@Gavin尝试过,现在我遇到异常
不允许在未连接的套接字上执行该操作,因为当我使用
using
时,网络流发生了处理。请尝试在每行之后添加一个CRLF(\r\n):swSender.WriteLine(“S\r\n”);在AcceptClient中使用围绕代码的“using(Stream s=TcpClient.GetStream()”,然后直接写入流,而不是使用流编写器,这可能是值得的。@DarrenDavies尝试过,但不起作用。@Gavin尝试过,现在我遇到异常
不允许在未连接的套接字上执行该操作,因为当我使用
using
时,网络流发生了处理。我的问题是“如果我关闭了网络流(以便可以发送数据),我如何再次连接TcpClient?”。因为,当我再次尝试使用网络流时,我收到InvalidOperationException,即我无法访问已处置的对象。我的问题是“如果我关闭了网络流(以便可以发送数据),我如何再次连接TcpClient?”。因为,当我再次尝试使用NetworkStream时,我收到InvalidOperationException,我无法访问已处置的对象。