从客户端发送数据到服务器工作,但服务器到客户端不工作

时间:2012-05-30 11:39:41

标签: 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.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;
            }
    }
}

但是没有发送任何内容(没有运行时异常)。

我错过了什么?

2 个答案:

答案 0 :(得分:1)

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

服务器:

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();
                }
            }

        }
    }
}

下面的屏幕截图显示了工作:

Screenshot of client/server working

答案 1 :(得分:0)

昨天我遇到了类似的问题。以下是问题 Read data from socket, send response and close

它的最后一个答案是,虽然因为我需要等待2天才能接受它。

希望这会有所帮助