Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1434de93

Přidáno uživatelem Eliška Mourycová před více než 3 roky(ů)

Re #8619. Connection handling improvements + simple parser of sent messages.

Zobrazit rozdíly:

ClientConnectionTest/ClientTest/ClientTest/Program.cs
226 226
            //---send the text---
227 227
            Console.WriteLine("Sending : " + textToSend);
228 228
            nwStream.Write(bytesToSend, 0, bytesToSend.Length);
229
			if (textToSend.Equals("bye!"))
230
			{
231
                break;
232
			}
229
			
233 230

  
234 231
            //---read back the text---
235 232
            byte[] bytesToRead = new byte[client.ReceiveBufferSize];
236 233
            int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
237 234
            Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
235

  
236
            if (textToSend.Equals("bye!"))
237
            {
238
                break;
239
            }
238 240
        }
239 241
        
240 242
        client.Close();
Server/ServerApp/Connection/SocketListener.cs
31 31
        {
32 32
        }
33 33

  
34
        public static void StartListening()
34
        public void StartListening()
35 35
        {
36 36
            // Data buffer for incoming data.
37 37
            byte[] bytes = new Byte[1024];
......
78 78

  
79 79
        }
80 80

  
81
        public static void AcceptCallback(IAsyncResult ar)
81
        public void AcceptCallback(IAsyncResult ar)
82 82
        {
83 83
            // Signal the main thread to continue.
84 84
            allDone.Set();
85
            Console.WriteLine("Accepted a connection.");
85 86

  
86 87
            // Get the socket that handles the client request.
87 88
            Socket listener = (Socket)ar.AsyncState;
......
94 95
            
95 96
        }
96 97

  
97
        public static void ReadCallback(IAsyncResult ar)
98
        public void ReadCallback(IAsyncResult ar)
98 99
        {
100

  
101
            
102

  
103

  
99 104
            String content = String.Empty;
100 105

  
101 106
            // Retrieve the state object and the handler socket
......
103 108
            StateObject state = (StateObject)ar.AsyncState;
104 109
            Socket handler = state.workSocket;
105 110

  
106
            // Read data from the client socket. 
111
            if (!SocketConnected(handler))
112
			{
113
                Console.WriteLine("Closing connection - unexpected...");
114
                CloseConnection(handler);
115
                return;
116
            }
117

  
118
                // Read data from the client socket. 
107 119
            int bytesRead = handler.EndReceive(ar);
108 120

  
109 121
            if (bytesRead > 0)
......
119 131
                    // All the data has been read from the 
120 132
                    // client. Display it on the console.
121 133
                    Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content);
134

  
135
                    content = ParseMessageAndBuildAnswer(content);
136
                    if (content.Equals("bye!"))
137
                    {
138
                        Console.WriteLine("Closing connection - expected...");
139
                        //handler.Shutdown(SocketShutdown.Both);
140
                        //handler.Close();
141
                        CloseConnection(handler);
142
                        return;
143
                    }
144

  
122 145
                    // Echo the data back to the client.
123 146
                    Send(handler, content);
147
					
124 148

  
149
                    state.sb.Clear();
125 150
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
126 151
                }
127 152
                else
......
132 157
            }
133 158
        }
134 159

  
135
        private static void Send(Socket handler, String data)
160
        private void Send(Socket handler, String data)
136 161
        {
137 162
            // Convert the string data to byte data using ASCII encoding.
138 163
            byte[] byteData = Encoding.ASCII.GetBytes(data);
......
141 166
            handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
142 167
        }
143 168

  
144
        private static void SendCallback(IAsyncResult ar)
169
        private void SendCallback(IAsyncResult ar)
145 170
        {
146 171
            try
147 172
            {
......
152 177
                int bytesSent = handler.EndSend(ar);
153 178
                Console.WriteLine("Sent {0} bytes to client.", bytesSent);
154 179

  
180

  
155 181
                //handler.Shutdown(SocketShutdown.Both);
156 182
                //handler.Close();
157 183

  
......
161 187
                Console.WriteLine(e.ToString());
162 188
            }
163 189
        }
190

  
191

  
192

  
193

  
194

  
195

  
196

  
197
        private string ParseMessageAndBuildAnswer(string message)
198
		{
199
            string ans = "";
200

  
201
			if (message.Equals("hello!"))
202
			{
203
                ans = "hello to you too.";
204
			}
205
            else if (message.Equals("weather!"))
206
			{
207
                ans = "weather will be nice";
208
            }
209
            else if (message.Equals("bye!"))
210
			{
211
                ans = "bye!";
212
			}
213
            else
214
			{
215
                // here bye too, if we want to disconnect clients who don't follow protocol
216
                ans = "bye!";//"i didnt understand that";
217

  
218
            }
219

  
220
            return ans;
221
		}
222

  
223

  
224
        private void CloseConnection(Socket s)
225
		{
226
            s.Shutdown(SocketShutdown.Both);
227
            s.Close();
228
        }
229

  
230
        private bool SocketConnected(Socket s)
231
        {
232
            bool part1 = s.Poll(1000, SelectMode.SelectRead);
233
            bool part2 = (s.Available == 0);
234
            if (part1 && part2)
235
                return false;
236
            else
237
                return true;
238
        }
164 239
    }
240
   
165 241
}
Server/ServerApp/Program.cs
36 36

  
37 37

  
38 38
            // test - connection:
39
            AsynchronousSocketListener.StartListening();
39
            AsynchronousSocketListener asl = new AsynchronousSocketListener();
40
            asl.StartListening();
40 41
           
41 42

  
42 43
            Console.ReadLine();

Také k dispozici: Unified diff