本文整理汇总了Java中java.net.Socket.getPort方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.getPort方法的具体用法?Java Socket.getPort怎么用?Java Socket.getPort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.Socket
的用法示例。
在下文中一共展示了Socket.getPort方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: MMOConnection
import java.net.Socket; //导入方法依赖的package包/类
public MMOConnection(SelectorThread<T> selectorThread, Socket socket, SelectionKey key, boolean tcpNoDelay)
{
_selectorThread = selectorThread;
_socket = socket;
_address = socket.getInetAddress();
_readableByteChannel = socket.getChannel();
_writableByteChannel = socket.getChannel();
_port = socket.getPort();
_selectionKey = key;
_sendQueue = new NioNetStackList<>();
try
{
_socket.setTcpNoDelay(tcpNoDelay);
}
catch (SocketException e)
{
e.printStackTrace();
}
}
示例2: start
import java.net.Socket; //导入方法依赖的package包/类
/**
* Create a server with the specified port, listen backlog, and local IP
* address to bind to. The localIP argument can be used on a multi-homed
* host for a ServerSocket that will only accept connect requests to one of
* its addresses. If localIP is null, it will default accepting connections
* on any/all local addresses. The port must be between 0 and 65535,
* inclusive. <br>
* This methods blocks.
*/
public void start(final int port, final int backlog,
final InetAddress localIP) {
try {
ss = new ServerSocket(port, backlog, localIP);
final String address = ss.getInetAddress().getHostAddress();
final int localPort = ss.getLocalPort();
log.info("Starting SOCKS Proxy on: {}:{}", address, localPort);
while (true) {
final Socket s = ss.accept();
final String hostName = s.getInetAddress().getHostName();
final int port2 = s.getPort();
log.info("Accepted from:{}:{}", hostName, port2);
final ProxyServer ps = new ProxyServer(auth, s);
(new Thread(ps)).start();
}
} catch (final IOException ioe) {
ioe.printStackTrace();
} finally {
}
}
示例3: ControlChannel
import java.net.Socket; //导入方法依赖的package包/类
/**
* A remote peer connected to FDT
*
* @param s - the socket
* @throws Exception - if anything goes wrong in intialization
*/
public ControlChannel(Socket s, ControlChannelNotifier notifier) throws Exception {
try {
this.controlSocket = s;
this.remoteAddress = s.getInetAddress();
this.remotePort = s.getPort();
this.localPort = s.getLocalPort();
this.notifier = notifier;
initStreams();
controlSocket.setTcpNoDelay(true);
controlSocket.setSoTimeout(1000);
} catch (Throwable t) {
close("Cannot instantiate ControlChannel", t);
throw new Exception(t);
}
}
示例4: MMOConnection
import java.net.Socket; //导入方法依赖的package包/类
public MMOConnection(final SelectorThread<T> selectorThread, final Socket socket, final SelectionKey key)
{
_selectorThread = selectorThread;
_socket = socket;
_address = socket.getInetAddress();
_readableByteChannel = socket.getChannel();
_writableByteChannel = socket.getChannel();
_port = socket.getPort();
_selectionKey = key;
_sendQueue = new NioNetStackList<SendablePacket<T>>();
}
示例5: MinicraftServerThread
import java.net.Socket; //导入方法依赖的package包/类
public MinicraftServerThread(Socket socket, MinicraftServer serverInstance) {
super("MinicraftServerThread", socket);
this.serverInstance = serverInstance;
if(serverInstance.isFull()) {
sendError("server at max capacity.");
super.endConnection();
return;
}
client = new RemotePlayer(null, false, socket.getInetAddress(), socket.getPort());
// username is set later
packetTypesToKeep.addAll(InputType.tileUpdates);
packetTypesToKeep.addAll(InputType.entityUpdates);
gameTimers = new ArrayList<>();
Timer t = new Timer("ClientPing");
t.schedule((new MyTask() {
public void run() { MinicraftServerThread.this.ping(); }
}), 1000, PING_INTERVAL*1000);
gameTimers.add(t);
start();
}
示例6: chooseClientAlias
import java.net.Socket; //导入方法依赖的package包/类
@Override
public String chooseClientAlias(final String[] keyTypes, final Principal[] issuers, final Socket socket) {
final Key key = new Key(socket.getInetAddress().getHostName(), socket.getPort(), issuers);
final String alias = this.find(key);
if(alias != null) {
return alias;
}
final String s = super.chooseClientAlias(keyTypes, issuers, socket);
if(null == s) {
return null;
}
return this.save(key, s);
}
示例7: addNewUserConnection
import java.net.Socket; //导入方法依赖的package包/类
/**
* Add a new user connection. That is a new connection to the server
* that has not yet logged in as a player.
*
* @param socket The client {@code Socket} the connection arrives on.
* @exception IOException if the socket was already broken.
*/
public void addNewUserConnection(Socket socket) throws IOException {
final String name = socket.getInetAddress() + ":" + socket.getPort();
Connection c = new Connection(socket, FreeCol.SERVER_THREAD + name)
.setMessageHandler(this.userConnectionHandler);
getServer().addConnection(c);
// Short delay here improves reliability
Utils.delay(100, "New connection delay interrupted");
c.send(new GameStateMessage(this.serverState));
if (this.serverState == ServerState.IN_GAME) {
c.send(new VacantPlayersMessage().setVacantPlayers(getGame()));
}
logger.info("Client connected from " + name);
}
示例8: XDRTcpSocket
import java.net.Socket; //导入方法依赖的package包/类
public XDRTcpSocket(Socket s) throws IOException {
super("XDRTcpSocket for [ " + s.getInetAddress() + ":" + s.getPort() + " ] ", new XDROutputStream(s.getOutputStream()), new XDRInputStream(s.getInputStream()));
this.rawSocket = s;
closed = false;
/*this.mode = mode;
this.auth = auth;*/
}
示例9: InterposeSocket
import java.net.Socket; //导入方法依赖的package包/类
/**
* Construct a socket that interposes on a socket to match and replace.
* @param socket the underlying socket
* @param triggerBytes array of bytes to enable matching
* @param matchBytes the bytes that must match
* @param replaceBytes the replacement bytes
*/
public InterposeSocket(Socket socket, byte[]
triggerBytes, byte[] matchBytes, byte[] replaceBytes) {
this.socket = socket;
this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes");
this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
this.inLogStream = new ByteArrayOutputStream();
this.outLogStream = new ByteArrayOutputStream();
this.name = "IS" + ++num + "::"
+ Thread.currentThread().getName() + ": "
+ socket.getLocalPort() + " < " + socket.getPort();
}
示例10: Pump
import java.net.Socket; //导入方法依赖的package包/类
public Pump(Socket source, Socket dest) {
super("SocketProxy-DataTransfer-" + source.getPort() + ":" + dest.getPort());
src = source;
destination = dest;
pause.set(new CountDownLatch(0));
}
示例11: connect
import java.net.Socket; //导入方法依赖的package包/类
/**
* Like {@link NetUtils#connect(Socket, SocketAddress, int)} but
* also takes a local address and port to bind the socket to.
*
* @param socket
* @param endpoint the remote address
* @param localAddr the local address to bind the socket to
* @param timeout timeout in milliseconds
*/
public static void connect(Socket socket,
SocketAddress endpoint,
SocketAddress localAddr,
int timeout) throws IOException {
if (socket == null || endpoint == null || timeout < 0) {
throw new IllegalArgumentException("Illegal argument for connect()");
}
SocketChannel ch = socket.getChannel();
if (localAddr != null) {
Class localClass = localAddr.getClass();
Class remoteClass = endpoint.getClass();
Preconditions.checkArgument(localClass.equals(remoteClass),
"Local address %s must be of same family as remote address %s.",
localAddr, endpoint);
socket.bind(localAddr);
}
try {
if (ch == null) {
// let the default implementation handle it.
socket.connect(endpoint, timeout);
} else {
SocketIOWithTimeout.connect(ch, endpoint, timeout);
}
} catch (SocketTimeoutException ste) {
throw new ConnectTimeoutException(ste.getMessage());
}
// There is a very rare case allowed by the TCP specification, such that
// if we are trying to connect to an endpoint on the local machine,
// and we end up choosing an ephemeral port equal to the destination port,
// we will actually end up getting connected to ourself (ie any data we
// send just comes right back). This is only possible if the target
// daemon is down, so we'll treat it like connection refused.
if (socket.getLocalPort() == socket.getPort() &&
socket.getLocalAddress().equals(socket.getInetAddress())) {
LOG.info("Detected a loopback TCP socket, disconnecting it");
socket.close();
throw new ConnectException(
"Localhost targeted connection resulted in a loopback. " +
"No daemon is listening on the target port.");
}
}
示例12: getHostPort
import java.net.Socket; //导入方法依赖的package包/类
public static String getHostPort(Socket s)
{
String h = s.getInetAddress().getHostAddress().toString();
int port = s.getPort();
return h + ":" + port;
}
示例13: getRemotePort
import java.net.Socket; //导入方法依赖的package包/类
@Override
public int getRemotePort() {
final Socket socket = this.socketHolder.get();
return socket != null ? socket.getPort() : -1;
}
示例14: createHandle
import java.net.Socket; //导入方法依赖的package包/类
private String createHandle(Socket socket) {
return "Socket:" + socket.getInetAddress().toString() + ":" + socket.getPort() + "/" + (++socketIdx);
}
示例15: ClientConnection
import java.net.Socket; //导入方法依赖的package包/类
public ClientConnection(Server serv, Socket s) {
this.serv = serv;
this.s = s;
byte[] addr = s.getInetAddress().getAddress();
chId = (addr[0] << 48 | addr[1] << 32 | addr[2] << 24 | addr[3] << 16) + s.getPort(); //generate unique chId from client's IP and port
}