summaryrefslogtreecommitdiff
path: root/storage/ndb/src/cw/cpcc-win32/csharp/socketcomm/SocketComm.cs
blob: 34678086057e446fc2ce00d2f963cfca4c63b4c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright (c) 2004, 2005 MySQL AB
   Use is subject to license terms

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace NDB_CPC.socketcomm
{
	/// <summary>
	/// Summary description for SocketComm.
	/// </summary>
	public class SocketComm
	{
		private myTcpClient sender;
		private StreamWriter writer;
		private StreamReader reader;
		private string m_host;
		private int m_port;
		private bool m_connected;
		private bool m_connecting;
		private Thread connectThread;
		public SocketComm(string host, int port)
		{

			m_host=host;
			m_port=port;
			m_connected=false;
			m_connecting=false;
		}

		

		public bool isConnected()
		{
			return m_connected;
		}

		public void doConnect()
		{
			if(!m_connecting && !m_connected) 
			{
				connectThread= new Thread(new ThreadStart(connect));
				connectThread.Start();
			}
					
		}

		private void connect()
		{
			m_connecting=true;
			while(true) 
			{
				if(!m_connected)
				{
					try 
					{
						// Establish the remote endpoint for the socket.
						//    The name of the
						//   remote device is "host.contoso.com".
						
						// Create a TCP/IP  socket.
						sender = new myTcpClient();
						// Connect the socket to the remote endpoint. Catch any errors.
						try 
						{
							/*
							IPAddress ipAddress = Dns.Resolve(host).AddressList[0];
							IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);
*/

							
							sender.Connect(m_host,m_port);;

							writer = new StreamWriter(sender.GetStream(), Encoding.ASCII);
							reader = new StreamReader(sender.GetStream(), Encoding.ASCII);
							m_connected=true;
							m_connecting=false;
				//			break;
							Console.WriteLine("Socket connected to {0}",
								sender.ToString());
       
						} 
						catch (ArgumentNullException ane) 
						{
							Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
							m_connected=false;
						} 
						catch (SocketException se) 
						{
							Console.WriteLine("SocketException : {0}",se.ToString());
							m_connected=false;
						} 
					}
					catch (Exception e) 
					{
						Console.WriteLine("Unexpected exception : {0}", e.ToString());
						m_connected=false;
					}
				
				}
				
				Thread.Sleep(200);
			}
		}

		public bool disconnect()
		{
			try 
			{
				this.m_connected=false;
				this.m_connecting=false;
				sender.GetUnderlyingSocket().Shutdown(SocketShutdown.Both);
				sender.GetUnderlyingSocket().Close();
				writer.Close();
				reader.Close();
				sender.Close();
        
			} 
			catch (ArgumentNullException ane) 
			{
				Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
				connectThread.Abort();
				return false;
			} 
			catch (SocketException se) 
			{
				Console.WriteLine("SocketException : {0}",se.ToString());
				connectThread.Abort();
				return false;
			} 
			catch (Exception e) 
			{
				Console.WriteLine("Unexpected exception : {0}", e.ToString());
				connectThread.Abort();
				return false;
			}
			connectThread.Abort();
			return true;
		}
		
		public bool writeMessage(string message) 
		{
			int attempts=0;
			while (attempts < 10)
			{
				try 
				{
					writer.WriteLine(message);
					writer.Flush();
					message="";
					return true;
				} 
				catch(IOException e)
				{
					this.disconnect();
					this.doConnect();
					Thread.Sleep(200);
					attempts++;
				}
				catch(System.NullReferenceException)
				{
					this.disconnect();
					this.doConnect();
			
					Thread.Sleep(200);
					attempts++;
				}
			}
			return false;
		}

		public string readLine() 
		{
			int attempts=0;
			string line="";
			while (attempts < 10){
				try 
				{
					line = reader.ReadLine();
					if(line==null)
						line="";
					return line;
				}
				catch(IOException e)
				{
					this.disconnect();
					this.doConnect();
					Thread.Sleep(400);
					attempts++;
				}
				catch(System.NullReferenceException)
				{
					this.disconnect();
					this.doConnect();
					Thread.Sleep(400);
					attempts++;
				}
			}
			return "";
			
		}
		
	}
}