Example #1
0
        private void ListenTcp()
        {
            int ReceiveDataBytesCount;

            byte[] ReceiveDataBuffer;
            while (true)
            {
                try
                {
                    ReceiveDataBuffer = new byte[1024];
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        do
                        {
                            ReceiveDataBytesCount = tcpSocket.Receive(ReceiveDataBuffer, ReceiveDataBuffer.Length, SocketFlags.None);
                            memoryStream.Write(ReceiveDataBuffer, 0, ReceiveDataBytesCount);
                        }while (tcpSocket.Available > 0);
                        if (ReceiveDataBytesCount > 0)
                        {
                            ReceiveMessageEvent(messageSerializer.Deserialize(memoryStream.ToArray()));
                        }
                    }
                }
                catch (SocketException)
                {
                    ClientDisconnectedEvent(this);
                    FunctionsCommon.CloseAndNullSocket(ref tcpSocket);
                    FunctionsCommon.CloseAndNullThread(ref listenTcpThread);
                }
            }
        }
Example #2
0
 public void Close()
 {
     FunctionsCommon.CloseAndNullSocket(ref tcpSocket);
     FunctionsCommon.CloseAndNullSocket(ref udpSocket);
     FunctionsCommon.CloseAndNullThread(ref listenTcpThread);
     FunctionsCommon.CloseAndNullThread(ref listenUdpThread);
 }
Example #3
0
        private bool SetupUdpAndTcpLocalIp()
        {
            udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSocket.EnableBroadcast = true;
            tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localUdpIp = new IPEndPoint(IPAddress.Any, MainServerPort);
            IPEndPoint localTcpIp = new IPEndPoint(FunctionsCommon.GetCurrrentHostIp(), MainServerPort);

            try
            {
                udpSocket.Bind(localUdpIp);
                tcpSocket.Bind(localTcpIp);
                return(true);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                return(false);
            }
        }
Example #4
0
 private ServerUdpAnswerMessages GetServerUdpAnswerMessage()
 {
     return(new ServerUdpAnswerMessages(DateTime.Now, FunctionsCommon.GetCurrrentHostIp(), MainServerPort, name));
 }
Example #5
0
        private ClientUdpRequestMessages GetClientUdpRequestMessage()
        {
            IPEndPoint localIp = (IPEndPoint)udpSocket.LocalEndPoint;

            return(new ClientUdpRequestMessages(DateTime.Now, FunctionsCommon.GetCurrrentHostIp(), localIp.Port));
        }
Example #6
0
 public void DisconnectFromServer()
 {
     FunctionsCommon.CloseAndNullSocket(ref tcpSocket);
     FunctionsCommon.CloseAndNullThread(ref listenTcpThread);
 }