DELL
2025-06-15 17023827ffbbdc5df5a639779ced776178a12cc2
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
 * Copyright 2011-17 Fraunhofer ISE, energy & meteo Systems GmbH and other contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.openmuc.jositransport;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
 
public final class TConnection {
 
    // private static final Logger logger = LoggerFactory.getLogger(TConnection.class);
 
    private final Socket socket;
    private final DataOutputStream os;
    private final DataInputStream is;
    private static Integer connectionCounter = 0;
 
    public byte[] tSelRemote = null;
    public byte[] tSelLocal = null;
 
    private int srcRef;
    private int dstRef;
    private int maxTPduSizeParam;
    private int maxTPduSize;
    private int messageTimeout;
    private int messageFragmentTimeout;
 
    private boolean closed = false;
 
    private final ServerThread serverThread;
 
    TConnection(Socket socket, int maxTPduSizeParam, int messageTimeout, int messageFragmentTimeout,
            ServerThread serverThread) throws IOException {
        if (maxTPduSizeParam < 7 || maxTPduSizeParam > 16) {
            throw new RuntimeException("maxTPduSizeParam is incorrect");
        }
        this.socket = socket;
        os = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        is = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
 
        synchronized (socket) {
            connectionCounter++;
            connectionCounter %= 65520;
            if (connectionCounter == 0) {
                connectionCounter = 1; // some servers do not like srcRef 0
            }
            srcRef = connectionCounter;
        }
 
        this.messageTimeout = messageTimeout;
        this.messageFragmentTimeout = messageFragmentTimeout;
        this.maxTPduSizeParam = maxTPduSizeParam;
        maxTPduSize = ClientTSap.getMaxTPDUSize(maxTPduSizeParam);
 
        this.serverThread = serverThread;
    }
 
    /**
     * This function is called once a client has connected to the server. It listens for a Connection Request (CR). If
     * this is successful it replies afterwards with a Connection Confirm (CC). According to the norm a syntax error in
     * the CR should be followed by an ER. This implementation does not send an ER because it seems unnecessary.
     *
     * @throws IOException
     */
    void listenForCR() throws IOException {
 
        socket.setSoTimeout(messageFragmentTimeout);
 
        byte myByte;
        int lengthIndicator;
        int parameterLength;
        
        //is.available();
 
        // start reading rfc 1006 header
        if (is.read() != 0x03) {
            throw new IOException();
        }
        if (is.read() != 0) {
            throw new IOException();
        }
        // read Packet length, but is not needed
        is.readShort();
        // reading rfc 1006 header finished
 
        lengthIndicator = is.read() & 0xff;
        // 0xe0 is the CR-Code
        if ((is.read() & 0xff) != 0xe0) {
            throw new IOException();
        }
        // DST-REF needs to be 0 in a CR packet
        if (is.readShort() != 0) {
            throw new IOException();
        }
        // read the srcRef which is the dstRef for this end-point
        dstRef = is.readShort() & 0xffff;
        // read class
        if ((is.read() & 0xff) != 0) {
            throw new IOException();
        }
        int variableBytesRead = 0;
        while (lengthIndicator > (6 + variableBytesRead)) {
            // read parameter code
            myByte = is.readByte();
            switch (myByte & 0xff) {
            case 0xc2:
                parameterLength = is.readByte() & 0xff;
 
                if (tSelLocal == null) {
                    tSelLocal = new byte[parameterLength];
                    is.readFully(tSelLocal);
                }
                else {
                    if (parameterLength != tSelLocal.length) {
                        throw new IOException("local T-SElECTOR is wrong.");
                    }
                    for (int i = 0; i < parameterLength; i++) {
                        if ((tSelLocal[i] & 0xff) != is.read()) {
                            throw new IOException("local T-SElECTOR is wrong.");
                        }
                    }
                }
                variableBytesRead += 2 + parameterLength;
                break;
            case 0xc1:
                parameterLength = is.readByte() & 0xff;
 
                if (tSelRemote == null) {
                    tSelRemote = new byte[parameterLength];
                    is.readFully(tSelRemote);
                }
                else {
                    if (parameterLength != tSelRemote.length) {
                        throw new IOException("remote T-SElECTOR is wrong.");
                    }
                    for (int i = 0; i < parameterLength; i++) {
                        if ((tSelRemote[i] & 0xff) != is.read()) {
                            throw new IOException("remote T-SElECTOR is wrong.");
                        }
                    }
                }
                variableBytesRead += 2 + parameterLength;
                break;
 
            case 0xc0:
                if ((is.readByte() & 0xff) != 1) {
                    throw new IOException();
                }
                myByte = is.readByte();
                int newMaxTPDUSizeParam = (myByte & 0xff);
                if (newMaxTPDUSizeParam < 7 || newMaxTPDUSizeParam > 16) {
                    throw new IOException("maxTPDUSizeParam is out of bound");
                }
                else {
                    if (newMaxTPDUSizeParam < maxTPduSizeParam) {
                        maxTPduSizeParam = newMaxTPDUSizeParam;
                        maxTPduSize = ClientTSap.getMaxTPDUSize(maxTPduSizeParam);
                    }
                }
                variableBytesRead += 3;
                break;
            default:
                throw new IOException();
            }
 
        }
 
        // write RFC 1006 Header
        os.write(0x03);
        os.write(0x00);
        // write complete packet length
 
        int variableLength = 3;
 
        if (tSelLocal != null) {
            variableLength += 2 + tSelLocal.length;
        }
        if (tSelRemote != null) {
            variableLength += 2 + tSelRemote.length;
        }
        os.writeShort(4 + 7 + variableLength);
 
        // write connection request (CR) TPDU (§13.3)
 
        // write length indicator
        os.write(6 + variableLength);
 
        // write fixed part
        // write CC CDT
        os.write(0xd0);
        // write DST-REF
        os.writeShort(dstRef);
        // write SRC-REF
        os.writeShort(srcRef);
        // write class
        os.write(0);
 
        // write variable part
        if (tSelLocal != null) {
            os.write(0xc2);
            os.write(tSelLocal.length);
            os.write(tSelLocal);
        }
 
        if (tSelRemote != null) {
            os.write(0xc1);
            os.write(tSelRemote.length);
            os.write(tSelRemote);
        }
        // write proposed maximum TPDU Size
        os.write(0xc0);
        os.write(1);
        os.write(maxTPduSizeParam);
 
        os.flush();
 
    }
 
    /**
     * Starts a connection, sends a CR, waits for a CC and throws an IOException if not successful
     *
     * @throws IOException
     */
    void startConnection() throws IOException {
        
        ByteBuffer bf = ByteBuffer.allocate(1024);
        
        // write RFC 1006 Header
        bf.put((byte) 0x03);
        bf.put((byte) 0x00);
 
        // write complete packet length
        int variableLength = 3;
 
        if (tSelLocal != null) {
            variableLength += 2 + tSelLocal.length;
        }
        if (tSelRemote != null) {
            variableLength += 2 + tSelRemote.length;
        }
        bf.putShort((short) (4 + 7 + variableLength));
        // writing RFC 1006 Header finished
 
        // write connection request (CR) TPDU (§13.3)
 
        // write length indicator
        bf.put((byte) (6 + variableLength));
 
        // write fixed part
        // write CR CDT
        bf.put((byte) 0xe0);
        // write DST-REF
        bf.put((byte) 0);
        bf.put((byte) 0);
        // write SRC-REF
        bf.putShort((short) srcRef);
        // write class
        bf.put((byte) 0);
 
        // write variable part
        // write proposed maximum TPDU Size
        bf.put((byte) 0xc0);
        bf.put((byte) 1);
        bf.put((byte) maxTPduSizeParam);
 
        if (tSelRemote != null) {
            bf.put((byte) 0xc2);
            bf.put((byte) tSelRemote.length);
            bf.put(tSelRemote);
        }
        if (tSelLocal != null) {
            bf.put((byte) 0xc1);
            bf.put((byte) tSelLocal.length);
            bf.put(tSelLocal);
        }
        
        bf.flip();
        byte[] outbyte = new byte[bf.limit()];
        bf.get(outbyte);
        os.write(outbyte);
        
        /*
        // write RFC 1006 Header
        os.write(0x03);
        os.write(0x00);
 
        // write complete packet length
        int variableLength = 3;
 
        if (tSelLocal != null) {
            variableLength += 2 + tSelLocal.length;
        }
        if (tSelRemote != null) {
            variableLength += 2 + tSelRemote.length;
        }
        os.writeShort(4 + 7 + variableLength);
        // writing RFC 1006 Header finished
 
        // write connection request (CR) TPDU (§13.3)
 
        // write length indicator
        os.write(6 + variableLength);
 
        // write fixed part
        // write CR CDT
        os.write(0xe0);
        // write DST-REF
        os.write(0);
        os.write(0);
        // write SRC-REF
        os.writeShort(srcRef);
        // write class
        os.write(0);
 
        // write variable part
        // write proposed maximum TPDU Size
        os.write(0xc0);
        os.write(1);
        os.write(maxTPduSizeParam);
 
        if (tSelRemote != null) {
            os.write(0xc2);
            os.write(tSelRemote.length);
            os.write(tSelRemote);
        }
        if (tSelLocal != null) {
            os.write(0xc1);
            os.write(tSelLocal.length);
            os.write(tSelLocal);
        }
        */
        os.flush();
 
        socket.setSoTimeout(messageTimeout);
 
        byte myByte;
        int lengthIndicator;
        int parameterLength;
        //is.readFully(outbyte);
        //System.out.println("is.availabless() is : " + is.available());
        /*
        byte res_b = is.readByte();
        ByteBuffer is_bf = ByteBuffer.allocate(is.available()+1);
        is_bf.put(res_b);
        is.readFully(is_bf.array(), 1, is.available());
        */
        if (is.readByte() != 0x03) {
            throw new IOException();
        }
        if (is.readByte() != 0) {
            throw new IOException();
        }
        //System.out.println("line 376 is.availabless() is : " + is.available());
        // read packet length, but is not needed
        is.readShort();
        lengthIndicator = is.readByte() & 0xff;
        if ((is.readByte() & 0xff) != 0xd0) {
            throw new IOException();
        }
        // read the dstRef which is the srcRef for this end-point
        is.readShort();
        // read the srcRef which is the dstRef for this end-point
        dstRef = is.readShort() & 0xffff;
        // read class
        if (is.readByte() != 0) {
            throw new IOException();
        }
 
        int variableBytesRead = 0;
        while (lengthIndicator > (6 + variableBytesRead)) {
            // read parameter code
            myByte = is.readByte();
            switch (myByte & 0xff) {
 
            case 0xc1:
                parameterLength = is.readByte() & 0xff;
 
                if (tSelLocal == null) {
                    tSelLocal = new byte[parameterLength];
                    is.readFully(tSelLocal);
                }
                else {
                    for (int i = 0; i < parameterLength; i++) {
                        //System.out.println("line 407 is.availabless() is : " + is.available());
                        is.read();
                    }
                }
                variableBytesRead += 2 + parameterLength;
                break;
            case 0xc2:
                parameterLength = is.readByte() & 0xff;
 
                if (tSelRemote == null) {
                    tSelRemote = new byte[parameterLength];
                    is.readFully(tSelRemote);
                }
                else {
                    for (int i = 0; i < parameterLength; i++) {
                        //System.out.println("line 422 is.availabless() is : " + is.available());
                        is.read();
                    }
                }
                variableBytesRead += 2 + parameterLength;
                break;
 
            case 0xc0:
                if (is.readByte() != 1) {
                    throw new IOException("maxTPduSizeParam size is not equal to 1");
                }
                myByte = is.readByte();
                if ((myByte & 0xff) < 7 || (myByte & 0xff) > maxTPduSizeParam) {
                    throw new IOException("maxTPduSizeParam out of bound");
                }
                else {
                    if ((myByte & 0xff) < maxTPduSizeParam) {
                        maxTPduSizeParam = (myByte & 0xff);
                    }
                }
                variableBytesRead += 4;
                break;
            default:
                throw new IOException();
            }
 
        }
 
    }
 
    public void send(List<byte[]> tsdus, List<Integer> offsets, List<Integer> lengths) throws IOException {
 
        int bytesLeft = 0;
        // for (byte[] tsdu : tsdus) {
        // bytesLeft += tsdu.length;
        // }
        for (int length : lengths) {
            bytesLeft += length;
        }
        int tsduOffset = 0;
        int byteArrayListIndex = 0;
        int numBytesToWrite;
        boolean lastPacket = false;
        int maxTSDUSize = maxTPduSize - 3;
        while (bytesLeft > 0) {
 
            if (bytesLeft > maxTSDUSize) {
                numBytesToWrite = maxTSDUSize;
            }
            else {
                numBytesToWrite = bytesLeft;
                lastPacket = true;
            }
 
            // --write RFC 1006 Header--
            // write Version
            os.write(0x03);
            // write reserved bits
            os.write(0);
            // write packet Length
            os.writeShort(numBytesToWrite + 7);
 
            // --write 8073 Header--
            // write Length Indicator of header
            os.write(0x02);
            // write TPDU Code for DT Data
            os.write(0xf0);
            // write TPDU-NR and EOT, TPDU-NR is always 0 for class 0
            if (lastPacket) {
                os.write(0x80);
            }
            else {
                os.write(0x00);
            }
 
            bytesLeft -= numBytesToWrite;
            while (numBytesToWrite > 0) {
                byte[] tsdu = tsdus.get(byteArrayListIndex);
                int length = lengths.get(byteArrayListIndex);
                int offset = offsets.get(byteArrayListIndex);
 
                int tsduWriteLength = length - tsduOffset;
                
                //System.out.println("send tsduWriteLength " + tsduWriteLength + " numBytesToWrite " + numBytesToWrite);
 
                if (numBytesToWrite > tsduWriteLength) {
                    os.write(tsdu, offset + tsduOffset, tsduWriteLength);
                    numBytesToWrite -= tsduWriteLength;
                    tsduOffset = 0;
                    byteArrayListIndex++;
                }
                else {
                    os.write(tsdu, offset + tsduOffset, numBytesToWrite);
                    if (numBytesToWrite == tsduWriteLength) {
                        tsduOffset = 0;
                        byteArrayListIndex++;
                    }
                    else {
                        tsduOffset += numBytesToWrite;
                    }
                    numBytesToWrite = 0;
                }
            }
 
            os.flush();
        }
    }
 
    public void send_ex(byte[] tsdu, int offset, int length) throws IOException {
        List<byte[]> tsdus = new ArrayList<>();
        tsdus.add(tsdu);
        List<Integer> offsets = new ArrayList<>();
        offsets.add(offset);
        List<Integer> lengths = new ArrayList<>();
        lengths.add(length);
        send(tsdus, offsets, lengths);
    }
 
    public int getMessageTimeout() {
        return messageTimeout;
    }
 
    /**
     * Set the TConnection timeout for waiting for the first byte of a new message. Default is 0 (unlimited)
     *
     * @param messageTimeout
     *            in milliseconds
     */
    public void setMessageTimeout(int messageTimeout) {
        this.messageTimeout = messageTimeout;
    }
 
    public int getMessageFragmentTimeout() {
        return messageFragmentTimeout;
    }
 
    /**
     * Set the TConnection timeout for receiving data once the beginning of a message has been received. Default is
     * 60000 (60 seconds)
     *
     * @param messageFragmentTimeout
     *            in milliseconds
     */
    public void setMessageFragmentTimeout(int messageFragmentTimeout) {
        this.messageFragmentTimeout = messageFragmentTimeout;
    }
 
    /**
     * Listens for a new TPDU and writes the extracted TSDU into the passed buffer.
     *
     * @param tSduBuffer
     *            the buffer that is filled with the received TSDU data.
     * @throws EOFException
     *             if a Disconnect Request (DR) was received or the socket was simply closed
     * @throws SocketTimeoutException
     *             if a messageFragmentTimeout is thrown by the socket while receiving the remainder of a message
     * @throws IOException
     *             if an ErrorPDU (ER) was received, any syntax error in the received message header was detected or the
     *             tSduBuffer is too small to hold the complete PDU.
     * @throws TimeoutException
     *             this exception is thrown if the first byte of new message is not received within the message timeout.
     */
    public void receive(ByteBuffer tSduBuffer)
            throws EOFException, SocketTimeoutException, IOException, TimeoutException {
 
        tSduBuffer.mark();
 
        int packetLength;
        int eot = 0;
        int li = 0;
        int tPduCode;
 
//        socket.setSoTimeout(messageTimeout);
        
        //System.err.println("³¬Ê±Ê±¼ä£º"+messageTimeout);
        //ÐÞ¸´Linux»·¾³Ï²»³¬Ê±£¬Í¨ÐŹÊÕÏÑÓ³ÙÑÏÖØÎÊÌâ
        //socket.setSoTimeout(messageTimeout);
        socket.setSoTimeout(30000);
        byte version;
        try {
            version = is.readByte();
        } catch (SocketTimeoutException e) {
            throw (new TimeoutException());
        }
        socket.setSoTimeout(messageFragmentTimeout);
 
        do {
            // read version
            if (version != 3) {
                throw new IOException("Syntax error at beginning of RFC1006 header: version not equal to 3");
            }
 
            // read reserved
            if (is.readByte() != 0) {
                throw new IOException("Syntax errorat beginning of RFC1006 header: reserved not equal to 0");
            }
 
            // read packet length
            packetLength = is.readShort() & 0xffff;
            if (packetLength <= 7) {
                throw new IOException("Syntax error: packet length parameter < 7");
            }
 
            // read length indicator
            li = is.readByte() & 0xff;
 
            // read TPDU code
            tPduCode = is.readByte() & 0xff;
 
            if (tPduCode == 0xf0) {
                // Data Transfer (DT) Code
 
                if (li != 2) {
                    throw new IOException("Syntax error: LI field does not equal 2");
                }
 
                // read EOT
                eot = is.readByte() & 0xff;
                if (eot != 0 && eot != 0x80) {
                    throw new IOException("Syntax error: eot wrong");
                }
 
                if (packetLength - 7 > tSduBuffer.limit() - tSduBuffer.position()) {
                    throw new IOException("tSduBuffer size is too small to hold the complete TSDU");
                }
                is.readFully(tSduBuffer.array(), tSduBuffer.arrayOffset() + tSduBuffer.position(), packetLength - 7);
                tSduBuffer.position(tSduBuffer.position() + packetLength - 7);
            }
            else if (tPduCode == 0x80) {
                // Disconnect Request (DR)
 
                if (li != 6) {
                    throw new IOException("Syntax error: LI field does not equal 6");
                }
 
                // check if the DST-REF field is set to the reference of the
                // receiving entity -> srcRef
                if (is.readShort() != srcRef) {
                    throw new IOException("Syntax error: srcRef wrong");
                }
 
                // check if the SRC-REF field is that of the entity sending
                // the DR
                if (is.readShort() != dstRef) {
                    throw new IOException("Syntax error: dstRef wrong");
                }
 
                // check the reason field, for class 0 only between 1 and 4
                int reason = is.readByte() & 0xff;
                if (reason > 4) {
                    throw new IOException("Syntax error: reason out of bound");
                }
 
                // Disconnect is valid, throw exception
                throw new EOFException("Disconnect request. Reason:" + reason);
 
            }
            else if (tPduCode == 0x70) {
                throw new IOException("Got TPDU error (ER) message");
            }
            else {
                throw new IOException("Syntax error: unknown TPDU code");
            }
 
            if (eot != 0x80) {
                version = is.readByte();
            }
 
        } while (eot != 0x80);
 
        tSduBuffer.limit(tSduBuffer.position());
        tSduBuffer.reset();
    }
 
    /**
     * This function sends a Disconnect Request but does not wait for a Disconnect Confirm.
     */
    public void disconnect() {
 
        try {
            // write header for rfc
            // write version
            os.write(0x03);
            // write reserved
            os.write(0x00);
 
            // write packet length
            os.writeShort(4 + 7); // this does not include the variable part
            // which
            // contains additional user information for
            // disconnect
 
            // beginning of ISO 8073 header
            // write LI
            os.write(0x06);
 
            // write DR
            os.write(0x80);
 
            // write DST-REF
            os.writeShort(dstRef);
 
            // write SRC-REF
            os.writeShort(srcRef);
 
            // write reason - 0x00 corresponds to reason not specified. Can
            // write
            // the reasons as case structure, but need input from client
            os.write(0x00);
 
            os.flush();
        } catch (IOException e) {
        } finally {
            close();
        }
    }
 
    /**
     * Will close the TCP connection if its still open and free any resources of this connection.
     */
    public void close() {
        if (!closed) {
            closed = true;
            try {
                // will also close socket
                os.close();
            } catch (Exception e) {
            }
            try {
                is.close();
            } catch (Exception e) {
            }
            if (serverThread != null) {
                serverThread.connectionClosedSignal();
            }
        }
    }
}