Source code: com/act365/net/echo/EchoServer.java
1 /*
2 * JSocket Wrench
3 *
4 * Copyright (C) act365.com October 2003
5 *
6 * Web site: http://www.act365.com/wrench
7 * E-mail: developers@act365.com
8 *
9 * The JSocket Wrench library adds support for low-level Internet protocols
10 * to the Java programming language.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 * Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 package com.act365.net.echo ;
28
29 import com.act365.net.* ;
30
31 import java.io.*;
32 import java.net.*;
33
34 /**
35 EchoServer acts as the server for the TCP echo service.
36 Usage: <code>EchoServer -p protocol -l localhost localport</code>.
37 NB When a TCPJ protocol has been chosen, the server will be created with a
38 backlog of 1 (as against the standard Berkeley value of 5) because of a limitation
39 in the current TCPJ implementation.
40 <p><code>-p protocol</code> (optional) defines the socket protocol to be used.
41 By default, the JDK TCP implementation will be used. The alternatives are
42 TCPJ, RawTCP or RawTCPJ. (NB The RawTCP protocol will behave identically to TCP
43 because no <code>DatagramSocket</code> objects will be instantiated. However, a
44 <code>DatagramSocket</code> is always used by TCPJ, so the behaviour of TCPJ
45 and RawTCPJ will differ).
46 <p><code>-l localhost</code> (optional) should be specified if the protocol
47 has been set to RawTCPJ. The information will be used to construct the IP header.
48 <p><code>localport</code> is the port to be used by the echo server.
49 */
50
51 public class EchoServer extends Thread {
52
53 ServerSocket server = null ;
54
55 public static void main( String[] args ){
56
57 final String errortext = "EchoServer -p protocol -l localhost port";
58
59 if( args.length == 0 ){
60 System.err.println( errortext );
61 System.exit( 1 );
62 }
63
64 int i = -1 ,
65 port = 0 ;
66
67 String protocollabel = "" ,
68 localhost = null ;
69
70 try {
71 port = Integer.parseInt( args[ args.length - 1 ] );
72 } catch( NumberFormatException e ){
73 System.err.println("Invalid port number");
74 System.exit( 3 );
75 }
76
77 while( ++ i < args.length - 1 ){
78 if( args[ i ].equals("-p") && i < args.length - 2 ){
79 protocollabel = args[ ++ i ];
80 if( ! protocollabel.equalsIgnoreCase("TCP") &&
81 ! protocollabel.equalsIgnoreCase("TCPJ") &&
82 ! protocollabel.equalsIgnoreCase("RawTCP") &&
83 ! protocollabel.equalsIgnoreCase("RawTCPJ") ){
84 System.err.println("Unsupported protocol");
85 System.exit( 2 );
86 }
87 } else if( args[ i ].equals("-l") && i < args.length - 2 ){
88 localhost = args[ ++ i ];
89 } else {
90 System.err.println( errortext );
91 System.exit( 1 );
92 }
93 }
94
95 try {
96 SocketUtils.setProtocol( protocollabel );
97 } catch ( java.io.IOException e ) {
98 System.err.println("Unsupported protocol");
99 System.exit( 2 );
100 }
101
102 final int protocol = SocketUtils.getProtocol();
103
104 boolean includeheader = SocketUtils.includeHeader();
105
106 new SocketWrenchSession();
107
108 InetAddress localaddr = null ;
109
110 try {
111 if( localhost instanceof String ){
112 localaddr = InetAddress.getByName( localhost );
113 }
114 } catch( UnknownHostException e ){
115 System.err.println("Address " + e.getMessage() + " is unknown");
116 System.exit( 4 );
117 }
118
119 new EchoServer( port , localaddr );
120 }
121
122 EchoServer( int port , InetAddress localaddr ){
123
124 try {
125 if( localaddr instanceof InetAddress ){
126 server = new ServerSocket( port , 1 , localaddr );
127 } else {
128 server = new ServerSocket( port );
129 }
130 } catch( IOException e ){
131 System.err.println( e.getMessage() );
132 System.exit( 5 );
133 }
134
135 System.err.println("Server Socket: " + server.toString() );
136 System.err.println("Socket is connected to: " + server.getInetAddress() );
137 System.err.println("Local port: " + server.getLocalPort() );
138
139 start();
140 }
141
142 public void run() {
143 Socket socket ;
144 while( true ){
145 try {
146 socket = server.accept();
147 ( new EchoWorker( socket.getInputStream() , socket.getOutputStream() ) ).start();
148 } catch( IOException e ) {
149 System.err.println( e.getMessage() );
150 System.exit( 2 );
151 }
152 }
153 }
154
155 protected void finalize() {
156 if( server != null ){
157 try {
158 server.close();
159 } catch( IOException e ){
160 System.err.println( e.getMessage() );
161 System.exit( 3 );
162 }
163 }
164 }
165 }
166