using System.IO;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System;
namespace AsterNET.IO
{
public class SocketConnection
{
private TcpClient tcpClient;
private NetworkStream networkStream;
private StreamReader reader;
private StreamWriter writer;
private Encoding encoding;
private bool initial;
#region Constructor - SocketConnection(string host, int port, int receiveTimeout)
///
/// Consructor
///
/// client host
/// client port
/// encoding
public SocketConnection(string host, int port, Encoding encoding)
{
initial = true;
this.encoding = encoding;
this.tcpClient = new TcpClient(host, port);
this.networkStream = this.tcpClient.GetStream();
this.reader = new StreamReader(this.networkStream, encoding);
this.writer = new StreamWriter(this.networkStream, encoding);
this.writer.AutoFlush = true;
}
#endregion
#region Constructor - SocketConnection(socket)
///
/// Constructor
///
/// TCP client from Listener
/// encoding
internal SocketConnection(TcpClient tcpClient, Encoding encoding)
{
initial = true;
this.encoding = encoding;
this.tcpClient = tcpClient;
this.networkStream = this.tcpClient.GetStream();
this.reader = new StreamReader(this.networkStream, encoding);
this.writer = new StreamWriter(this.networkStream, encoding);
this.writer.AutoFlush = true;
}
#endregion
public TcpClient TcpClient
{
get { return tcpClient; }
}
public NetworkStream NetworkStream
{
get { return networkStream; }
}
public Encoding Encoding
{
get { return encoding; }
}
public bool Initial
{
get { return initial; }
set { initial = value; }
}
#region IsConnected
///
/// Returns the connection state of the socket.
///
public bool IsConnected
{
get { return tcpClient.Connected; }
}
#endregion
#region LocalAddress
public IPAddress LocalAddress
{
get
{
return ((IPEndPoint)(tcpClient.Client.LocalEndPoint)).Address;
}
}
#endregion
#region LocalPort
public int LocalPort
{
get
{
return ((IPEndPoint)(tcpClient.Client.LocalEndPoint)).Port;
}
}
#endregion
#region RemoteAddress
public IPAddress RemoteAddress
{
get
{
return ((IPEndPoint)(tcpClient.Client.RemoteEndPoint)).Address;
}
}
#endregion
#region RemotePort
public int RemotePort
{
get
{
return ((IPEndPoint)(tcpClient.Client.LocalEndPoint)).Port;
}
}
#endregion
#region ReadLine()
///
/// Reads a line of text from the socket connection. The current thread is
/// blocked until either the next line is received or an IOException
/// encounters.
///
/// the line of text received excluding any newline character
/// IOException if the connection has been closed.
public string ReadLine()
{
string line = null;
try
{
line = reader.ReadLine();
}
catch
{
line = null;
}
return line;
}
#endregion
#region Write(string s)
///
/// Sends a given String to the socket connection.
///
/// the String to send.
/// IOException if the String cannot be sent, maybe because the
/// connection has already been closed.
public void Write(string s)
{
writer.Write(s);
}
#endregion
#region Write(string msg)
///
/// Sends a given String to the socket connection.
///
/// the String to send.
/// IOException if the String cannot be sent, maybe because the
/// connection has already been closed.
public void WriteEx(string msg)
{
byte[] data = encoding.GetBytes(msg);
networkStream.BeginWrite(data, 0, data.Length, onWriteFinished, networkStream);
networkStream.Flush();
}
private void onWriteFinished(IAsyncResult ar)
{
NetworkStream stream = (NetworkStream)ar.AsyncState;
stream.EndWrite(ar);
}
#endregion
#region Close
///
/// Closes the socket connection including its input and output stream and
/// frees all associated ressources.
/// When calling close() any Thread currently blocked by a call to readLine()
/// will be unblocked and receive an IOException.
///
/// IOException if the socket connection cannot be closed.
public void Close()
{
try
{
tcpClient.Client.Shutdown(SocketShutdown.Both);
tcpClient.Client.Close();
tcpClient.Close();
}
catch { }
}
#endregion
}
}