开发者

How reading messages from Server? (tcp)

Client write to server - server read. and Server write to client - client not read.

Server:

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

    class SocketServer
    {

        public static void Main()
        {

            StreamReader streamReader;
            NetworkStream networkStream;

            TcpListener tcpListener = new TcpListener(5555);
            tcpListener.Start();

            Console.WriteLine("The Server has started on port 5555");
            Socket serverSocket = tcpList开发者_开发知识库ener.AcceptSocket();

            try
            {
                if (serverSocket.Connected)
                {
                    Console.WriteLine("Client connected");
                    networkStream = new NetworkStream(serverSocket);

                    AsynchronousMethodDelegate asyncDeleg = new AsynchronousMethodDelegate(AsynchronousMethod);
                    AsyncCallback callback = new AsyncCallback(CallbackMethod);
                    asyncDeleg.BeginInvoke(networkStream, callback, asyncDeleg);
                    streamReader = new StreamReader(networkStream);
                    while (true)
                    {
                        Console.WriteLine(streamReader.ReadLine());
                    }

                }

                if (serverSocket.Connected)
                    serverSocket.Close();

                Console.Read();
            }

            catch (SocketException ex)
            {
                Console.WriteLine(ex);
            }
            catch (IOException ex) { Console.WriteLine(ex); }
        }



        private static void CallbackMethod(IAsyncResult ar)
        {
            AsynchronousMethodDelegate asyncDeleg = (AsynchronousMethodDelegate)ar.AsyncState;
        }
        private delegate void AsynchronousMethodDelegate(NetworkStream networkStream);
        private static void AsynchronousMethod(NetworkStream networkStream)
        {
            try
            {
                StreamWriter streamWriter;
                streamWriter = new StreamWriter(networkStream);
                while (true)
                {
                    streamWriter.WriteLine(Console.ReadLine());
                }
            }
            catch (SocketException ex){}
        }
    }

Client:

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

class SocketClient
{
    static void Main(string[] args)
    {
        TcpClient tcpClient;
        NetworkStream networkStream;

        StreamWriter streamWriter;

        try
        {
            tcpClient = new TcpClient("localhost", 5555);
            networkStream = tcpClient.GetStream();

            AsynchronousMethodDelegate asyncDeleg = new AsynchronousMethodDelegate(AsynchronousMethod);
            AsyncCallback callback = new AsyncCallback(CallbackMethod);
            asyncDeleg.BeginInvoke(networkStream, callback, asyncDeleg);


            streamWriter = new StreamWriter(networkStream);
            while (true)
            {
                streamWriter.WriteLine(Console.ReadLine());
                streamWriter.Flush();
                //Console.WriteLine(streamReader.ReadLine());
            }
            //Console.Read();
        }
        catch (SocketException ex){
            Console.WriteLine(ex);
        }

    }

    private static void CallbackMethod(IAsyncResult ar)
    {
        AsynchronousMethodDelegate asyncDeleg = (AsynchronousMethodDelegate)ar.AsyncState;
        //lblCallback.Text = asyncDeleg.EndInvoke(ar);
    }

    private delegate void AsynchronousMethodDelegate(NetworkStream networkStream);

    private static void AsynchronousMethod(NetworkStream networkStream)
    {
        try
        {
             StreamReader streamReader = new StreamReader(networkStream);
            while (true)
            {

                Console.WriteLine(streamReader.ReadLine());
            }
        }
        catch (SocketException ex) { }
        catch (IOException ioex) { }
    }

}


First of all, you are not understanding how multithreaded/asynchronous network programming works. Don't use a lot of homebrewn delegates.

Second, do not catch that exception.. Only catch exceptions that you can handle, or to prevent your application from shutting down. You can read more about exceptions in my blog: http://blog.gauffin.org/tag/exceptions/

Cleaned up server:

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

class SocketServer
{

    public static void Main()
    {

        StreamReader streamReader;
        NetworkStream networkStream;

        TcpListener tcpListener = new TcpListener(5555);
        tcpListener.Start();

        Console.WriteLine("The Server has started on port 5555");
        Socket serverSocket = tcpListener.AcceptSocket();

        try
        {
            Console.WriteLine("Client connected");
            networkStream = new NetworkStream(serverSocket);

            streamReader = new StreamReader(networkStream);
            while (true)
            {
                var line = streamReader.ReadLine();
                Console.WriteLine(line);

                //echoing
                var buffer = Encoding.ASCII.GetBytes(line);
                networkStream.Write(buffer, 0, buffer.Length);
            }

            serverSocket.Close();
            Console.Read();
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}

Cleaned up client:

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

class SocketClient
{
    static void Main(string[] args)
    {
        TcpClient tcpClient;
        NetworkStream networkStream;
        StreamWriter streamWriter;

        try
        {
            tcpClient = new TcpClient("localhost", 5555);
            networkStream = tcpClient.GetStream();

            streamWriter = new StreamWriter(networkStream);
            while (true)
            {
                streamWriter.WriteLine(Console.ReadLine());
                streamWriter.Flush();
                Console.WriteLine(streamReader.ReadLine());
            }
            Console.Read();
        }
        catch (SocketException ex){
            Console.WriteLine(ex);
        }

    }
}

If you want asynchronous client/server read about BeginRead/EndRead, BeginWrite,EndWrite. Do not mix with your own delegates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜