1

客户端写入服务器 - 服务器读取。和服务器写入客户端 - 客户端未读取。

服务器:

    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
            {
                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){}
        }
    }

客户:

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) { }
    }

}
4

1 回答 1

5

首先,您不了解多线程/异步网络编程的工作原理。不要使用很多自制的代表。

其次,不要捕获该异常。仅捕获您可以处理的异常,或防止您的应用程序关闭。您可以在我的博客中阅读有关异常的更多信息:http: //blog.gauffin.org/tag/exceptions/

清理服务器:

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

清理客户端:

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

    }
}

如果您想要异步客户端/服务器阅读有关 BeginRead/EndRead、BeginWrite、EndWrite 的信息。不要与自己的代表混在一起。

于 2010-12-23T09:17:08.013 回答