├── .gitattributes
├── .gitignore
├── README.md
├── build.xml
├── dist
├── SocketTest.jar
└── metouia.jar
├── lib
├── metouia.jar
├── swt-awt-win32-3111.dll
├── swt-win32-3111.dll
└── swt.jar
├── manifest.mf
├── nbproject
├── build-impl.xml
├── genfiles.properties
├── private
│ ├── cache
│ │ └── .refactorit
│ │ │ └── cache.bin
│ ├── config.properties
│ ├── private.properties
│ └── private.xml
├── project.properties
└── project.xml
├── src
├── icons
│ ├── ball.gif
│ └── logo.gif
├── net
│ └── sf
│ │ └── sockettest
│ │ ├── MyTrustManager.java
│ │ ├── NetService.java
│ │ ├── PortModel.java
│ │ ├── SocketClient.java
│ │ ├── SocketServer.java
│ │ ├── SocketTest.java
│ │ ├── UdpServer.java
│ │ ├── Util.java
│ │ └── swing
│ │ ├── About.java
│ │ ├── PortDialog.java
│ │ ├── SocketTestClient.java
│ │ ├── SocketTestServer.java
│ │ ├── SocketTestUdp.java
│ │ └── SplashScreen.java
├── readme.txt
├── run.sh
├── tcpports.txt
└── udpports.txt
└── webstart
├── SocketTest.jnlp
└── logo.gif
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Set default behaviour, in case users don't have core.autocrlf set.
2 | text eol=lf
3 |
4 | *.bat text eol=crlf
5 |
6 | # Explicitly declare text files we want to always be normalized and converted to native line endings on checkout.
7 | *.txt text
8 | *.java text
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | build
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | SocketTest v3.0.1
2 | ------------------
3 |
4 | A java tool for socket testing. It can create both TCP and UDP client or server. It can be used to test any server or client that uses TCP or UDP protocol to communicate.
5 |
6 | Licence: GNU Lesser General Public License
7 |
8 | Features:
9 | * Create a TCP client socket and send commands.
10 | * Create a UDP client socket and send commands.
11 | * Create a TCP server socket and send responses to connected clients.
12 | * Create a UDP server socket that listens on a particular port.
13 | * Save the conversation with the client or host to a txt file.
14 | * TrustManager to prompt if certificate does not get validated.
15 |
16 | Copyright © 2003-2008 Akshathkumar Shetty
17 |
18 | @Update
19 | * added to program read from command line values
20 |
--------------------------------------------------------------------------------
/build.xml:
--------------------------------------------------------------------------------
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 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/dist/SocketTest.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/dist/SocketTest.jar
--------------------------------------------------------------------------------
/dist/metouia.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/dist/metouia.jar
--------------------------------------------------------------------------------
/lib/metouia.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/lib/metouia.jar
--------------------------------------------------------------------------------
/lib/swt-awt-win32-3111.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/lib/swt-awt-win32-3111.dll
--------------------------------------------------------------------------------
/lib/swt-win32-3111.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/lib/swt-win32-3111.dll
--------------------------------------------------------------------------------
/lib/swt.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/lib/swt.jar
--------------------------------------------------------------------------------
/manifest.mf:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Main-Class: net.sf.sockettest.SocketTest
3 | Class-Path: metouia.jar
4 | X-Author: Akshathkumar Shetty
5 | X-URL: http://sockettest.sourceforge.net/
6 |
--------------------------------------------------------------------------------
/nbproject/build-impl.xml:
--------------------------------------------------------------------------------
1 |
2 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
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 | Must set src.dir
209 | Must set test.src.dir
210 | Must set build.dir
211 | Must set dist.dir
212 | Must set build.classes.dir
213 | Must set dist.javadoc.dir
214 | Must set build.test.classes.dir
215 | Must set build.test.results.dir
216 | Must set build.classes.excludes
217 | Must set dist.jar
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 | Must set javac.includes
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 | Must set JVM to use for profiling in profiler.info.jvm
405 | Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
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 |
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 | Must select some files in the IDE or set javac.includes
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 | To run this application from the command line without Ant, try:
659 |
660 |
661 |
662 |
663 |
664 |
665 | java -cp "${run.classpath.with.dist.jar}" ${main.class}
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 | To run this application from the command line without Ant, try:
691 |
692 | java -jar "${dist.jar.resolved}"
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 | Must select one file in the IDE or set run.class
722 |
723 |
724 |
725 | Must select one file in the IDE or set run.class
726 |
727 |
728 |
733 |
734 |
735 |
736 |
737 |
738 |
739 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 |
750 |
751 |
752 | Must select one file in the IDE or set debug.class
753 |
754 |
755 |
756 |
757 | Must select one file in the IDE or set debug.class
758 |
759 |
760 |
761 |
762 | Must set fix.includes
763 |
764 |
765 |
766 |
767 |
768 |
769 |
774 |
775 |
776 |
777 |
778 |
779 |
780 |
781 |
782 |
783 | Must select one file in the IDE or set profile.class
784 |
785 |
786 |
787 |
788 |
789 |
790 |
791 |
796 |
797 |
798 |
799 |
800 |
801 |
802 |
803 |
804 |
805 |
806 |
807 |
808 |
813 |
814 |
815 |
816 |
817 |
818 |
819 |
820 |
821 |
822 |
823 |
824 |
825 |
826 |
827 |
828 |
829 |
830 |
831 |
832 |
833 |
834 |
835 |
840 |
841 |
842 |
843 |
844 |
845 |
846 |
847 |
848 |
849 |
850 |
851 |
852 |
853 |
854 |
855 |
856 |
857 |
858 |
859 |
860 |
861 |
862 |
863 |
864 |
865 |
866 |
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
879 |
880 |
881 |
882 |
883 |
884 |
885 |
886 |
887 |
888 |
889 |
890 |
891 |
892 |
893 |
894 |
895 |
896 |
897 | Must select some files in the IDE or set javac.includes
898 |
899 |
900 |
901 |
902 |
903 |
904 |
905 |
906 |
907 |
908 |
909 |
914 |
915 |
916 |
917 |
918 |
919 |
920 |
921 | Some tests failed; see details above.
922 |
923 |
924 |
925 |
926 |
927 |
928 |
929 |
930 | Must select some files in the IDE or set test.includes
931 |
932 |
933 |
934 | Some tests failed; see details above.
935 |
936 |
937 |
942 |
943 | Must select one file in the IDE or set test.class
944 |
945 |
946 |
947 |
948 |
949 |
950 |
951 |
952 |
953 |
954 |
955 |
956 |
957 |
958 |
959 |
960 |
961 |
962 |
963 |
964 |
965 |
966 |
967 |
968 |
973 |
974 | Must select one file in the IDE or set applet.url
975 |
976 |
977 |
978 |
979 |
980 |
981 |
986 |
987 | Must select one file in the IDE or set applet.url
988 |
989 |
990 |
991 |
992 |
993 |
994 |
995 |
1000 |
1001 |
1002 |
1003 |
1004 |
1005 |
1006 |
1007 |
1008 |
1009 |
1010 |
1011 |
1012 |
1013 |
1014 |
1015 |
1016 |
1017 |
1018 |
1019 |
1020 |
1021 |
1022 |
1023 |
1024 |
1025 |
1026 |
1027 |
1028 |
1029 |
1030 |
1031 |
1032 |
1033 |
1034 |
1035 |
1036 |
1037 |
1038 |
1039 |
1040 |
1041 |
1042 |
--------------------------------------------------------------------------------
/nbproject/genfiles.properties:
--------------------------------------------------------------------------------
1 | build.xml.data.CRC32=cba644ff
2 | build.xml.script.CRC32=74386eec
3 | build.xml.stylesheet.CRC32=ba5d3624
4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6 | nbproject/build-impl.xml.data.CRC32=94bef83f
7 | nbproject/build-impl.xml.script.CRC32=c8383d25
8 | nbproject/build-impl.xml.stylesheet.CRC32=0c01fd8e@1.43.1.45
9 |
--------------------------------------------------------------------------------
/nbproject/private/cache/.refactorit/cache.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/nbproject/private/cache/.refactorit/cache.bin
--------------------------------------------------------------------------------
/nbproject/private/config.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/nbproject/private/config.properties
--------------------------------------------------------------------------------
/nbproject/private/private.properties:
--------------------------------------------------------------------------------
1 | application.args=
2 | compile.on.save=false
3 | do.depend=false
4 | do.jar=true
5 | javac.debug=true
6 | javadoc.preview=true
7 | user.properties.file=C:\\Users\\akshath\\.netbeans\\7.0\\build.properties
8 | default.javac.source=1.3
9 | default.javac.target=1.3
10 |
--------------------------------------------------------------------------------
/nbproject/private/private.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/nbproject/project.properties:
--------------------------------------------------------------------------------
1 | annotation.processing.enabled=true
2 | annotation.processing.enabled.in.editor=false
3 | annotation.processing.run.all.processors=true
4 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
5 | application.args=
6 | application.title=SocketTest
7 | application.vendor=Akshathkumar Shetty
8 | build.classes.dir=${build.dir}/classes
9 | build.classes.excludes=**/*.java,**/*.form
10 | # This directory is removed when the project is cleaned:
11 | build.dir=build
12 | build.generated.dir=${build.dir}/generated
13 | build.generated.sources.dir=${build.dir}/generated-sources
14 | # Only compile against the classpath explicitly listed here:
15 | build.sysclasspath=ignore
16 | build.test.classes.dir=${build.dir}/test/classes
17 | build.test.results.dir=${build.dir}/test/results
18 | debug.classpath=\
19 | ${run.classpath}
20 | debug.test.classpath=\
21 | ${run.test.classpath}
22 | # This directory is removed when the project is cleaned:
23 | dist.dir=dist
24 | dist.jar=${dist.dir}/SocketTest.jar
25 | dist.javadoc.dir=${dist.dir}/javadoc
26 | endorsed.classpath=
27 | excludes=
28 | file.reference.metouia.jar=lib/metouia.jar
29 | file.reference.swt.jar=lib/swt.jar
30 | includes=**
31 | jar.archive.disabled=${jnlp.enabled}
32 | jar.compress=false
33 | jar.index=${jnlp.enabled}
34 | javac.classpath=\
35 | ${file.reference.swt.jar}:\
36 | ${file.reference.metouia.jar}
37 | # Space-separated list of extra javac options
38 | javac.compilerargs=
39 | javac.deprecation=false
40 | javac.processorpath=\
41 | ${javac.classpath}
42 | javac.source=1.3
43 | javac.target=1.1
44 | javac.test.classpath=\
45 | ${javac.classpath}:\
46 | ${build.classes.dir}:\
47 | ${libs.junit.classpath}
48 | javac.test.processorpath=${javac.test.classpath}
49 | javadoc.additionalparam=
50 | javadoc.author=false
51 | javadoc.encoding=
52 | javadoc.noindex=false
53 | javadoc.nonavbar=false
54 | javadoc.notree=false
55 | javadoc.private=false
56 | javadoc.splitindex=true
57 | javadoc.use=true
58 | javadoc.version=false
59 | javadoc.windowtitle=
60 | jnlp.codebase.type=no.codebase
61 | jnlp.descriptor=application
62 | jnlp.enabled=false
63 | jnlp.mixed.code=default
64 | jnlp.offline-allowed=false
65 | jnlp.signed=false
66 | jnlp.signing=
67 | jnlp.signing.alias=
68 | jnlp.signing.keystore=
69 | main.class=net.sf.sockettest.SocketTest
70 | manifest.file=manifest.mf
71 | meta.inf.dir=${src.dir}/META-INF
72 | mkdist.disabled=false
73 | platform.active=default_platform
74 | run.classpath=\
75 | ${javac.classpath}:\
76 | ${build.classes.dir}
77 | # Space-separated list of JVM arguments used when running the project
78 | # (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value
79 | # or test-sys-prop.name=value to set system properties for unit tests):
80 | run.jvmargs=
81 | run.test.classpath=\
82 | ${javac.test.classpath}:\
83 | ${build.test.classes.dir}
84 | src.dir=src
85 | test.src.dir=test
86 |
--------------------------------------------------------------------------------
/nbproject/project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | org.netbeans.modules.java.j2seproject
4 |
5 |
6 | SocketTest
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/icons/ball.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/src/icons/ball.gif
--------------------------------------------------------------------------------
/src/icons/logo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/src/icons/logo.gif
--------------------------------------------------------------------------------
/src/net/sf/sockettest/MyTrustManager.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest;
2 |
3 | import javax.net.ssl.*;
4 | import java.security.cert.*;
5 | import java.awt.*;
6 | import javax.swing.*;
7 | import java.security.*;
8 |
9 | /**
10 | * Custom MyTrustManager
11 | * @author Benjamin Fleckenstein (Thanks Ben for your email)
12 | * @author Akshathkumar Shetty
13 | */
14 | public class MyTrustManager implements X509TrustManager {
15 |
16 | private Component parentComponent;
17 | private X509TrustManager sunJSSEX509TrustManager;
18 |
19 |
20 |
21 | public MyTrustManager(Component parentComponent) throws Exception {
22 | this.parentComponent = parentComponent;
23 |
24 | TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
25 |
26 |
27 | KeyStore ks = null;
28 | tmf.init((KeyStore)null);
29 |
30 | TrustManager tms [] = tmf.getTrustManagers();
31 |
32 |
33 | /*
34 | * Iterate over the returned trustmanagers, look
35 | * for an instance of X509TrustManager. If found,
36 | * use that as our "default" trust manager.
37 | */
38 | for(int i = 0; i < tms.length; i++) {
39 | if (tms[i] instanceof X509TrustManager) {
40 | sunJSSEX509TrustManager = (X509TrustManager) tms[i];
41 | break;
42 | }
43 | }
44 | if(sunJSSEX509TrustManager==null) throw new Exception("Couldn't initialize");
45 | }
46 |
47 | /*
48 | * The default X509TrustManager returned by SunX509. We'll delegate
49 | * decisions to it, and fall back to the logic in this class if the
50 | * default X509TrustManager doesn't trust it.
51 | */
52 |
53 | /*
54 | * Delegate to the default trust manager.
55 | */
56 | public void checkClientTrusted(X509Certificate[] chain, String authType)
57 | throws CertificateException {
58 | try {
59 | sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
60 | } catch (CertificateException excep) {
61 | // do any special handling here, or rethrow exception.
62 | throw excep;
63 | }
64 | }
65 |
66 | /*
67 | * Delegate to the default trust manager.
68 | */
69 | public void checkServerTrusted(X509Certificate[] chain, String authType)
70 | throws CertificateException {
71 | try {
72 | sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
73 | } catch (CertificateException excep) {
74 | /*
75 | * Possibly pop up a dialog box asking whether to trust the
76 | * cert chain.
77 | */
78 |
79 | StringBuffer sb = new StringBuffer();
80 | for(int i=0;i");
72 | //got = got.replaceAll("\r","");
73 | //parent.append("R: "+got);
74 | parent.appendnoNewLine(got);
75 | } catch(IOException e) {
76 | if(!desonnected) {
77 | parent.error(e.getMessage(),"Connection lost");
78 | parent.disconnect();
79 | }
80 | break;
81 | }
82 | }//end of while
83 | try {
84 | is.close();
85 | in.close();
86 | //socket.close();
87 | } catch (Exception err) {}
88 | socket=null;
89 | }//end of run
90 |
91 | private static String readInputStream(BufferedInputStream _in) throws IOException {
92 | String data = "";
93 | int s = _in.read();
94 | if(s==-1)
95 | return null;
96 | data += ""+(char)s;
97 | int len = _in.available();
98 | System.out.println("Len got : "+len);
99 | if(len > 0) {
100 | byte[] byteData = new byte[len];
101 | _in.read(byteData);
102 | data += new String(byteData);
103 | }
104 | return data;
105 | }
106 |
107 | }
108 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/SocketServer.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest;
2 |
3 | import java.net.*;
4 | import java.io.*;
5 | import net.sf.sockettest.swing.SocketTestServer;
6 |
7 | /**
8 | *
9 | * @author Akshathkumar Shetty
10 | */
11 | public class SocketServer extends Thread {
12 |
13 | private static SocketServer socketServer = null;
14 | private Socket socket = null;
15 | private ServerSocket server = null;
16 | private SocketTestServer parent;
17 | private BufferedInputStream in;
18 | private boolean desonnected=false;
19 | private boolean stop = false;
20 |
21 | //disconnect client
22 | public synchronized void setDesonnected(boolean cr) {
23 | if(socket!=null && cr==true) {
24 | try {
25 | socket.close();
26 | } catch (Exception e) {
27 | System.err.println("Error closing clinet : setDesonnected : "+e);
28 | }
29 | }
30 | desonnected=cr;
31 | //parent.setClientSocket(null);
32 | }
33 | //stop server
34 | public synchronized void setStop(boolean cr) {
35 | stop=cr;
36 | if(server!=null && cr==true) {
37 | try {
38 | server.close();
39 | } catch (Exception e) {
40 | System.err.println("Error closing server : setStop : "+e);
41 | }
42 | }
43 | }
44 |
45 | private SocketServer(SocketTestServer parent, ServerSocket s) {
46 | super("SocketServer");
47 | this.parent = parent;
48 | server=s;
49 | setStop(false);
50 | setDesonnected(false);
51 | start();
52 | }
53 |
54 |
55 |
56 | public static synchronized SocketServer handle(SocketTestServer parent,
57 | ServerSocket s) {
58 | if(socketServer==null)
59 | socketServer=new SocketServer(parent, s);
60 | else {
61 | if(socketServer.server!=null) {
62 | try {
63 | socketServer.setDesonnected(true);
64 | socketServer.setStop(true);
65 | if(socketServer.socket!=null)
66 | socketServer.socket.close();
67 | if(socketServer.server!=null)
68 | socketServer.server.close();
69 | } catch (Exception e) {
70 | parent.error(e.getMessage());
71 | }
72 | }
73 | socketServer.server = null;
74 | socketServer.socket = null;
75 | socketServer=new SocketServer(parent,s);
76 | }
77 | return socketServer;
78 | }
79 |
80 | public void run() {
81 | while(!stop) {
82 | try {
83 | socket = server.accept();
84 | } catch (Exception e) {
85 | if(!stop) {
86 | parent.error(e.getMessage(),"Error acception connection");
87 | stop=true;
88 | }
89 | continue;
90 | }
91 | startServer();
92 | if(socket!=null) {
93 | try {
94 | socket.close();
95 | } catch (Exception e) {
96 | System.err.println("Erro closing client socket : "+e);
97 | }
98 | socket=null;
99 | parent.setClientSocket(socket);
100 | }
101 | }//end of while
102 | }//end of run
103 |
104 | private void startServer() {
105 | parent.setClientSocket(socket);
106 | InputStream is = null;
107 | parent.append("> New Client: "+socket.getInetAddress().getHostAddress());
108 | try {
109 | is = socket.getInputStream();
110 | in = new BufferedInputStream(is);
111 | } catch(IOException e) {
112 | parent.append("> Cound't open input stream on Clinet "+e.getMessage());
113 | setDesonnected(true);
114 | return;
115 | }
116 |
117 | String rec=null;
118 | while(true) {
119 | rec=null;
120 | try {
121 | rec = readInputStream(in);//in.readLine();
122 | } catch (Exception e) {
123 | setDesonnected(true);
124 | if(!desonnected) {
125 | parent.error(e.getMessage(),"Lost Client conection");
126 | parent.append("> Server lost Client conection.");
127 | } else
128 | parent.append("> Server closed Client conection.");
129 | break;
130 | }
131 |
132 | if (rec != null) {
133 | //rec = rec.replaceAll("\n","");
134 | //rec = rec.replaceAll("\r","");
135 | //parent.append("R: "+rec);
136 | parent.appendnoNewLine(rec);
137 | } else {
138 | setDesonnected(true);
139 | parent.append("> Client closed conection.");
140 | break;
141 | }
142 | } //end of while
143 | } //end of startServer
144 |
145 | private static String readInputStream(BufferedInputStream _in)
146 | throws IOException {
147 | String data = "";
148 | int s = _in.read();
149 | if(s==-1)
150 | return null;
151 | data += ""+(char)s;
152 | int len = _in.available();
153 | System.out.println("Len got : "+len);
154 | if(len > 0) {
155 | byte[] byteData = new byte[len];
156 | _in.read(byteData);
157 | data += new String(byteData);
158 | }
159 | return data;
160 | }
161 | }
162 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/SocketTest.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest;
2 |
3 | import java.awt.*;
4 | import java.util.HashMap;
5 | import java.util.Map;
6 | import javax.swing.*;
7 |
8 | import net.sf.sockettest.swing.About;
9 | import net.sf.sockettest.swing.SocketTestClient;
10 | import net.sf.sockettest.swing.SocketTestServer;
11 | import net.sf.sockettest.swing.SocketTestUdp;
12 | import net.sf.sockettest.swing.SplashScreen;
13 |
14 | /**
15 | * @author Akshathkumar Shetty
16 | */
17 | public class SocketTest extends JFrame {
18 | private static final String CLIENT = "c";
19 | private static final String SERVER = "s";
20 | private static final String UDP = "u";
21 | private ClassLoader cl = getClass().getClassLoader();
22 | public ImageIcon logo = new ImageIcon(cl.getResource("icons/logo.gif"));
23 | public ImageIcon ball = new ImageIcon(cl.getResource("icons/ball.gif"));
24 | private JTabbedPane tabbedPane;
25 |
26 | private static Map services = new HashMap<>();
27 |
28 | /**
29 | * Creates a new instance of SocketTest
30 | */
31 | public SocketTest() {
32 | Container cp = getContentPane();
33 |
34 | tabbedPane = new JTabbedPane(JTabbedPane.TOP);
35 | SocketTestClient client = new SocketTestClient(this);
36 | SocketTestServer server = new SocketTestServer(this);
37 | SocketTestUdp udp = new SocketTestUdp(this);
38 | About about = new About();
39 |
40 | services.put(CLIENT, client);
41 | services.put(SERVER, server);
42 | services.put(UDP, udp);
43 |
44 | tabbedPane.addTab("Client", ball, (Component) client, "Test any server");
45 | tabbedPane.addTab("Server", ball, server, "Test any client");
46 | tabbedPane.addTab("Udp", ball, udp, "Test any UDP Client or Server");
47 | tabbedPane.addTab("About", ball, about, "About SocketTest");
48 |
49 | tabbedPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
50 | cp.add(tabbedPane);
51 | }
52 |
53 | /**
54 | * @param args the command line arguments
55 | */
56 | public static void main(String[] args) {
57 | try {
58 | UIManager.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
59 | } catch (Exception e) {
60 | //e.printStackTrace();
61 | try {
62 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
63 | } catch (Exception ee) {
64 | System.out.println("Error setting native LAF: " + ee);
65 | }
66 | }
67 |
68 | boolean toSplash = true;
69 | if (args.length > 0) {
70 | if ("nosplash".equals(args[0])) toSplash = false;
71 | }
72 |
73 | SplashScreen splash = null;
74 | if (toSplash) splash = new SplashScreen();
75 |
76 | SocketTest st = new SocketTest();
77 | st.setTitle("SocketTest v 3.0.1");
78 | st.setSize(600, 500);
79 | Util.centerWindow(st);
80 | st.setDefaultCloseOperation(EXIT_ON_CLOSE);
81 | st.setIconImage(st.logo.getImage());
82 |
83 | setUpArgParams(args);
84 |
85 | if (toSplash) splash.kill();
86 | st.setVisible(true);
87 | }
88 |
89 | private static void setUpArgParams(String[] configuration) {
90 | if (configuration.length < 1) {
91 | return;
92 | }
93 |
94 | //c:ip:port
95 | //s:ip:port
96 | //u:ip:port
97 | for (String arg : configuration) {
98 | String[] params = arg.split(":");
99 | if (params.length == 3) {
100 | String service = params[0];
101 | String ip = params[1];
102 | String port = params[2];
103 | services.get(service).setUpConfiguration(ip, port);
104 | }
105 | }
106 | }
107 |
108 | }
109 |
110 |
111 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/UdpServer.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest;
2 |
3 | import java.net.*;
4 | import java.io.*;
5 | import net.sf.sockettest.swing.SocketTestUdp;
6 | /**
7 | *
8 | * @author Akshathkumar Shetty
9 | */
10 | public class UdpServer extends Thread {
11 | public static int BUFFER_SIZE = 1024;
12 |
13 | private static UdpServer udpServer = null;
14 | //for listening for client responses
15 | private static UdpServer udpServer2 = null;
16 |
17 |
18 | private DatagramSocket server;
19 | private SocketTestUdp parent;
20 | private boolean stop = false;
21 | private byte buffer[] = new byte[BUFFER_SIZE];
22 |
23 | //stop server
24 | public synchronized void setStop(boolean cr) {
25 | stop=cr;
26 | if(server!=null && cr==true) {
27 | /*
28 | if(server instanceof MulticastSocket) {
29 | MulticastSocket ms = (MulticastSocket) server;
30 | ms.leaveGroup(?);
31 | }
32 | */
33 | try {
34 | server.close();
35 | } catch (Exception e) {
36 | System.err.println("Error closing server : setStop : "+e);
37 | }
38 | }
39 | }
40 |
41 | private UdpServer(SocketTestUdp parent, DatagramSocket s) {
42 | super("SocketUdp");
43 | this.parent = parent;
44 | server=s;
45 | setStop(false);
46 | start();
47 | }
48 |
49 | public static synchronized UdpServer handle(SocketTestUdp parent,
50 | DatagramSocket s) {
51 | if(udpServer==null)
52 | udpServer=new UdpServer(parent, s);
53 | else {
54 | if(udpServer.server!=null) {
55 | try {
56 | udpServer.setStop(true);
57 | if(udpServer.server!=null)
58 | udpServer.server.close();
59 | } catch (Exception e) {
60 | parent.error(e.getMessage());
61 | }
62 | }
63 | udpServer.server = null;
64 | udpServer=new UdpServer(parent,s);
65 | }
66 | return udpServer;
67 | }
68 |
69 | public static synchronized UdpServer handleClient(SocketTestUdp parent,
70 | DatagramSocket s) {
71 | if(udpServer2==null)
72 | udpServer2=new UdpServer(parent, s);
73 | else {
74 | if(udpServer2.server!=null) {
75 | try {
76 | udpServer2.setStop(true);
77 | if(udpServer2.server!=null)
78 | udpServer2.server.close();
79 | } catch (Exception e) {
80 | parent.error(e.getMessage());
81 | }
82 | }
83 | udpServer2.server = null;
84 | udpServer2=new UdpServer(parent,s);
85 | }
86 | return udpServer2;
87 | }
88 |
89 | public void run() {
90 | DatagramPacket pack = new DatagramPacket(buffer,buffer.length);
91 | while(!stop) {
92 | try {
93 | server.receive(pack);
94 | if(udpServer!=null) {
95 | if(server == udpServer.server) {
96 | parent.append("R[" + pack.getAddress().getHostAddress()+":"
97 | +pack.getPort()+"]: " +
98 | new String(pack.getData(),0,pack.getLength()) );
99 | } else {
100 | parent.append("R: " +
101 | new String(pack.getData(),0,pack.getLength()) );
102 | }
103 | } else {
104 | parent.append("R: " +new String(pack.getData(),0,pack.getLength()) );
105 | }
106 | } catch (Exception e) {
107 | if(!stop) {
108 | parent.error(e.getMessage(),"Error acception connection");
109 | stop=true;
110 | }
111 | continue;
112 | }
113 | }//end of while
114 | }//end of run
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/Util.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest;
2 |
3 | import java.net.*;
4 | import java.io.*;
5 | import java.awt.*;
6 |
7 | /**
8 | *
9 | * @author Akshathkumar Shetty
10 | */
11 | public class Util {
12 |
13 | public static void centerWindow(Window win) {
14 | Dimension dim = win.getToolkit().getScreenSize();
15 | win.setLocation(dim.width/2 - win.getWidth()/2,
16 | dim.height/2 - win.getHeight()/2);
17 | }
18 |
19 | public static boolean checkHost(String host) {
20 | try {
21 | InetAddress.getByName(host);
22 | return(true);
23 | } catch(UnknownHostException uhe) {
24 | return(false);
25 | }
26 | }
27 |
28 | public static void writeFile(String fileName, String text)
29 | throws IOException {
30 | PrintWriter out = new PrintWriter(
31 | new BufferedWriter(new FileWriter(fileName)));
32 | out.print(text);
33 | out.close();
34 | }
35 |
36 | public static String readFile(String fileName, Object parent)
37 | throws IOException {
38 | StringBuffer sb = new StringBuffer();
39 | ClassLoader cl = parent.getClass().getClassLoader();
40 | InputStream is = cl.getResourceAsStream(fileName);
41 | BufferedReader in = new BufferedReader(new InputStreamReader(is));
42 | String s;
43 | while((s = in.readLine()) != null) {
44 | sb.append(s);
45 | sb.append("\n");
46 | }
47 | in.close();
48 | return sb.toString();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/swing/About.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest.swing;
2 |
3 | import java.awt.*;
4 | import java.awt.event.*;
5 | import javax.swing.*;
6 | import javax.swing.event.*;
7 | import javax.swing.border.*;
8 | import java.io.IOException;
9 | import net.sf.sockettest.*;
10 |
11 | /**
12 | * About Tab
13 | * @author Akshathkumar Shetty
14 | */
15 | public class About extends JPanel /*JFrame*/ {
16 | private ClassLoader cl = getClass().getClassLoader();
17 | public ImageIcon logo = new ImageIcon(
18 | cl.getResource("icons/logo.gif"));
19 | public ImageIcon ball = new ImageIcon(
20 | cl.getResource("icons/ball.gif"));
21 |
22 | private JPanel centerPanel;
23 | private JPanel topPanel;
24 |
25 | private JLabel productName = new JLabel(""+
26 | "SocketTest",logo,JLabel.CENTER);
27 | private JTextArea readme = new JTextArea();
28 | private JScrollPane jsp;
29 |
30 | String html="";
31 |
32 | private JLabel versionText = new JLabel(html+"Version",ball,JLabel.LEFT);
33 | private JLabel version = new JLabel(html+": 3.0.0", JLabel.LEFT);
34 |
35 | private JLabel licenceText = new JLabel(html+"Licence",ball,JLabel.LEFT);
36 | private JLabel licence = new JLabel(html+": GNU Lesser General Public License", JLabel.LEFT);
37 |
38 | private JLabel authorText = new JLabel(html+"Author",ball,JLabel.LEFT);
39 | private JLabel author = new JLabel(html+": Akshathkumar Shetty", JLabel.LEFT);
40 |
41 | private JLabel copyrightText = new JLabel(html+"Copyright © 2003-2008 Akshathkumar Shetty",ball,JLabel.LEFT);
42 |
43 | private JLabel websiteText = new JLabel(html+"Website",ball,JLabel.LEFT);
44 | private JLabel website = new JLabel(html+": http://sockettest.sourceforge.net/", JLabel.LEFT);
45 |
46 | private JLabel readmeText = new JLabel(html+"ReadMe",ball,JLabel.LEFT);
47 |
48 | private GridBagConstraints gbc = new GridBagConstraints();
49 |
50 | /** Creates a new instance of About */
51 | public About() {
52 | //Container cp = getContentPane();
53 | Container cp = this;
54 |
55 | topPanel = new JPanel();
56 | topPanel.setLayout(new GridBagLayout());
57 | gbc.insets = new Insets( 2, 2, 2, 2 );
58 | gbc.weighty = 0.0;
59 | gbc.weightx = 0.0;
60 | gbc.gridx = 0;
61 | gbc.gridy = 0;
62 | gbc.gridheight = 1;
63 | gbc.gridwidth = 3;
64 | gbc.anchor = GridBagConstraints.CENTER;
65 | gbc.fill = GridBagConstraints.NONE;
66 | topPanel.add(productName, gbc);
67 |
68 | gbc.anchor = GridBagConstraints.WEST;
69 | gbc.fill = GridBagConstraints.HORIZONTAL;
70 |
71 | gbc.gridy = 1;
72 | gbc.gridx = 0;
73 | gbc.weightx = 0.0;
74 | topPanel.add(versionText, gbc);
75 | gbc.gridx = 1;
76 | topPanel.add(version, gbc);
77 | gbc.gridx = 2;
78 | gbc.weightx = 1.0;
79 | gbc.fill = GridBagConstraints.HORIZONTAL;
80 | topPanel.add(Box.createHorizontalGlue(), gbc);
81 |
82 | gbc.gridy = 2;
83 | gbc.gridx = 0;
84 | gbc.weightx = 0.0;
85 | topPanel.add(licenceText, gbc);
86 | gbc.gridx = 1;
87 | topPanel.add(licence, gbc);
88 | gbc.gridx = 2;
89 | gbc.weightx = 1.0;//1.0
90 | gbc.fill = GridBagConstraints.HORIZONTAL;
91 | topPanel.add(new JLabel(), gbc);
92 |
93 | gbc.gridy = 3;
94 | gbc.gridx = 0;
95 | gbc.weightx = 0.0;
96 | topPanel.add(authorText, gbc);
97 | gbc.gridx = 1;
98 | topPanel.add(author, gbc);
99 | gbc.gridx = 2;
100 | gbc.weightx = 1.0;
101 | gbc.fill = GridBagConstraints.HORIZONTAL;
102 | topPanel.add(Box.createHorizontalGlue(), gbc);
103 |
104 | gbc.gridy = 4;
105 | gbc.gridx = 0;
106 | gbc.weightx = 0.0;
107 | topPanel.add(websiteText, gbc);
108 | gbc.gridx = 1;
109 | topPanel.add(website, gbc);
110 | gbc.gridx = 2;
111 | gbc.weightx = 1.0;
112 | gbc.fill = GridBagConstraints.HORIZONTAL;
113 | topPanel.add(Box.createHorizontalGlue(), gbc);
114 |
115 | gbc.gridy = 5;
116 | gbc.gridx = 0;
117 | gbc.weightx = 0.0;
118 | gbc.gridwidth = 2;
119 | topPanel.add(copyrightText, gbc);
120 | gbc.gridwidth = 1;
121 | gbc.gridx = 2;
122 | gbc.weightx = 1.0;
123 | gbc.fill = GridBagConstraints.HORIZONTAL;
124 | topPanel.add(Box.createHorizontalGlue(), gbc);
125 |
126 | gbc.gridy = 6;
127 | gbc.gridx = 0;
128 | gbc.weightx = 0.0;
129 | gbc.gridwidth = 1;
130 | topPanel.add(readmeText, gbc);
131 | gbc.gridx = 1;
132 | topPanel.add(new JLabel(" "), gbc);
133 | gbc.gridx = 2;
134 | gbc.weightx = 1.0;
135 | gbc.fill = GridBagConstraints.HORIZONTAL;
136 | topPanel.add(Box.createHorizontalGlue(), gbc);
137 |
138 |
139 | centerPanel = new JPanel();
140 | readme.setText("Loading... readme");
141 | try {
142 | String cont = Util.readFile("readme.txt",(Object)About.this);
143 | readme.setText(cont);
144 | } catch (IOException e){
145 | System.err.println("Error reading readme.txt "+e);
146 | readme.append("\r\nFailed : "+e.getMessage());
147 | }
148 | readme.setEditable(false);
149 | readme.setLineWrap(true);
150 | readme.setWrapStyleWord(true);
151 | jsp = new JScrollPane(readme);
152 | centerPanel.setLayout(new BorderLayout());
153 | centerPanel.add(jsp);
154 | centerPanel.setBorder(BorderFactory.createEmptyBorder(0,9,0,9));
155 |
156 | cp.setLayout(new BorderLayout(0,10));
157 | cp.add(topPanel,BorderLayout.NORTH);
158 | cp.add(centerPanel,BorderLayout.CENTER);
159 | setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
160 | }
161 |
162 | }
163 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/swing/PortDialog.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest.swing;
2 |
3 | import javax.swing.*;
4 | import javax.swing.table.*;
5 | import java.awt.*;
6 | import java.io.IOException;
7 | import net.sf.sockettest.PortModel;
8 | import net.sf.sockettest.*;
9 |
10 | /**
11 | *
12 | * @author Akshathkumar Shetty
13 | */
14 | public class PortDialog extends JDialog {
15 | public static final int UDP = 1;
16 | public static final int TCP = 2;
17 | private PortModel model;
18 |
19 | /** Creates a new instance of PortDialog */
20 | public PortDialog(JFrame parent, int type) {
21 | super(parent);
22 | if(type==TCP) {
23 | setTitle("Standard TCP Port");
24 | model = new PortModel("tcpports.txt");
25 | } else {
26 | setTitle("Select UDP port");
27 | model = new PortModel("udpports.txt");
28 | }
29 | Container cp = getContentPane();
30 |
31 | JTable table = new JTable(model);
32 | cp.add(new JScrollPane(table));
33 | setSize(300,200);
34 | Util.centerWindow(this);
35 | }
36 |
37 | public String getPort() {
38 | return model.getPort();
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/swing/SocketTestClient.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest.swing;
2 |
3 | import java.awt.*;
4 | import java.awt.event.*;
5 | import javax.swing.*;
6 | import javax.swing.event.*;
7 | import javax.swing.border.*;
8 |
9 | import java.security.SecureRandom;
10 |
11 | import java.net.*;
12 | import java.io.*;
13 |
14 | import javax.net.*;
15 | import javax.net.ssl.*;
16 |
17 | import net.sf.sockettest.*;
18 |
19 | /**
20 | * @author Akshathkumar Shetty
21 | */
22 | public class SocketTestClient extends JPanel implements NetService{
23 |
24 | private final String NEW_LINE = "\r\n";
25 | private ClassLoader cl = getClass().getClassLoader();
26 | public ImageIcon logo = new ImageIcon(
27 | cl.getResource("icons/logo.gif"));
28 |
29 | private JPanel topPanel;
30 | private JPanel toPanel;
31 |
32 | private JPanel centerPanel;
33 | private JPanel textPanel;
34 | private JPanel buttonPanel;
35 | private JPanel sendPanel;
36 |
37 | private JLabel ipLabel = new JLabel("IP Address");
38 | private JLabel portLabel = new JLabel("Port");
39 | private JLabel logoLabel = new JLabel("SocketTest v 3.0", logo,
40 | JLabel.CENTER);
41 | private JTextField ipField = new JTextField("127.0.0.1", 20);
42 | private JTextField portField = new JTextField("21", 10);
43 | private JButton portButton = new JButton("Port");
44 | private JButton connectButton = new JButton("Connect");
45 |
46 | private JLabel convLabel = new JLabel("Conversation with host");
47 | private Border connectedBorder = BorderFactory.createTitledBorder(new EtchedBorder(), "Connected To < NONE >");
48 | private JTextArea messagesField = new JTextArea();
49 |
50 | private JLabel sendLabel = new JLabel("Message");
51 | private JTextField sendField = new JTextField();
52 |
53 | private JButton sendButton = new JButton("Send");
54 | private JButton saveButton = new JButton("Save");
55 | private JButton clearButton = new JButton("Clear");
56 |
57 | private JCheckBox secureButton = new JCheckBox("Secure");
58 | private boolean isSecure = false;
59 | private GridBagConstraints gbc = new GridBagConstraints();
60 |
61 | private Socket socket;
62 | private PrintWriter out;
63 | private SocketClient socketClient;
64 | protected final JFrame parent;
65 |
66 | public SocketTestClient(final JFrame parent) {
67 | //Container cp = getContentPane();
68 | this.parent = parent;
69 | Container cp = this;
70 |
71 | topPanel = new JPanel();
72 | toPanel = new JPanel();
73 | toPanel.setLayout(new GridBagLayout());
74 | gbc.insets = new Insets(2, 2, 2, 2);
75 | gbc.weighty = 0.0;
76 | gbc.weightx = 0.0;
77 | gbc.gridx = 0;
78 | gbc.gridy = 0;
79 | gbc.gridheight = 1;
80 | gbc.gridwidth = 1;
81 | gbc.anchor = GridBagConstraints.EAST;
82 | gbc.fill = GridBagConstraints.NONE;
83 | toPanel.add(ipLabel, gbc);
84 |
85 | gbc.weightx = 1.0; //streach
86 | gbc.gridx = 1;
87 | gbc.gridwidth = 4;
88 | gbc.fill = GridBagConstraints.HORIZONTAL;
89 | ActionListener ipListener = new ActionListener() {
90 | public void actionPerformed(ActionEvent e) {
91 | portField.requestFocus();
92 | }
93 | };
94 | ipField.addActionListener(ipListener);
95 | toPanel.add(ipField, gbc);
96 |
97 | gbc.weightx = 0.0;
98 | gbc.gridy = 1;
99 | gbc.gridx = 0;
100 | gbc.gridwidth = 1;
101 | gbc.fill = GridBagConstraints.NONE;
102 | toPanel.add(portLabel, gbc);
103 |
104 | gbc.weightx = 1.0;
105 | gbc.gridy = 1;
106 | gbc.gridx = 1;
107 | gbc.gridwidth = 1;
108 | gbc.fill = GridBagConstraints.HORIZONTAL;
109 | ActionListener connectListener = new ActionListener() {
110 | public void actionPerformed(ActionEvent e) {
111 | connect();
112 | }
113 | };
114 | portField.addActionListener(connectListener);
115 | toPanel.add(portField, gbc);
116 |
117 | gbc.weightx = 0.0;
118 | gbc.gridy = 1;
119 | gbc.gridx = 2;
120 | gbc.gridwidth = 1;
121 | gbc.fill = GridBagConstraints.NONE;
122 | portButton.setMnemonic('P');
123 | portButton.setToolTipText("View Standard Ports");
124 | ActionListener portButtonListener = new ActionListener() {
125 | public void actionPerformed(ActionEvent e) {
126 | PortDialog dia = new PortDialog(parent, PortDialog.TCP);
127 | dia.show();
128 | }
129 | };
130 | portButton.addActionListener(portButtonListener);
131 | toPanel.add(portButton, gbc);
132 |
133 | gbc.weightx = 0.0;
134 | gbc.gridy = 1;
135 | gbc.gridx = 3;
136 | gbc.gridwidth = 1;
137 | gbc.fill = GridBagConstraints.NONE;
138 | connectButton.setMnemonic('C');
139 | connectButton.setToolTipText("Start Connection");
140 | connectButton.addActionListener(connectListener);
141 | toPanel.add(connectButton, gbc);
142 |
143 |
144 | gbc.weightx = 0.0;
145 | gbc.gridy = 1;
146 | gbc.gridx = 4;
147 | gbc.gridwidth = 1;
148 | gbc.fill = GridBagConstraints.NONE;
149 | secureButton.setToolTipText("Set Has Secure");
150 | secureButton.addItemListener(
151 | new ItemListener() {
152 | public void itemStateChanged(ItemEvent e) {
153 | isSecure = !isSecure;
154 | }
155 | });
156 | toPanel.add(secureButton, gbc);
157 |
158 |
159 | toPanel.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(), "Connect To"));
160 | topPanel.setLayout(new BorderLayout(10, 0));
161 | topPanel.add(toPanel);
162 | logoLabel.setVerticalTextPosition(JLabel.BOTTOM);
163 | logoLabel.setHorizontalTextPosition(JLabel.CENTER);
164 | topPanel.add(logoLabel, BorderLayout.EAST);
165 | topPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 5, 10));
166 |
167 |
168 | textPanel = new JPanel();
169 | textPanel.setLayout(new BorderLayout(0, 5));
170 | textPanel.add(convLabel, BorderLayout.NORTH);
171 | messagesField.setEditable(false);
172 | JScrollPane jsp = new JScrollPane(messagesField);
173 | textPanel.add(jsp);
174 | textPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 0, 3));
175 |
176 | sendPanel = new JPanel();
177 | sendPanel.setLayout(new GridBagLayout());
178 | gbc.weighty = 0.0;
179 | gbc.weightx = 0.0;
180 | gbc.gridx = 0;
181 | gbc.gridy = 0;
182 | gbc.gridheight = 1;
183 | gbc.gridwidth = 1;
184 | gbc.anchor = GridBagConstraints.EAST;
185 | gbc.fill = GridBagConstraints.NONE;
186 | sendPanel.add(sendLabel, gbc);
187 | gbc.gridx = 1;
188 | gbc.weightx = 1.0;
189 | gbc.fill = GridBagConstraints.HORIZONTAL;
190 | sendField.setEditable(false);
191 | sendPanel.add(sendField, gbc);
192 | gbc.gridx = 2;
193 | gbc.weightx = 0.0;
194 | gbc.fill = GridBagConstraints.NONE;
195 | sendButton.setEnabled(false);
196 | sendButton.setToolTipText("Send text to host");
197 | ActionListener sendListener = new ActionListener() {
198 | public void actionPerformed(ActionEvent e) {
199 | String msg = sendField.getText();
200 | if (!msg.equals(""))
201 | sendMessage(msg);
202 | else {
203 | int value = JOptionPane.showConfirmDialog(
204 | SocketTestClient.this, "Send Blank Line ?",
205 | "Send Data To Server",
206 | JOptionPane.YES_NO_OPTION);
207 | if (value == JOptionPane.YES_OPTION)
208 | sendMessage(msg);
209 | }
210 | }
211 | };
212 | sendButton.addActionListener(sendListener);
213 | sendField.addActionListener(sendListener);
214 | sendPanel.add(sendButton, gbc);
215 | sendPanel.setBorder(
216 | new CompoundBorder(
217 | BorderFactory.createEmptyBorder(0, 0, 0, 3),
218 | BorderFactory.createTitledBorder("Send")));
219 |
220 | buttonPanel = new JPanel();
221 | buttonPanel.setLayout(new GridBagLayout());
222 | gbc.weighty = 0.0;
223 | gbc.weightx = 1.0;
224 | gbc.gridx = 0;
225 | gbc.gridy = 0;
226 | gbc.gridheight = 2;
227 | gbc.gridwidth = 1;
228 | gbc.anchor = GridBagConstraints.EAST;
229 | gbc.fill = GridBagConstraints.BOTH;
230 | buttonPanel.add(sendPanel, gbc);
231 | gbc.weighty = 0.0;
232 | gbc.weightx = 0.0;
233 | gbc.gridx = 1;
234 | gbc.gridheight = 1;
235 | gbc.fill = GridBagConstraints.HORIZONTAL;
236 | saveButton.setToolTipText("Save conversation with host to a file");
237 | saveButton.setMnemonic('S');
238 | ActionListener saveListener = new ActionListener() {
239 | public void actionPerformed(ActionEvent e) {
240 | String text = messagesField.getText();
241 | if (text.equals("")) {
242 | error("Nothing to save", "Save to file");
243 | return;
244 | }
245 | String fileName = "";
246 | JFileChooser chooser = new JFileChooser();
247 | chooser.setCurrentDirectory(new File("."));
248 | int returnVal = chooser.showSaveDialog(SocketTestClient.this);
249 | if (returnVal == JFileChooser.APPROVE_OPTION) {
250 | fileName = chooser.getSelectedFile().getAbsolutePath();
251 | try {
252 | Util.writeFile(fileName, text);
253 | } catch (Exception ioe) {
254 | JOptionPane.showMessageDialog(SocketTestClient.this,
255 | "" + ioe.getMessage(),
256 | "Error saving to file..",
257 | JOptionPane.ERROR_MESSAGE);
258 | }
259 | }
260 | }
261 | };
262 | saveButton.addActionListener(saveListener);
263 | buttonPanel.add(saveButton, gbc);
264 | gbc.gridy = 1;
265 | clearButton.setToolTipText("Clear conversation with host");
266 | clearButton.setMnemonic('C');
267 | ActionListener clearListener = new ActionListener() {
268 | public void actionPerformed(ActionEvent e) {
269 | messagesField.setText("");
270 | }
271 | };
272 | clearButton.addActionListener(clearListener);
273 | buttonPanel.add(clearButton, gbc);
274 | buttonPanel.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 3));
275 |
276 | centerPanel = new JPanel();
277 | centerPanel.setLayout(new BorderLayout(0, 10));
278 | centerPanel.add(buttonPanel, BorderLayout.SOUTH);
279 | centerPanel.add(textPanel, BorderLayout.CENTER);
280 |
281 | CompoundBorder cb = new CompoundBorder(
282 | BorderFactory.createEmptyBorder(5, 10, 10, 10),
283 | connectedBorder);
284 | centerPanel.setBorder(cb);
285 |
286 | cp.setLayout(new BorderLayout(10, 0));
287 | cp.add(topPanel, BorderLayout.NORTH);
288 | cp.add(centerPanel, BorderLayout.CENTER);
289 | }
290 |
291 | /*
292 | public static void main(String args[]) {
293 | SocketTestClient client=new SocketTestClient();
294 | client.setTitle("SocketTest Client");
295 | //client.pack();
296 | client.setSize(500,400);
297 | Util.centerWindow(client);
298 | client.setDefaultCloseOperation(EXIT_ON_CLOSE);
299 | client.setIconImage(client.logo.getImage());
300 | client.setVisible(true);
301 | }
302 | */
303 |
304 | /////////////////
305 | //action methods
306 | //////////////////
307 | private void connect() {
308 | if (socket != null) {
309 | disconnect();
310 | return;
311 | }
312 | String ip = ipField.getText();
313 | String port = portField.getText();
314 | if (ip == null || ip.equals("")) {
315 | JOptionPane.showMessageDialog(SocketTestClient.this,
316 | "No IP Address. Please enter IP Address",
317 | "Error connecting", JOptionPane.ERROR_MESSAGE);
318 | ipField.requestFocus();
319 | ipField.selectAll();
320 | return;
321 | }
322 | if (port == null || port.equals("")) {
323 | JOptionPane.showMessageDialog(SocketTestClient.this,
324 | "No Port number. Please enter Port number",
325 | "Error connecting", JOptionPane.ERROR_MESSAGE);
326 | portField.requestFocus();
327 | portField.selectAll();
328 | return;
329 | }
330 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
331 | if (!Util.checkHost(ip)) {
332 | JOptionPane.showMessageDialog(SocketTestClient.this,
333 | "Bad IP Address",
334 | "Error connecting", JOptionPane.ERROR_MESSAGE);
335 | ipField.requestFocus();
336 | ipField.selectAll();
337 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
338 | return;
339 | }
340 | int portNo = 0;
341 | try {
342 | portNo = Integer.parseInt(port);
343 | } catch (Exception e) {
344 | JOptionPane.showMessageDialog(SocketTestClient.this,
345 | "Bad Port number. Please enter Port number",
346 | "Error connecting", JOptionPane.ERROR_MESSAGE);
347 | portField.requestFocus();
348 | portField.selectAll();
349 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
350 | return;
351 | }
352 | try {
353 | if (isSecure == false) {
354 | System.out.println("Connectig in normal mode : " + ip + ":" + portNo);
355 | socket = new Socket(ip, portNo);
356 | } else {
357 | System.out.println("Connectig in secure mode : " + ip + ":" + portNo);
358 | //SocketFactory factory = SSLSocketFactory.getDefault();
359 |
360 | TrustManager[] tm = new TrustManager[]{new MyTrustManager(SocketTestClient.this)};
361 |
362 | SSLContext context = SSLContext.getInstance("TLS");
363 | context.init(new KeyManager[0], tm, new SecureRandom());
364 |
365 | SSLSocketFactory factory = context.getSocketFactory();
366 | socket = factory.createSocket(ip, portNo);
367 | }
368 |
369 | ipField.setEditable(false);
370 | portField.setEditable(false);
371 | connectButton.setText("Disconnect");
372 | connectButton.setMnemonic('D');
373 | connectButton.setToolTipText("Stop Connection");
374 | sendButton.setEnabled(true);
375 | sendField.setEditable(true);
376 | } catch (Exception e) {
377 | e.printStackTrace();
378 | error(e.getMessage(), "Opening connection");
379 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
380 | return;
381 | }
382 | changeBorder(" " + socket.getInetAddress().getHostName() +
383 | " [" + socket.getInetAddress().getHostAddress() + "] ");
384 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
385 | messagesField.setText("");
386 | socketClient = SocketClient.handle(this, socket);
387 | sendField.requestFocus();
388 | }
389 |
390 | public synchronized void disconnect() {
391 | try {
392 | socketClient.setDesonnected(true);
393 | socket.close();
394 | } catch (Exception e) {
395 | System.err.println("Error closing client : " + e);
396 | }
397 | socket = null;
398 | out = null;
399 | changeBorder(null);
400 | ipField.setEditable(true);
401 | portField.setEditable(true);
402 | connectButton.setText("Connect");
403 | connectButton.setMnemonic('C');
404 | connectButton.setToolTipText("Start Connection");
405 | sendButton.setEnabled(false);
406 | sendField.setEditable(false);
407 | }
408 |
409 | public void error(String error) {
410 | if (error == null || error.equals(""))
411 | return;
412 | JOptionPane.showMessageDialog(SocketTestClient.this,
413 | error, "Error", JOptionPane.ERROR_MESSAGE);
414 | }
415 |
416 | public void error(String error, String heading) {
417 | if (error == null || error.equals(""))
418 | return;
419 | JOptionPane.showMessageDialog(SocketTestClient.this,
420 | error, heading, JOptionPane.ERROR_MESSAGE);
421 | }
422 |
423 | public void append(String msg) {
424 | messagesField.append(msg + NEW_LINE);
425 | messagesField.setCaretPosition(messagesField.getText().length());
426 | }
427 |
428 | public void appendnoNewLine(String msg) {
429 | messagesField.append(msg);
430 | messagesField.setCaretPosition(messagesField.getText().length());
431 | }
432 |
433 | public void sendMessage(String s) {
434 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
435 | try {
436 | if (out == null) {
437 | out = new PrintWriter(new BufferedWriter(
438 | new OutputStreamWriter(socket.getOutputStream())), true);
439 | }
440 | append("S: " + s);
441 | out.print(s + NEW_LINE);
442 | out.flush();
443 | sendField.setText("");
444 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
445 | } catch (Exception e) {
446 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
447 | JOptionPane.showMessageDialog(SocketTestClient.this,
448 | e.getMessage(), "Error Sending Message",
449 | JOptionPane.ERROR_MESSAGE);
450 | disconnect();
451 | }
452 | }
453 |
454 | private void changeBorder(String ip) {
455 | if (ip == null || ip.equals(""))
456 | connectedBorder = BorderFactory.createTitledBorder(
457 | new EtchedBorder(), "Connected To < NONE >");
458 | else
459 | connectedBorder = BorderFactory.createTitledBorder(
460 | new EtchedBorder(), "Connected To < " + ip + " >");
461 | CompoundBorder cb = new CompoundBorder(
462 | BorderFactory.createEmptyBorder(5, 10, 10, 10),
463 | connectedBorder);
464 | centerPanel.setBorder(cb);
465 | invalidate();
466 | repaint();
467 | }
468 |
469 | public void setUpConfiguration(String ip, String port) {
470 | ipField.setText(ip);
471 | portField.setText(port);
472 | }
473 |
474 | }
475 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/swing/SocketTestServer.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest.swing;
2 |
3 | import java.awt.*;
4 | import java.awt.event.*;
5 | import javax.swing.*;
6 | import javax.swing.event.*;
7 | import javax.swing.border.*;
8 |
9 | import java.net.*;
10 | import java.io.*;
11 |
12 | import net.sf.sockettest.*;
13 |
14 | /**
15 | *
16 | * @author Akshathkumar Shetty
17 | */
18 | public class SocketTestServer extends JPanel implements NetService/*JFrame*/ {
19 | private final String NEW_LINE = "\r\n";
20 | private ClassLoader cl = getClass().getClassLoader();
21 | public ImageIcon logo = new ImageIcon(
22 | cl.getResource("icons/logo.gif"));
23 |
24 | private JPanel topPanel;
25 | private JPanel toPanel;
26 |
27 | private JPanel centerPanel;
28 | private JPanel textPanel;
29 | private JPanel buttonPanel;
30 | private JPanel sendPanel;
31 |
32 | private JLabel ipLabel = new JLabel("IP Address");
33 | private JLabel portLabel = new JLabel("Port");
34 | private JLabel logoLabel = new JLabel("SocketTest v 3.0", logo,
35 | JLabel.CENTER);
36 | private JTextField ipField = new JTextField("0.0.0.0",20);
37 | private JTextField portField = new JTextField("21",10);
38 | private JButton portButton = new JButton("Port");
39 | private JButton connectButton = new JButton("Start Listening");
40 |
41 | private JLabel convLabel = new JLabel("Conversation with Client");
42 | private Border connectedBorder = BorderFactory.createTitledBorder(new EtchedBorder(),"Connected Client : < NONE >");
43 | private JTextArea messagesField = new JTextArea();
44 |
45 | private JLabel sendLabel = new JLabel("Message");
46 | private JTextField sendField = new JTextField();
47 |
48 | private JButton sendButton = new JButton("Send");
49 | private JButton disconnectButton = new JButton("Disconnect");
50 | private JButton saveButton = new JButton("Save");
51 | private JButton clearButton = new JButton("Clear");
52 |
53 | private GridBagConstraints gbc = new GridBagConstraints();
54 |
55 | private Socket socket;
56 | private ServerSocket server;
57 | private SocketServer socketServer;
58 | private PrintWriter out;
59 |
60 | protected final JFrame parent;
61 |
62 | public SocketTestServer(final JFrame parent) {
63 | //Container cp = getContentPane();
64 | this.parent = parent;
65 | Container cp = this;
66 |
67 | topPanel = new JPanel();
68 | toPanel = new JPanel();
69 | toPanel.setLayout(new GridBagLayout());
70 | gbc.insets = new Insets( 2, 2, 2, 2 );
71 | gbc.weighty = 0.0;
72 | gbc.weightx = 0.0;
73 | gbc.gridx = 0;
74 | gbc.gridy = 0;
75 | gbc.gridheight = 1;
76 | gbc.gridwidth = 1;
77 | gbc.anchor = GridBagConstraints.EAST;
78 | gbc.fill = GridBagConstraints.NONE;
79 | toPanel.add(ipLabel, gbc);
80 |
81 | gbc.weightx = 1.0; //streach
82 | gbc.gridx = 1;
83 | gbc.gridwidth = 3;
84 | gbc.fill = GridBagConstraints.HORIZONTAL;
85 | ActionListener ipListener = new ActionListener() {
86 | public void actionPerformed(ActionEvent e) {
87 | portField.requestFocus();
88 | }
89 | };
90 | ipField.addActionListener(ipListener);
91 | toPanel.add(ipField, gbc);
92 |
93 | gbc.weightx = 0.0;
94 | gbc.gridy = 1;
95 | gbc.gridx = 0;
96 | gbc.gridwidth = 1;
97 | gbc.fill = GridBagConstraints.NONE;
98 | toPanel.add(portLabel, gbc);
99 |
100 | gbc.weightx = 1.0;
101 | gbc.gridy = 1;
102 | gbc.gridx = 1;
103 | gbc.gridwidth = 1;
104 | gbc.fill = GridBagConstraints.HORIZONTAL;
105 | ActionListener connectListener = new ActionListener() {
106 | public void actionPerformed(ActionEvent e) {
107 | connect();
108 | }
109 | };
110 | portField.addActionListener(connectListener);
111 | toPanel.add(portField, gbc);
112 |
113 | gbc.weightx = 0.0;
114 | gbc.gridy = 1;
115 | gbc.gridx = 2;
116 | gbc.gridwidth = 1;
117 | gbc.fill = GridBagConstraints.NONE;
118 | portButton.setMnemonic('P');
119 | portButton.setToolTipText("View Standard Ports");
120 | ActionListener portButtonListener = new ActionListener() {
121 | public void actionPerformed(ActionEvent e) {
122 | PortDialog dia = new PortDialog(parent, PortDialog.TCP);
123 | dia.show();
124 | }
125 | };
126 | portButton.addActionListener(portButtonListener);
127 | toPanel.add(portButton, gbc);
128 |
129 | gbc.weightx = 0.0;
130 | gbc.gridy = 1;
131 | gbc.gridx = 3;
132 | gbc.gridwidth = 1;
133 | gbc.fill = GridBagConstraints.NONE;
134 | connectButton.setMnemonic('S');
135 | connectButton.setToolTipText("Start Listening");
136 | connectButton.addActionListener(connectListener);
137 | toPanel.add(connectButton, gbc);
138 |
139 | toPanel.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(),"Listen On"));
140 | topPanel.setLayout(new BorderLayout(10,0));
141 | topPanel.add(toPanel);
142 | logoLabel.setVerticalTextPosition(JLabel.BOTTOM);
143 | logoLabel.setHorizontalTextPosition(JLabel.CENTER);
144 | topPanel.add(logoLabel,BorderLayout.EAST);
145 | topPanel.setBorder(BorderFactory.createEmptyBorder(10,10,5,10));
146 |
147 |
148 | textPanel = new JPanel();
149 | textPanel.setLayout(new BorderLayout(0,5));
150 | textPanel.add(convLabel,BorderLayout.NORTH);
151 | messagesField.setEditable(false);
152 | JScrollPane jsp = new JScrollPane(messagesField);
153 | textPanel.add(jsp);
154 | textPanel.setBorder(BorderFactory.createEmptyBorder(3,3,0,3));
155 |
156 | sendPanel = new JPanel();
157 | sendPanel.setLayout(new GridBagLayout());
158 | gbc.weighty = 0.0;
159 | gbc.weightx = 0.0;
160 | gbc.gridx = 0;
161 | gbc.gridy = 0;
162 | gbc.gridheight = 1;
163 | gbc.gridwidth = 1;
164 | gbc.anchor = GridBagConstraints.EAST;
165 | gbc.fill = GridBagConstraints.NONE;
166 | sendPanel.add(sendLabel, gbc);
167 | gbc.gridx = 1;
168 | gbc.weightx = 1.0;
169 | gbc.fill = GridBagConstraints.HORIZONTAL;
170 | sendField.setEditable(false);
171 | sendPanel.add(sendField, gbc);
172 | gbc.gridx = 2;
173 | gbc.weightx = 0.0;
174 | gbc.fill = GridBagConstraints.NONE;
175 | sendButton.setEnabled(false);
176 | sendButton.setToolTipText("Send text to client");
177 | ActionListener sendListener = new ActionListener() {
178 | public void actionPerformed(ActionEvent e) {
179 | String msg = sendField.getText();
180 | if(!msg.equals(""))
181 | sendMessage(msg);
182 | else {
183 | int value = JOptionPane.showConfirmDialog(
184 | SocketTestServer.this, "Send Blank Line ?",
185 | "Send Data To Client",
186 | JOptionPane.YES_NO_OPTION);
187 | if (value == JOptionPane.YES_OPTION)
188 | sendMessage(msg);
189 | }
190 | }
191 | };
192 | sendButton.addActionListener(sendListener);
193 | sendField.addActionListener(sendListener);
194 | sendPanel.add(sendButton, gbc);
195 | ActionListener disconnectListener = new ActionListener() {
196 | public void actionPerformed(ActionEvent e) {
197 | disconnect();
198 | }
199 | };
200 | gbc.gridx = 3;
201 | disconnectButton.addActionListener(disconnectListener);
202 | disconnectButton.setEnabled(false);
203 | sendPanel.add(disconnectButton, gbc);
204 |
205 | sendPanel.setBorder(
206 | new CompoundBorder(
207 | BorderFactory.createEmptyBorder(0,0,0,3),
208 | BorderFactory.createTitledBorder("Send")));
209 |
210 | buttonPanel = new JPanel();
211 | buttonPanel.setLayout(new GridBagLayout());
212 | gbc.weighty = 0.0;
213 | gbc.weightx = 1.0;
214 | gbc.gridx = 0;
215 | gbc.gridy = 0;
216 | gbc.gridheight = 2;
217 | gbc.gridwidth = 1;
218 | gbc.anchor = GridBagConstraints.EAST;
219 | gbc.fill = GridBagConstraints.BOTH;
220 | buttonPanel.add(sendPanel, gbc);
221 | gbc.weighty = 0.0;
222 | gbc.weightx = 0.0;
223 | gbc.gridx = 1;
224 | gbc.gridheight = 1;
225 | gbc.fill = GridBagConstraints.HORIZONTAL;
226 | saveButton.setToolTipText("Save conversation with client to a file");
227 | saveButton.setMnemonic('S');
228 | ActionListener saveListener = new ActionListener() {
229 | public void actionPerformed(ActionEvent e) {
230 | String text = messagesField.getText();
231 | if(text.equals("")) {
232 | error("Nothing to save","Save to file");
233 | return;
234 | }
235 | String fileName="";
236 | JFileChooser chooser = new JFileChooser();
237 | chooser.setCurrentDirectory(new File("."));
238 | int returnVal = chooser.showSaveDialog(SocketTestServer.this);
239 | if(returnVal == JFileChooser.APPROVE_OPTION) {
240 | fileName=chooser.getSelectedFile().getAbsolutePath();
241 | try {
242 | Util.writeFile(fileName,text);
243 | } catch (Exception ioe) {
244 | JOptionPane.showMessageDialog(SocketTestServer.this,
245 | ""+ioe.getMessage(),
246 | "Error saving to file..",
247 | JOptionPane.ERROR_MESSAGE);
248 | }
249 | }
250 | }
251 | };
252 | saveButton.addActionListener(saveListener);
253 | buttonPanel.add(saveButton, gbc);
254 | gbc.gridy = 1;
255 | clearButton.setToolTipText("Clear conversation with client");
256 | clearButton.setMnemonic('C');
257 | ActionListener clearListener = new ActionListener() {
258 | public void actionPerformed(ActionEvent e) {
259 | messagesField.setText("");
260 | }
261 | };
262 | clearButton.addActionListener(clearListener);
263 | buttonPanel.add(clearButton, gbc);
264 | buttonPanel.setBorder(BorderFactory.createEmptyBorder(3,0,0,3));
265 |
266 | centerPanel = new JPanel();
267 | centerPanel.setLayout(new BorderLayout(0,10));
268 | centerPanel.add(buttonPanel,BorderLayout.SOUTH);
269 | centerPanel.add(textPanel,BorderLayout.CENTER);
270 |
271 | CompoundBorder cb=new CompoundBorder(
272 | BorderFactory.createEmptyBorder(5,10,10,10),
273 | connectedBorder);
274 | centerPanel.setBorder(cb);
275 |
276 | cp.setLayout(new BorderLayout(10,0));
277 | cp.add(topPanel,BorderLayout.NORTH);
278 | cp.add(centerPanel,BorderLayout.CENTER);
279 | }
280 |
281 | /*
282 | public static void main(String args[]) {
283 | SocketTestServer stServer=new SocketTestServer();
284 | stServer.setTitle("SocketTest Server");
285 | stServer.setSize(500,400);
286 | Util.centerWindow(stServer);
287 | stServer.setDefaultCloseOperation(EXIT_ON_CLOSE);
288 | stServer.setIconImage(stServer.logo.getImage());
289 | stServer.setVisible(true);
290 | }
291 | */
292 |
293 | /////////////////////
294 | //action & helper methods
295 | /////////////////////
296 | private void connect() {
297 | if(server!=null) {
298 | stop();
299 | return;
300 | }
301 | String ip=ipField.getText();
302 | String port=portField.getText();
303 | if(ip==null || ip.equals("")) {
304 | JOptionPane.showMessageDialog(SocketTestServer.this,
305 | "No IP Address. Please enter IP Address",
306 | "Error connecting", JOptionPane.ERROR_MESSAGE);
307 | ipField.requestFocus();
308 | ipField.selectAll();
309 | return;
310 | }
311 | if(port==null || port.equals("")) {
312 | JOptionPane.showMessageDialog(SocketTestServer.this,
313 | "No Port number. Please enter Port number",
314 | "Error connecting", JOptionPane.ERROR_MESSAGE);
315 | portField.requestFocus();
316 | portField.selectAll();
317 | return;
318 | }
319 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
320 | if(!Util.checkHost(ip)) {
321 | JOptionPane.showMessageDialog(SocketTestServer.this,
322 | "Bad IP Address",
323 | "Error connecting", JOptionPane.ERROR_MESSAGE);
324 | ipField.requestFocus();
325 | ipField.selectAll();
326 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
327 | return;
328 | }
329 | int portNo = 0;
330 | try {
331 | portNo=Integer.parseInt(port);
332 | } catch (Exception e) {
333 | JOptionPane.showMessageDialog(SocketTestServer.this,
334 | "Bad Port number. Please enter Port number",
335 | "Error connecting", JOptionPane.ERROR_MESSAGE);
336 | portField.requestFocus();
337 | portField.selectAll();
338 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
339 | return;
340 | }
341 | try {
342 | InetAddress bindAddr=null;
343 | if(!ip.equals("0.0.0.0"))
344 | bindAddr = InetAddress.getByName(ip);
345 | else
346 | bindAddr = null;
347 | server = new ServerSocket(portNo,1,bindAddr);
348 |
349 | ipField.setEditable(false);
350 | portField.setEditable(false);
351 |
352 | connectButton.setText("Stop Listening");
353 | connectButton.setMnemonic('S');
354 | connectButton.setToolTipText("Stop Listening");
355 | //sendButton.setEnabled(true);
356 | //sendField.setEditable(true);
357 | } catch (Exception e) {
358 | error(e.getMessage(), "Starting Server at "+portNo);
359 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
360 | return;
361 | }
362 | //changeBorder(" "+socket.getInetAddress().getHostName()+
363 | // " ["+socket.getInetAddress().getHostAddress()+"] ");
364 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
365 | messagesField.setText("> Server Started on Port: "+portNo+NEW_LINE);
366 | append("> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
367 | socketServer=SocketServer.handle(this,server);
368 | //sendField.requestFocus();
369 | }
370 | //disconnect a client
371 | public synchronized void disconnect() {
372 | try {
373 | socketServer.setDesonnected(true);
374 | } catch (Exception e) {}
375 | }
376 |
377 | public synchronized void stop() {
378 | try {
379 | disconnect(); //close any client
380 | socketServer.setStop(true);
381 | } catch (Exception e) {}
382 | server=null;
383 | ipField.setEditable(true);
384 | portField.setEditable(true);
385 | connectButton.setText("Start Listening");
386 | connectButton.setMnemonic('S');
387 | connectButton.setToolTipText("Start Listening");
388 | append("> Server stopped");
389 | append("> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
390 | }
391 |
392 | public synchronized void setClientSocket(Socket s) {
393 |
394 | if(s==null) {
395 | out=null;
396 | socket = null;
397 | changeBorder(null);
398 | sendButton.setEnabled(false);
399 | sendField.setEditable(false);
400 | disconnectButton.setEnabled(false);
401 | } else {
402 | socket = s;
403 | changeBorder(" "+socket.getInetAddress().getHostName()+
404 | " ["+socket.getInetAddress().getHostAddress()+"] ");
405 | sendButton.setEnabled(true);
406 | sendField.setEditable(true);
407 | disconnectButton.setEnabled(true);
408 | }
409 | }
410 |
411 | public void error(String error) {
412 | if(error==null || error.equals(""))
413 | return;
414 | JOptionPane.showMessageDialog(SocketTestServer.this,
415 | error, "Error", JOptionPane.ERROR_MESSAGE);
416 | }
417 |
418 | public void error(String error, String heading) {
419 | if(error==null || error.equals(""))
420 | return;
421 | JOptionPane.showMessageDialog(SocketTestServer.this,
422 | error, heading, JOptionPane.ERROR_MESSAGE);
423 | }
424 |
425 | public void append(String msg) {
426 | messagesField.append(msg+NEW_LINE);
427 | messagesField.setCaretPosition(messagesField.getText().length());
428 | }
429 |
430 | public void appendnoNewLine(String msg) {
431 | messagesField.append(msg);
432 | messagesField.setCaretPosition(messagesField.getText().length());
433 | }
434 |
435 | public void sendMessage(String s) {
436 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
437 | try {
438 | if(out==null) {
439 | out = new PrintWriter(new BufferedWriter(
440 | new OutputStreamWriter(socket.getOutputStream())), true);
441 | }
442 | append("S: "+s);
443 | out.print(s+NEW_LINE);
444 | out.flush();
445 | sendField.setText("");
446 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
447 | } catch (Exception e) {
448 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
449 | JOptionPane.showMessageDialog(SocketTestServer.this,
450 | e.getMessage(),"Error Sending Message",
451 | JOptionPane.ERROR_MESSAGE);
452 | disconnect();
453 | }
454 | }
455 |
456 | private void changeBorder(String ip) {
457 | if(ip==null || ip.equals(""))
458 | connectedBorder = BorderFactory.createTitledBorder(
459 | new EtchedBorder(), "Connected Client : < NONE >");
460 | else
461 | connectedBorder = BorderFactory.createTitledBorder(
462 | new EtchedBorder(), "Connected Client : < "+ip+" >");
463 | CompoundBorder cb=new CompoundBorder(
464 | BorderFactory.createEmptyBorder(5,10,10,10),
465 | connectedBorder);
466 | centerPanel.setBorder(cb);
467 | invalidate();
468 | repaint();
469 | }
470 |
471 | public void setUpConfiguration(String ip, String port) {
472 | ipField.setText(ip);
473 | portField.setText(port);
474 | }
475 |
476 | }
477 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/swing/SocketTestUdp.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest.swing;
2 |
3 | import java.awt.*;
4 | import java.awt.event.*;
5 | import javax.swing.*;
6 | import javax.swing.event.*;
7 | import javax.swing.border.*;
8 |
9 | import java.net.*;
10 | import java.io.*;
11 |
12 | import net.sf.sockettest.*;
13 |
14 | /**
15 | * @author Akshathkumar Shetty
16 | */
17 | public class SocketTestUdp extends JPanel implements NetService/*JFrame*/ {
18 | private final String NEW_LINE = "\r\n";
19 | private ClassLoader cl = getClass().getClassLoader();
20 | public ImageIcon logo = new ImageIcon(
21 | cl.getResource("icons/logo.gif"));
22 |
23 | private JPanel northPanel;
24 | private JPanel serverPanel;
25 |
26 | private JPanel convPanel;
27 |
28 | private JPanel clientPanel;
29 | private JPanel buttonPanel;
30 |
31 | private JLabel ipLabel1 = new JLabel("IP Address");
32 | private JLabel portLabel1 = new JLabel("Port");
33 | private JLabel logoLabel = new JLabel("SocketTest v 3.0", logo,
34 | JLabel.CENTER);
35 |
36 | private JTextField ipField1 = new JTextField("0.0.0.0", 20);
37 | private JTextField portField1 = new JTextField("21", 5);
38 | private JButton portButton1 = new JButton("Port");
39 | private JButton connectButton = new JButton("Start Listening");
40 |
41 | private Border convBorder = BorderFactory.createTitledBorder(new EtchedBorder(), "Conversation");
42 | private JTextArea messagesField = new JTextArea();
43 |
44 | private JLabel ipLabel2 = new JLabel("IP Address");
45 | private JLabel portLabel2 = new JLabel("Port");
46 | private JTextField ipField2 = new JTextField("127.0.0.1");
47 | private JTextField portField2 = new JTextField("21", 5);
48 | private JButton portButton2 = new JButton("Port");
49 | private JLabel sendLabel = new JLabel("Message");
50 | private JTextField sendField = new JTextField();
51 | private JButton sendButton = new JButton("Send");
52 |
53 | private JButton saveButton = new JButton("Save");
54 | private JButton clearButton = new JButton("Clear");
55 |
56 | private GridBagConstraints gbc = new GridBagConstraints();
57 |
58 | private DatagramSocket server, client;
59 | private DatagramPacket packet;
60 | private UdpServer udpServer;
61 | private DatagramPacket pack;
62 | private byte buffer[];
63 |
64 | protected final JFrame parent;
65 |
66 | public SocketTestUdp(final JFrame parent) {
67 | //Container cp = getContentPane();
68 | this.parent = parent;
69 | Container cp = this;
70 |
71 | northPanel = new JPanel();
72 | serverPanel = new JPanel();
73 | serverPanel.setLayout(new GridBagLayout());
74 | gbc.insets = new Insets(2, 2, 2, 2);
75 | gbc.weighty = 0.0;
76 | gbc.weightx = 0.0;
77 | gbc.gridx = 0;
78 | gbc.gridy = 0;
79 | gbc.gridheight = 1;
80 | gbc.gridwidth = 1;
81 | gbc.anchor = GridBagConstraints.EAST;
82 | gbc.fill = GridBagConstraints.NONE;
83 | serverPanel.add(ipLabel1, gbc);
84 |
85 | gbc.weightx = 1.0; //streach
86 | gbc.gridx = 1;
87 | gbc.gridwidth = 3;
88 | gbc.fill = GridBagConstraints.HORIZONTAL;
89 | ActionListener ipListener1 = new ActionListener() {
90 | public void actionPerformed(ActionEvent e) {
91 | portField1.requestFocus();
92 | }
93 | };
94 | ipField1.addActionListener(ipListener1);
95 | serverPanel.add(ipField1, gbc);
96 |
97 | gbc.weightx = 0.0;
98 | gbc.gridy = 1;
99 | gbc.gridx = 0;
100 | gbc.gridwidth = 1;
101 | gbc.fill = GridBagConstraints.NONE;
102 | serverPanel.add(portLabel1, gbc);
103 |
104 | gbc.weightx = 1.0;
105 | gbc.gridy = 1;
106 | gbc.gridx = 1;
107 | gbc.gridwidth = 1;
108 | gbc.fill = GridBagConstraints.HORIZONTAL;
109 | ActionListener connectListener = new ActionListener() {
110 | public void actionPerformed(ActionEvent e) {
111 | listen();
112 | }
113 | };
114 | portField1.addActionListener(connectListener);
115 | serverPanel.add(portField1, gbc);
116 |
117 | gbc.weightx = 0.0;
118 | gbc.gridy = 1;
119 | gbc.gridx = 2;
120 | gbc.gridwidth = 1;
121 | gbc.fill = GridBagConstraints.NONE;
122 | portButton1.setMnemonic('P');
123 | portButton1.setToolTipText("View Standard Ports");
124 | ActionListener portButtonListener = new ActionListener() {
125 | public void actionPerformed(ActionEvent e) {
126 | PortDialog dia = new PortDialog(parent, PortDialog.UDP);
127 | dia.show();
128 | }
129 | };
130 | portButton1.addActionListener(portButtonListener);
131 | serverPanel.add(portButton1, gbc);
132 |
133 | gbc.weightx = 0.0;
134 | gbc.gridy = 1;
135 | gbc.gridx = 3;
136 | gbc.gridwidth = 1;
137 | gbc.fill = GridBagConstraints.NONE;
138 | connectButton.setMnemonic('S');
139 | connectButton.setToolTipText("Start Listening");
140 | connectButton.addActionListener(connectListener);
141 | serverPanel.add(connectButton, gbc);
142 |
143 | serverPanel.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(), "Server"));
144 | northPanel.setLayout(new BorderLayout(10, 0));
145 | northPanel.add(serverPanel);
146 | logoLabel.setVerticalTextPosition(JLabel.BOTTOM);
147 | logoLabel.setHorizontalTextPosition(JLabel.CENTER);
148 | northPanel.add(logoLabel, BorderLayout.EAST);
149 | northPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 5, 10));
150 |
151 | convPanel = new JPanel();
152 | convPanel.setLayout(new BorderLayout(0, 5));
153 | messagesField.setEditable(false);
154 | JScrollPane jsp = new JScrollPane(messagesField);
155 | convPanel.add(jsp);
156 | convPanel.setBorder(new CompoundBorder(
157 | BorderFactory.createEmptyBorder(5, 10, 5, 10),
158 | convBorder));
159 |
160 | clientPanel = new JPanel();
161 | clientPanel.setLayout(new GridBagLayout());
162 | gbc.weighty = 0.0;
163 | gbc.weightx = 0.0;
164 | gbc.gridx = 0;
165 | gbc.gridy = 0;
166 | gbc.gridheight = 1;
167 | gbc.gridwidth = 1;
168 | gbc.anchor = GridBagConstraints.EAST;
169 | gbc.fill = GridBagConstraints.NONE;
170 | clientPanel.add(ipLabel2, gbc);
171 |
172 | gbc.weightx = 1.0; //streach
173 | gbc.gridx = 1;
174 | gbc.gridwidth = 1;
175 | gbc.fill = GridBagConstraints.HORIZONTAL;
176 | ActionListener ipListener2 = new ActionListener() {
177 | public void actionPerformed(ActionEvent e) {
178 | portField2.requestFocus();
179 | }
180 | };
181 | ipField2.addActionListener(ipListener2);
182 | clientPanel.add(ipField2, gbc);
183 |
184 | gbc.weightx = 0.0;
185 | gbc.gridy = 0;
186 | gbc.gridx = 2;
187 | gbc.fill = GridBagConstraints.NONE;
188 | clientPanel.add(portLabel2, gbc);
189 |
190 | gbc.weightx = 0.0;
191 | gbc.gridy = 0;
192 | gbc.gridx = 3;
193 | gbc.gridwidth = 1;
194 | gbc.fill = GridBagConstraints.NONE;
195 | ActionListener portListener2 = new ActionListener() {
196 | public void actionPerformed(ActionEvent e) {
197 | sendField.requestFocus();
198 | }
199 | };
200 | portField2.addActionListener(portListener2);
201 | clientPanel.add(portField2, gbc);
202 |
203 | gbc.weightx = 0.0;
204 | gbc.gridy = 0;
205 | gbc.gridx = 4;
206 | gbc.gridwidth = 1;
207 | gbc.fill = GridBagConstraints.HORIZONTAL;
208 | portButton2.setMnemonic('P');
209 | portButton2.setToolTipText("View Standard Ports");
210 | portButton2.addActionListener(portButtonListener);
211 | clientPanel.add(portButton2, gbc);
212 |
213 | gbc.weightx = 0.0;
214 | gbc.gridy = 1;
215 | gbc.gridx = 0;
216 | gbc.gridwidth = 1;
217 | gbc.fill = GridBagConstraints.NONE;
218 | clientPanel.add(sendLabel, gbc);
219 |
220 | gbc.weightx = 1.0;
221 | gbc.gridy = 1;
222 | gbc.gridx = 1;
223 | gbc.gridwidth = 3;
224 | gbc.fill = GridBagConstraints.HORIZONTAL;
225 | ActionListener sendListener = new ActionListener() {
226 | public void actionPerformed(ActionEvent e) {
227 | String msg = sendField.getText();
228 | if (!msg.equals(""))
229 | sendMessage(msg);
230 | else {
231 | int value = JOptionPane.showConfirmDialog(
232 | SocketTestUdp.this, "Send Blank Line ?",
233 | "Send Data To Server",
234 | JOptionPane.YES_NO_OPTION);
235 | if (value == JOptionPane.YES_OPTION)
236 | sendMessage(msg);
237 | }
238 | }
239 | };
240 | sendField.addActionListener(sendListener);
241 | clientPanel.add(sendField, gbc);
242 |
243 | gbc.weightx = 0.0;
244 | gbc.gridy = 1;
245 | gbc.gridx = 4;
246 | gbc.gridwidth = 1;
247 | gbc.fill = GridBagConstraints.HORIZONTAL;
248 | sendButton.addActionListener(sendListener);
249 | clientPanel.add(sendButton, gbc);
250 | clientPanel.setBorder(
251 | new CompoundBorder(
252 | BorderFactory.createEmptyBorder(0, 0, 0, 3),
253 | BorderFactory.createTitledBorder("Client")));
254 |
255 | buttonPanel = new JPanel();
256 | buttonPanel.setLayout(new GridBagLayout());
257 | gbc.weighty = 0.0;
258 | gbc.weightx = 1.0;
259 | gbc.gridx = 0;
260 | gbc.gridy = 0;
261 | gbc.gridheight = 2;
262 | gbc.gridwidth = 1;
263 | gbc.anchor = GridBagConstraints.EAST;
264 | gbc.fill = GridBagConstraints.BOTH;
265 | buttonPanel.add(clientPanel, gbc);
266 | gbc.weighty = 0.0;
267 | gbc.weightx = 0.0;
268 | gbc.gridx = 1;
269 | gbc.gridheight = 1;
270 | gbc.anchor = GridBagConstraints.NORTHEAST;
271 | gbc.fill = GridBagConstraints.HORIZONTAL;
272 | saveButton.setToolTipText("Save conversation with client to a file");
273 | saveButton.setMnemonic('S');
274 | ActionListener saveListener = new ActionListener() {
275 | public void actionPerformed(ActionEvent e) {
276 | String text = messagesField.getText();
277 | if (text.equals("")) {
278 | error("Nothing to save", "Save to file");
279 | return;
280 | }
281 | String fileName = "";
282 | JFileChooser chooser = new JFileChooser();
283 | chooser.setCurrentDirectory(new File("."));
284 | int returnVal = chooser.showSaveDialog(SocketTestUdp.this);
285 | if (returnVal == JFileChooser.APPROVE_OPTION) {
286 | fileName = chooser.getSelectedFile().getAbsolutePath();
287 | try {
288 | Util.writeFile(fileName, text);
289 | } catch (Exception ioe) {
290 | JOptionPane.showMessageDialog(SocketTestUdp.this,
291 | "" + ioe.getMessage(),
292 | "Error saving to file..",
293 | JOptionPane.ERROR_MESSAGE);
294 | }
295 | }//end of if
296 | }
297 | };
298 | saveButton.addActionListener(saveListener);
299 | buttonPanel.add(saveButton, gbc);
300 | gbc.gridy = 1;
301 | clearButton.setToolTipText("Clear conversation with client");
302 | clearButton.setMnemonic('C');
303 | ActionListener clearListener = new ActionListener() {
304 | public void actionPerformed(ActionEvent e) {
305 | messagesField.setText("");
306 | }
307 | };
308 | clearButton.addActionListener(clearListener);
309 | buttonPanel.add(clearButton, gbc);
310 | buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
311 |
312 | cp.setLayout(new BorderLayout(10, 0));
313 | cp.add(northPanel, BorderLayout.NORTH);
314 | cp.add(convPanel, BorderLayout.CENTER);
315 | cp.add(buttonPanel, BorderLayout.SOUTH);
316 |
317 | }
318 |
319 | /////////////////////
320 | //action & helper methods
321 | /////////////////////
322 | private void listen() {
323 | if (server != null) {
324 | stop();
325 | return;
326 | }
327 | String ip = ipField1.getText();
328 | String port = portField1.getText();
329 | if (ip == null || ip.equals("")) {
330 | JOptionPane.showMessageDialog(SocketTestUdp.this,
331 | "No IP Address. Please enter IP Address",
332 | "Error connecting", JOptionPane.ERROR_MESSAGE);
333 | ipField1.requestFocus();
334 | ipField1.selectAll();
335 | return;
336 | }
337 | if (port == null || port.equals("")) {
338 | JOptionPane.showMessageDialog(SocketTestUdp.this,
339 | "No Port number. Please enter Port number",
340 | "Error connecting", JOptionPane.ERROR_MESSAGE);
341 | portField1.requestFocus();
342 | portField1.selectAll();
343 | return;
344 | }
345 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
346 | if (!Util.checkHost(ip)) {
347 | JOptionPane.showMessageDialog(SocketTestUdp.this,
348 | "Bad IP Address",
349 | "Error connecting", JOptionPane.ERROR_MESSAGE);
350 | ipField1.requestFocus();
351 | ipField1.selectAll();
352 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
353 | return;
354 | }
355 | int portNo = 0;
356 | try {
357 | portNo = Integer.parseInt(port);
358 | } catch (Exception e) {
359 | JOptionPane.showMessageDialog(SocketTestUdp.this,
360 | "Bad Port number. Please enter Port number",
361 | "Error connecting", JOptionPane.ERROR_MESSAGE);
362 | portField1.requestFocus();
363 | portField1.selectAll();
364 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
365 | return;
366 | }
367 |
368 | boolean multicase = false;
369 | try {
370 | InetAddress bindAddr = null;
371 |
372 | int i = ip.indexOf(".");
373 | int startBit = 0;
374 | if (i != -1) {
375 | startBit = Integer.parseInt(ip.substring(0, i));
376 | }
377 |
378 | //if 224.x.x.x - 239.x.x.x - multi cast
379 | if (startBit >= 224 && startBit <= 239) {
380 | MulticastSocket socket = new MulticastSocket(portNo); // must bind receive side
381 | socket.joinGroup(InetAddress.getByName(ip));
382 | server = socket;
383 | multicase = true;
384 | } else {
385 | if (!ip.equals("0.0.0.0")) {
386 | bindAddr = InetAddress.getByName(ip);
387 | server = new DatagramSocket(portNo, bindAddr);
388 | } else {
389 | bindAddr = null;
390 | server = new DatagramSocket(portNo);
391 | }
392 | }
393 |
394 | ipField1.setEditable(false);
395 | portField1.setEditable(false);
396 |
397 | connectButton.setText("Stop Listening");
398 | connectButton.setMnemonic('S');
399 | connectButton.setToolTipText("Stop Listening");
400 | } catch (Exception e) {
401 | error(e.getMessage(), "Starting Server at " + portNo);
402 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
403 | return;
404 | }
405 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
406 | if (multicase) {
407 | messagesField.setText("> MultiCase Server Joined on Port : " + portNo + NEW_LINE);
408 | } else {
409 | messagesField.setText("> Server Started on Port : " + portNo + NEW_LINE);
410 | }
411 | append("> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
412 | udpServer = UdpServer.handle(this, server);
413 | }
414 |
415 | public synchronized void stop() {
416 | try {
417 | udpServer.setStop(true);
418 | } catch (Exception e) {
419 | }
420 | server = null;
421 |
422 | ipField1.setEditable(true);
423 | portField1.setEditable(true);
424 |
425 | connectButton.setText("Start Listening");
426 | connectButton.setMnemonic('S');
427 | connectButton.setToolTipText("Start Listening");
428 | append("> Server stopped");
429 | append("> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
430 | }
431 |
432 | public void sendMessage(String s) {
433 | try {
434 | String ip = ipField2.getText();
435 | String port = portField2.getText();
436 | if (ip == null || ip.equals("")) {
437 | JOptionPane.showMessageDialog(SocketTestUdp.this,
438 | "No IP Address. Please enter IP Address",
439 | "Error connecting", JOptionPane.ERROR_MESSAGE);
440 | ipField2.requestFocus();
441 | ipField2.selectAll();
442 | return;
443 | }
444 | if (port == null || port.equals("")) {
445 | JOptionPane.showMessageDialog(SocketTestUdp.this,
446 | "No Port number. Please enter Port number",
447 | "Error connecting", JOptionPane.ERROR_MESSAGE);
448 | portField2.requestFocus();
449 | portField2.selectAll();
450 | return;
451 | }
452 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
453 | if (!Util.checkHost(ip)) {
454 | JOptionPane.showMessageDialog(SocketTestUdp.this,
455 | "Bad IP Address",
456 | "Error connecting", JOptionPane.ERROR_MESSAGE);
457 | ipField2.requestFocus();
458 | ipField2.selectAll();
459 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
460 | return;
461 | }
462 | int portNo = 0;
463 | try {
464 | portNo = Integer.parseInt(port);
465 | } catch (Exception e) {
466 | JOptionPane.showMessageDialog(SocketTestUdp.this,
467 | "Bad Port number. Please enter Port number",
468 | "Error connecting", JOptionPane.ERROR_MESSAGE);
469 | portField2.requestFocus();
470 | portField2.selectAll();
471 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
472 | return;
473 | }
474 |
475 | InetAddress toAddr = null;
476 | toAddr = InetAddress.getByName(ip);
477 |
478 | if (client == null) {
479 | client = new DatagramSocket();
480 | UdpServer.handleClient(this, client); //listen for its response
481 | }
482 | buffer = s.getBytes();
483 | pack = new DatagramPacket(buffer, buffer.length, toAddr, portNo);
484 | append("S[" + toAddr.getHostAddress() + ":" + portNo + "]: " + s);
485 | client.send(pack);
486 | sendField.setText("");
487 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
488 | } catch (Exception e) {
489 | JOptionPane.showMessageDialog(SocketTestUdp.this,
490 | e.getMessage(), "Error Sending Message",
491 | JOptionPane.ERROR_MESSAGE);
492 | client = null;
493 | }
494 | }
495 |
496 | public void error(String error) {
497 | if (error == null || error.equals(""))
498 | return;
499 | JOptionPane.showMessageDialog(SocketTestUdp.this,
500 | error, "Error", JOptionPane.ERROR_MESSAGE);
501 | }
502 |
503 | public void error(String error, String heading) {
504 | if (error == null || error.equals(""))
505 | return;
506 | JOptionPane.showMessageDialog(SocketTestUdp.this,
507 | error, heading, JOptionPane.ERROR_MESSAGE);
508 | }
509 |
510 | public void append(String msg) {
511 | messagesField.append(msg + NEW_LINE);
512 | messagesField.setCaretPosition(messagesField.getText().length());
513 | }
514 |
515 |
516 | public void setUpConfiguration(String ip, String port) {
517 | ipField1.setText(ip);
518 | portField1.setText(port);
519 | ipField2.setText(ip);
520 | portField2.setText(port);
521 | }
522 |
523 | }
524 |
--------------------------------------------------------------------------------
/src/net/sf/sockettest/swing/SplashScreen.java:
--------------------------------------------------------------------------------
1 | package net.sf.sockettest.swing;
2 |
3 | import java.awt.*;
4 | import javax.swing.*;
5 | import javax.swing.border.*;
6 | import java.awt.event.*;
7 | import net.sf.sockettest.*;
8 |
9 | /**
10 | *
11 | * @author Akshathkumar Shetty
12 | */
13 | public class SplashScreen extends JWindow {
14 |
15 | protected ImageIcon logo;
16 | protected JLabel productName;
17 |
18 | public SplashScreen() {
19 | ClassLoader cl = getClass().getClassLoader();
20 | logo = new ImageIcon(cl.getResource("icons/logo.gif"));
21 | productName = new JLabel(""+
22 | "SocketTest v 3.0.0",logo,JLabel.CENTER);
23 | //productName.setBackground(Color.white);
24 | productName.setOpaque(true);
25 |
26 | productName.setBorder(BorderFactory.createCompoundBorder(
27 | BorderFactory.createEmptyBorder(10,10,10,10),
28 | BorderFactory.createLineBorder(Color.black) ));
29 | getContentPane().add(productName);
30 | Dimension dim=productName.getPreferredSize();
31 | dim.setSize(dim.getWidth()+10,dim.getHeight()+10);
32 | setSize(dim);
33 | Util.centerWindow(this);
34 | setVisible(true);
35 | }
36 |
37 | public void kill() {
38 | dispose();
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/readme.txt:
--------------------------------------------------------------------------------
1 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 | Product : SocketTest v 3.0
3 | Licence : GNU Lesser General Public License
4 | Author : Akshathkumar Shetty
5 | Url : http://sockettest.sourceforge.net/
6 |
7 | A java tool for socket testing. It can create both TCP and UDP client or
8 | server. It can be used to test any server or client that uses TCP or UDP
9 | protocol to communicate.
10 |
11 | System Requirement: JDK 1.3 or above
12 |
13 |
14 | Other Libraries/Tools Used
15 | ~~~~~~~~~~~~~~~~~~~~~~~~~~
16 | SocketTest uses the following third party libraries and would like
17 | to thank all of them for making life easier for developers.
18 |
19 | Metouia Look And Feel :
20 | A free pluggable look and feel for java. License : GNU LGPL
21 | http://mlf.sourceforge.net
22 |
23 |
24 | License, Copyright
25 | ~~~~~~~~~~~~~~~~~~~~
26 | SocketTest - Test My Socket
27 | Copyright (C) 2003-2008 Akshathkumar Shetty
28 | Website : http://sockettest.sourceforge.net/
29 | Author : Akshathkumar Shetty
30 | Email : akshath@quickserver.org
31 |
32 | This library is free software; you can redistribute it and/or
33 | modify it under the terms of the GNU Lesser General Public
34 | License as published by the Free Software Foundation; either
35 | version 2.1 of the License, or (at your option) any later version.
36 |
37 | This library is distributed in the hope that it will be useful,
38 | but WITHOUT ANY WARRANTY; without even the implied warranty of
39 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40 | Lesser General Public License for more details.
41 |
42 | You should have received a copy of the GNU Lesser General Public
43 | License along with this library; if not, write to the Free Software
44 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--------------------------------------------------------------------------------
/src/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | java -jar ../dist/SocketTest.jar c:10.0.0.1:5555
--------------------------------------------------------------------------------
/src/tcpports.txt:
--------------------------------------------------------------------------------
1 | 21 ftp
2 | 22 ssh
3 | 23 telnet
4 | 25 smtp Send mails
5 | 42 nameserver
6 | 43 whois
7 | 53 dns-zone
8 | 63 whois++
9 | 70 gopher
10 | 79 finger
11 | 80 http Web Server
12 | 109 pop2
13 | 110 pop3
14 | 135 ntrpc/dce
15 | 137 netbios-ns
16 | 138 netbios-dgn
17 | 139 netbios
18 | 143 imap
19 | 389 ldap
20 | 386 netware-ip
21 | 443 https/ssl
22 | 515 printer
23 | 1024-1030 w2k rpc services
24 | 1080 socks
25 | 1745 winsock-proxy
26 | 5631 pcanywhere RAT s/w
27 | 5800 vnc RAT s/w
28 | 5900 vnc-java
29 | 8000 web
30 | 8001 web
31 | 8002 web
32 | 8080 web Web server like tomcat
33 | 12345 netbus trojan
34 | 54320 bo2k
35 | 65301 pcanywhere-def
36 |
--------------------------------------------------------------------------------
/src/udpports.txt:
--------------------------------------------------------------------------------
1 | 53 dns-lookup
2 | 63 whois++
3 | 70 gopher
4 | 135 ntrpc/dce
5 | 137 netbios-ns
6 | 138 netbios-dgn
7 | 161 snmp
8 | 515 printer
9 | 1024-1030 w2k rpc services
10 | 31337 backorifice
11 | 54321 bo2k
--------------------------------------------------------------------------------
/webstart/SocketTest.jnlp:
--------------------------------------------------------------------------------
1 |
2 |
3 | SocketTest
4 | Akshathkumar Shetty
5 |
6 | SocketTest
7 | Socket Test app.
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/webstart/logo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/akshath/SocketTest/d105d6433ff039b6399a04412c1e30867fb3a076/webstart/logo.gif
--------------------------------------------------------------------------------