DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TCPStreamDatabaseInterface.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 using System;
17 using System.Data;
18 using System.IO;
19 using System.Net.Sockets;
20 
21 namespace Deveel.Data.Client {
29  private readonly String host;
30 
34  private readonly int port;
35 
39  private Socket socket;
40 
41  internal TCPStreamDatabaseInterface(String host, int port, string initial_database)
42  : base(initial_database) {
43  this.host = host;
44  this.port = port;
45  }
46 
50  internal void ConnectToDatabase() {
51  if (socket != null) {
52  throw new DataException("Connection already established.");
53  }
54  try {
55  // Open a socket connection to the server.
56  socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
57  socket.Connect(host, port);
58  // Setup the stream with the given input and output streams.
59  Setup(new NetworkStream(socket, FileAccess.Read), new NetworkStream(socket, FileAccess.Write));
60  } catch (IOException e) {
61  throw new DataException(e.Message);
62  }
63  }
64  }
65 }
Connection to the database via the TCP protocol.
TCPStreamDatabaseInterface(String host, int port, string initial_database)
readonly String host
The name of the host we are connected to.
readonly int port
The port we are connected to.
An stream implementation of an interface to a database.