示例#1
0
            public void Connect(TcpClient socket)
            {
                this.socket = socket;
                socket.ReceiveBufferSize = dataBufferSize;
                stream = socket.GetStream();


                recievedData  = new Packet();
                recieveBuffer = new byte[dataBufferSize];
                stream.BeginRead(recieveBuffer, 0, dataBufferSize, ReceiveCallBack, null);

                ServerSend.Welcome(id, "Welcome to the server");
            }
示例#2
0
            public void Connect(TcpClient _socket)
            {
                socket = _socket;
                socket.ReceiveBufferSize = BufferSize;
                socket.SendBufferSize    = BufferSize;

                stream = socket.GetStream();

                receiveBuffer = new byte[BufferSize];

                stream.BeginRead(receiveBuffer, 0, BufferSize, ReceiveCallback, null);

                ServerSend.Welcome(id, $"Welcome {id} !");
            }
示例#3
0
        public void Connect(TcpClient _socket)
        {
            socket = _socket;
            socket.ReceiveBufferSize = dataBufferSize;
            socket.SendBufferSize    = dataBufferSize;

            stream = socket.GetStream();

            receivedData  = new Packet();
            receiveBuffer = new byte[dataBufferSize];

            stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null);

            ServerSend.Welcome(id, "Hello is this thing on?");
        }
示例#4
0
            public void Connect(TcpClient _socket)
            {
                socket = _socket;
                socket.ReceiveBufferSize = dataBufferSize;
                socket.SendBufferSize    = dataBufferSize;

                stream = socket.GetStream();

                recievedData = new Packet();
                buffer       = new byte[dataBufferSize];

                stream.BeginRead(buffer, 0, dataBufferSize, new AsyncCallback(RecieveCallback), null);

                ServerSend.Welcome(id);
            }
示例#5
0
            public void Connect(TcpClient client)
            {
                socket = client;

                socket.ReceiveBufferSize = dataBufferSize;
                socket.SendBufferSize    = dataBufferSize;

                networkStream  = socket.GetStream();
                receiveBuffer  = new byte[dataBufferSize];
                receivedPacket = new Packet();

                networkStream.BeginRead(receiveBuffer, 0, dataBufferSize, new AsyncCallback(ReceiveCallback), null);

                // TODO: send welcome message to client! (done)
                ServerSend.Welcome(id, "Welcome to server!");
            }
示例#6
0
 private void Connect()
 {
     ServerSend.Welcome(id, "Welcome to the server!");
 }
        /// <summary>Receives incoming UDP data.</summary>
        public static void UDPReceiveCallback(IAsyncResult _result)
        {
            try
            {
                IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[]     data           = udpListener.EndReceive(_result, ref clientEndPoint);
                udpListener.BeginReceive(UDPReceiveCallback, null);

                if (data.Length < 4)
                {
                    return;
                }

                //Handle received Data
                using (Packet _packet = new Packet(data))
                {
                    int clientId = _packet.ReadInt();

                    //// If this is a new connection
                    if (clientId == 0)
                    {
                        for (int i = 1; i < users.Count; i++)
                        {
                            if (users[i].udp.endPoint == null)
                            {
                                users[i].udp.Connect(clientEndPoint);
                                users[i].name = _packet.ReadString();
                                clientId      = i;
                                break;
                            }
                        }

                        //using (Packet _pack = new Packet((int)ServerPackets.welcome))
                        //{
                        //    //you have to catch these info at client on the same order
                        //    _pack.Write(clientId);
                        //    _pack.Write("Welcome");
                        //    _pack.WriteLength();


                        //    UDPServer.SendUDPData(clientEndPoint, _pack);

                        //}

                        //sent to own client a Welcome message
                        ServerSend.Welcome(clientId);

                        return;
                    }


                    if (users[clientId].udp.endPoint.ToString() == clientEndPoint.ToString())
                    {
                        users[clientId].udp.HandleData(_packet);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error receiving UDP data: {ex}");
            }
        }