00001 using System; 00002 using System.Net; 00003 using System.Net.Sockets; 00004 using System.Text; 00005 using System.Windows.Forms; 00006 using System.Threading; 00007 using System.IO; 00008 00009 namespace NDB_CPC.socketcomm 00010 { 00014 public class SocketComm 00015 { 00016 private myTcpClient sender; 00017 private StreamWriter writer; 00018 private StreamReader reader; 00019 private string m_host; 00020 private int m_port; 00021 private bool m_connected; 00022 private bool m_connecting; 00023 private Thread connectThread; 00024 public SocketComm(string host, int port) 00025 { 00026 00027 m_host=host; 00028 m_port=port; 00029 m_connected=false; 00030 m_connecting=false; 00031 } 00032 00033 00034 00035 public bool isConnected() 00036 { 00037 return m_connected; 00038 } 00039 00040 public void doConnect() 00041 { 00042 if(!m_connecting && !m_connected) 00043 { 00044 connectThread= new Thread(new ThreadStart(connect)); 00045 connectThread.Start(); 00046 } 00047 00048 } 00049 00050 private void connect() 00051 { 00052 m_connecting=true; 00053 while(true) 00054 { 00055 if(!m_connected) 00056 { 00057 try 00058 { 00059 // Establish the remote endpoint for the socket. 00060 // The name of the 00061 // remote device is "host.contoso.com". 00062 00063 // Create a TCP/IP socket. 00064 sender = new myTcpClient(); 00065 // Connect the socket to the remote endpoint. Catch any errors. 00066 try 00067 { 00068 /* 00069 IPAddress ipAddress = Dns.Resolve(host).AddressList[0]; 00070 IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000); 00071 */ 00072 00073 00074 sender.Connect(m_host,m_port);; 00075 00076 writer = new StreamWriter(sender.GetStream(), Encoding.ASCII); 00077 reader = new StreamReader(sender.GetStream(), Encoding.ASCII); 00078 m_connected=true; 00079 m_connecting=false; 00080 // break; 00081 Console.WriteLine("Socket connected to {0}", 00082 sender.ToString()); 00083 00084 } 00085 catch (ArgumentNullException ane) 00086 { 00087 Console.WriteLine("ArgumentNullException : {0}",ane.ToString()); 00088 m_connected=false; 00089 } 00090 catch (SocketException se) 00091 { 00092 Console.WriteLine("SocketException : {0}",se.ToString()); 00093 m_connected=false; 00094 } 00095 } 00096 catch (Exception e) 00097 { 00098 Console.WriteLine("Unexpected exception : {0}", e.ToString()); 00099 m_connected=false; 00100 } 00101 00102 } 00103 00104 Thread.Sleep(200); 00105 } 00106 } 00107 00108 public bool disconnect() 00109 { 00110 try 00111 { 00112 this.m_connected=false; 00113 this.m_connecting=false; 00114 sender.GetUnderlyingSocket().Shutdown(SocketShutdown.Both); 00115 sender.GetUnderlyingSocket().Close(); 00116 writer.Close(); 00117 reader.Close(); 00118 sender.Close(); 00119 00120 } 00121 catch (ArgumentNullException ane) 00122 { 00123 Console.WriteLine("ArgumentNullException : {0}",ane.ToString()); 00124 connectThread.Abort(); 00125 return false; 00126 } 00127 catch (SocketException se) 00128 { 00129 Console.WriteLine("SocketException : {0}",se.ToString()); 00130 connectThread.Abort(); 00131 return false; 00132 } 00133 catch (Exception e) 00134 { 00135 Console.WriteLine("Unexpected exception : {0}", e.ToString()); 00136 connectThread.Abort(); 00137 return false; 00138 } 00139 connectThread.Abort(); 00140 return true; 00141 } 00142 00143 public bool writeMessage(string message) 00144 { 00145 int attempts=0; 00146 while (attempts < 10) 00147 { 00148 try 00149 { 00150 writer.WriteLine(message); 00151 writer.Flush(); 00152 message=""; 00153 return true; 00154 } 00155 catch(IOException e) 00156 { 00157 this.disconnect(); 00158 this.doConnect(); 00159 Thread.Sleep(200); 00160 attempts++; 00161 } 00162 catch(System.NullReferenceException) 00163 { 00164 this.disconnect(); 00165 this.doConnect(); 00166 00167 Thread.Sleep(200); 00168 attempts++; 00169 } 00170 } 00171 return false; 00172 } 00173 00174 public string readLine() 00175 { 00176 int attempts=0; 00177 string line=""; 00178 while (attempts < 10){ 00179 try 00180 { 00181 line = reader.ReadLine(); 00182 if(line==null) 00183 line=""; 00184 return line; 00185 } 00186 catch(IOException e) 00187 { 00188 this.disconnect(); 00189 this.doConnect(); 00190 Thread.Sleep(400); 00191 attempts++; 00192 } 00193 catch(System.NullReferenceException) 00194 { 00195 this.disconnect(); 00196 this.doConnect(); 00197 Thread.Sleep(400); 00198 attempts++; 00199 } 00200 } 00201 return ""; 00202 00203 } 00204 00205 } 00206 } 00207
1.4.7

