forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPServerSocket.java
More file actions
98 lines (84 loc) · 3.22 KB
/
Copy pathTCPServerSocket.java
File metadata and controls
98 lines (84 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.bridge;
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
// Note: Make sure that classes like Util or JavaBridge are only accessed if something fails
// The default path should only load the Standalone and the Socket classes
class TCPServerSocket implements ISocketFactory {
static int TCP_PORT_BASE=9267; //try to find a free port in the range 9267, ..., 9366
private ServerSocket sock;
private int port;
boolean local;
public static ISocketFactory create(String name, int backlog) throws IOException {
int p;
boolean local = false;
if(name==null) throw new NullPointerException ("name");
if(name.startsWith("INET:")) name=name.substring(5);
else if(name.startsWith("INET_LOCAL:")) { local = true; name=name.substring(11); }
p=Integer.parseInt(name);
TCPServerSocket s = new TCPServerSocket(p, backlog, local);
return s;
}
private ServerSocket newServerSocket (int port, int backlog) throws java.io.IOException {
try {
if(local)
return new ServerSocket(port, backlog, InetAddress.getByName("127.0.0.1"));
} catch (java.net.UnknownHostException e) {/*cannot happen*/}
return new ServerSocket(port, backlog);
}
private void findFreePort(int start, int backlog) {
for (int port = start; port < start+100; port++) {
try {
this.sock = newServerSocket(port, backlog);
this.port = port;
return;
} catch (IOException e) {continue;}
}
}
private TCPServerSocket(int port, int backlog, boolean local) throws IOException {
this.local = local;
if(port==0) {
findFreePort(TCP_PORT_BASE, backlog);
} else {
this.sock = newServerSocket(port, backlog);
this.port = port;
}
}
public void close() throws IOException {
sock.close();
}
public Socket accept() throws IOException {
Socket s = sock.accept();
s.setTcpNoDelay(true);
return s;
}
public String getSocketName() {
return String.valueOf(port);
}
public String toString() {
return (local?"INET_LOCAL:":"INET:") +getSocketName();
}
}