首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TcpServer -发送和接收数据

TcpServer -发送和接收数据
EN

Stack Overflow用户
提问于 2009-10-16 17:13:38
回答 1查看 2.9K关注 0票数 3

我不得不为工作中的一个项目编写一个TCP服务器,我在谷歌上搜索了一下如何。我得到了一个MSDN答案,并设法让它工作,但我不知道如何让数据能够来回接收。套接字的连接时间最多不会超过30秒(因为我的老板不希望套接字在这里的计算机上保持打开状态)。这个类的目标是在我的主程序中提供一种快速的方法来检索关于计算机及其用户的数据。

根据命名空间的命名,您可能会猜到它是为一个计算机实验室准备的。

代码语言:javascript
复制
namespace LabAssist.Server.Common.Objects {
    using Atlantis.Net.Sockets;

    using LabAssist.Server.Common.Data;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Windows.Forms;

    public class TcpServer {

        #region Constructor(s)

        public TcpServer(IPEndPoint endPoint) {
            RemoteEndPoint = endPoint;
            Host = new Socket(RemoteEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        }

        public TcpServer(String hostNameOrIpAddress, Int32 port) {
            RemoteEndPoint = new IPEndPoint(Dns.GetHostEntry(hostNameOrIpAddress).AddressList[0], port);
            Host = new Socket(RemoteEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        }

        public TcpServer(Int32 port) {
            RemoteEndPoint = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], port);
            Host = new Socket(RemoteEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        }

        #endregion

        #region Fields

        private Boolean m_IsBound = false;
        private List<Socket> m_Connections = new List<Socket>(50); // allow 50 sockets 
        private static System.Threading.ManualResetEvent AllDone = new System.Threading.ManualResetEvent(false);

        #endregion

        #region Properties

        private Int32 m_Backlog = 32;
        /// <summary>
        ///     Gets or sets the number of connections the host can accept at any given point
        /// </summary>
        public Int32 Backlog {
            set {
                m_Backlog = value;
            }
            get {
                return m_Backlog;
            }
        }

        private Socket m_Host = null;
        /// <summary>
        ///     Gets or sets the host master socket
        /// </summary>
        public Socket Host {
            private set {
                m_Host = value;
            }
            get {
                return m_Host;
            }
        }

        private Int32 m_Port = 1337;
        /// <summary>
        ///     Gets or sets the binding port for the server
        /// </summary>
        public Int32 Port {
            set {
                m_Port = value;
            }
            get {
                return m_Port;
            }
        }

        private IPEndPoint m_EndPoint = null;
        /// <summary>
        ///     Gets or sets the binding address to be used when binding the socket
        /// </summary>
        public IPEndPoint RemoteEndPoint {
            private set {
                m_EndPoint = value;
            }
            get {               // follows a singleton pattern with a private-setter.
                if (m_EndPoint == null) {
                    RemoteEndPoint = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], Port);
                }

                return m_EndPoint;
            }
        }

        #endregion

        #region Methods

        private void AcceptCallback(IAsyncResult ar) {
            Socket client = ((Socket)ar.AsyncState);
            Socket handler = client.EndAccept(ar);
            m_Connections.Add(handler);

            AllDone.Set();

            Console.WriteLine("Client accepted.\t Remote address and port : {0}", handler.RemoteEndPoint.ToString());

            Byte[] buf = Encoding.ASCII.GetBytes("hello world. This is my first TCP Server >:)");
            Int32 ret = 0;
            Boolean ext = false;
            //try-catch temporary until sending is figured out. ><
            try {
                ret = client.Send(buf, buf.Length, SocketFlags.None);
            } catch (Exception ex) {
                ext = true;
                ConsoleColor c = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(ex.ToString());
                Console.ForegroundColor = c;
            }
            // error check for debugging
            if (ret > 0) {
                Console.WriteLine("Sent -> {0}", Encoding.ASCII.GetString(buf, 0, buf.Length));
            } else {
                if (ext) {
                    ConsoleColor c = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Caught an exception");
                    Console.ForegroundColor = c;
                }
                Console.WriteLine("Failed to send welcome packet to client.");
            }

            State state = new State();
            state.WorkSocket = handler;
            handler.BeginReceive(state.Buffer, 0, State.BufferSize, 0, new AsyncCallback(ReceiveDataCallback), state);
        }

        /// <summary>
        ///     Intialises the socket to listen and begins to accept connections
        /// </summary>
        /// <returns></returns>
        public void Initialise() {
            Host.Bind(RemoteEndPoint);

            Console.WriteLine("Local address and port : {0}", RemoteEndPoint.ToString());

            m_IsBound = true;
            Host.Listen(Backlog);
            try {
                while (true) {
                    AllDone.Reset();

                    Console.WriteLine("Awaiting{0} client connection...", (m_Connections.Count > 0 ? " another" : ""));

                    Host.BeginAccept(new AsyncCallback(AcceptCallback), Host);

                    AllDone.WaitOne();

                    Application.DoEvents();
                }
            } catch (Exception e) {
                Log.HandledException(e);
            }
        }

        private void ReceiveDataCallback(IAsyncResult ar) {
            State state = ((State)ar.AsyncState);
            Socket handler = state.WorkSocket;

            if (!handler.IsConnected()) {
                return;
            }

            Int32 read = handler.EndReceive(ar);

            if (read > 0) {
                state.DataReceived.Append(Encoding.ASCII.GetString(state.Buffer, 0, read));
                handler.BeginReceive(state.Buffer, 0, State.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveDataCallback), state);
            } else {
                if (state.DataReceived.Length > 1) {
                    String content = state.DataReceived.ToString();
                    Console.WriteLine("Read {0} bytes from socket.\n Data: {1}", content.Length, content);
                }
                handler.Close();
            }
        }

        #endregion
    }
}

目前,我正在尝试一个简单的版本,在connect上,客户端从服务器接收一个数据包,内容类似于"hello world from the net!“。我总是收到一些奇怪的异常。上面的代码只是从MSDN直接“复制”(可以这么说)。在编写它的时候,我使用了MSDN's example作为指导,并且仍然有一些工作要做(就像移除它使用的阻塞特性一样)。现在,我只想知道如何从主机向连接的客户端发送数据!:(

代码语言:javascript
复制
System.Net.Sockets.SocketException: A request to send or receive data was disall    owed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 size, SocketFlags socketFlags)
   at LabAssist.Server.Common.Objects.TcpServer.AcceptCallback(IAsyncResult ar) in F:\Source\ACCL\Lab Suite\Code\LabAssist.Server\Common\Objects\TcpServer.cs:li
ne 119
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-10-17 02:57:27

我建议使用UDP而不是TCP的手动ACK命令,这样你就不会像你的老板一样得到常量连接的套接字。UDP Information

我想说的另一点是,不要使用原始套接字作为您的第一个TCP连接,使用TCPClient类。TCPClient

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1579275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档