コード例 #1
0
        public void Connect(string host, int port)
        {
            serverState = new TcpState();
            serverState.ByteData = new List<byte>();
            serverState.Buffer = new byte[bufferSize];
            serverState.Connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverState.Data = "";
            serverState.Name = Name;

            serverState.Connection.BeginConnect(IPAddress.Parse(host), port, new AsyncCallback(RecvConnection), serverState);
        }
コード例 #2
0
 private void RemoveClient(TcpState state)
 {
     //console.Print("Server", "Info", state.id + " Disconnected(Manual)");
     if (state.Initialized)
         RaiseOnDisconnect(new ConnectionArgs(state.ID, state.Name));
     RemoveClient(state.ID);
 }
コード例 #3
0
 private void RecvConnection(IAsyncResult args)
 {
     Socket client = listener.EndAccept(args);
     if (idling)
     {
         //console.Print("Server", "Info", "Server hasn't started");
         //client.BeginSend()
         client.Disconnect(false);
         listener.BeginAccept(new AsyncCallback(RecvConnection), listener);
         return;
     }
     byte id = NextID();
     if (id == 255)//if no more room, send 0
     {
         try
         {
             //console.Print("Server", "Info", "No room for client");
             byte[] sendingData = msgTypes[0].ServerFormatData((byte)0);
             client.BeginSend(sendingData, 0, sendingData.Length, 0, new AsyncCallback(SendAck), null);
             client.Disconnect(false);
         }
         catch (SocketException ex)
         {
             //console.Print("Server", "Error", ex.Message);
         }
     }
     else
     {
         TcpState state = new TcpState();
         state.Connection = client;
         state.Buffer = new byte[bufferSize];
         state.ByteData = new List<byte>();
         state.ID = id;
         state.Data = "";
         //admin
         connections.Add(client, id);
         states[id] = state;
         connecting.Add(client);
         //console.Print("Server", "Info", "[Server]Connecting: (" + id + ") " + client.LocalEndPoint);
         try
         {
             //sends 3, id to client
             byte[] sentData = msgTypes[3].ServerFormatData((byte)id);
             client.BeginSend(sentData, 0, sentData.Length, 0, new AsyncCallback(SendAck), state);
             //client.BeginSend(new byte[]{ 3, 0, 1, (byte)id, 60, 69, 79, 70, 62 }, 0, 9, 0, new AsyncCallback(SendAck), state);
             client.BeginReceive(state.Buffer, 0, bufferSize, 0, new AsyncCallback(RecvMsg), state);
         }
         catch (SocketException ex)
         {
             //console.Print("Server", "Error", "[Server]Connection " + id + " Disconnected: " + ex.Message);
             RemoveClient(id);
         }
     }
     //start listening
     listener.BeginAccept(new AsyncCallback(RecvConnection), null);
 }