View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2006 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  
21  package org.microemu.cldc.socket;
22  
23  import java.io.IOException;
24  import java.net.InetAddress;
25  import java.net.ServerSocket;
26  
27  import javax.microedition.io.StreamConnection;
28  
29  public class ServerSocketConnection implements
30  		javax.microedition.io.ServerSocketConnection {
31  	
32  	private ServerSocket serverSocket;
33  	
34  	public ServerSocketConnection(int port) throws IOException {
35  		serverSocket = new ServerSocket(port);
36  	}
37  
38  	public String getLocalAddress() throws IOException {
39  		InetAddress localHost = InetAddress.getLocalHost();
40  		return localHost.getHostAddress();
41  	}
42  
43  	public int getLocalPort() throws IOException {
44  		return serverSocket.getLocalPort();
45  	}
46  
47  	public StreamConnection acceptAndOpen() throws IOException {
48  		return new SocketConnection(serverSocket.accept());
49  	}
50  
51  	public void close() throws IOException {
52  		serverSocket.close();
53  	}
54  
55  }