public NetJavaSocketImpl (Protocol protocol, String host, int port, SocketHints hints) { try { // create the socket socket = new java.net.Socket(); applyHints(hints); // better to call BEFORE socket is connected! // and connect... InetSocketAddress address = new InetSocketAddress(host, port); if (hints != null) { socket.connect(address, hints.connectTimeout); } else { socket.connect(address); } } catch (Exception e) { throw new GdxRuntimeException("Error making a socket connection to " + host + ":" + port, e); } }
private void applyHints (SocketHints hints) { if (hints != null) { try { socket.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency, hints.performancePrefBandwidth); socket.setTrafficClass(hints.trafficClass); socket.setTcpNoDelay(hints.tcpNoDelay); socket.setKeepAlive(hints.keepAlive); socket.setSendBufferSize(hints.sendBufferSize); socket.setReceiveBufferSize(hints.receiveBufferSize); socket.setSoLinger(hints.linger, hints.lingerDuration); socket.setSoTimeout(hints.socketTimeout); } catch (Exception e) { throw new GdxRuntimeException("Error setting socket hints.", e); } } }
public CardshifterNonGWTClient(CardshifterPlatform platform, String host, int port, CardshifterMessageHandler handler, LoginMessage loginMessage) { socket = Gdx.net.newClientSocket(Net.Protocol.TCP, host, port, new SocketHints()); output = socket.getOutputStream(); input = socket.getInputStream(); transformer = new ByteTransformer(new GdxLogger(), new GdxReflection()); this.handler = handler; try { output.write("{ \"command\": \"serial\", \"type\": \"1\" }".getBytes()); output.flush(); Gdx.app.log("Client", "Sent serial type"); platform.setupLogging(); send(loginMessage); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } new Thread(this).start(); }
public ClientSocket(Net.Protocol protocol, String host, int port, SocketHints hints) { try { // create the socket socket = new java.net.Socket(); applyHints(hints); // better to call BEFORE socket is connected! // and connect... InetSocketAddress address = new InetSocketAddress(host, port); if (hints != null) { socket.connect(address, hints.connectTimeout); } else { socket.connect(address); } } catch (Exception e) { throw new GdxRuntimeException("Error making a socket connection to " + host + ":" + port, e); } }
private void applyHints(SocketHints hints) { if (hints != null) { try { socket.setPerformancePreferences(hints.performancePrefConnectionTime, hints.performancePrefLatency, hints.performancePrefBandwidth); socket.setTrafficClass(hints.trafficClass); socket.setTcpNoDelay(hints.tcpNoDelay); socket.setKeepAlive(hints.keepAlive); socket.setSendBufferSize(hints.sendBufferSize); socket.setReceiveBufferSize(hints.receiveBufferSize); socket.setSoLinger(hints.linger, hints.lingerDuration); } catch (Exception e) { throw new GdxRuntimeException("Error setting socket hints.", e); } } }
public boolean connect() { if (socket != null) { if (socket.isConnected()) return true; } try { SocketHints hints = new SocketHints(); socket = Gdx.net.newClientSocket(Net.Protocol.TCP, serverAddress, serverPort, hints); os = socket.getOutputStream(); bos = new BufferedOutputStream(os, BUFFER_SIZE); dos = new DataOutputStream(bos); in = socket.getInputStream(); bis = new BufferedInputStream(in, BUFFER_SIZE); dis = new DataInputStream(bis); dos.writeUTF(CONNECT); dos.flush(); String response = dis.readUTF(); log.debug("got server message: " + response); if (response.equals(CONNECTED)) { return true; } } catch (Exception e) { log.error("an error occured", e); } return false; }
public void init() { if (initialized) return; invitesWindow = new InvitesWindow(); playersWindow = new InvitesWindow(); initialized = true; try { socket = Gdx.net.newClientSocket(Net.Protocol.TCP, "localhost", 1337, new SocketHints()); } catch (Exception e) { return; } new Thread(new Runnable() { @Override public void run() { final BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (true) { try { final String msg = r.readLine(); Gdx.app.postRunnable(new Runnable() { @Override public void run() { ClientServerMessage message = ClientServerMessage.json.fromJson(ClientServerMessage.class, msg); receive(message); } }); } catch (IOException ignored) { System.out.println(ignored.getMessage()); } } } }).start(); sendToServer(ClientServerMessage.Type.loadPlayersToInvite); sendToServer(ClientServerMessage.Type.loadInvites); }
@Override public Socket accept (SocketHints hints) { try { return new NetJavaSocketImpl(server.accept(), hints); } catch (Exception e) { throw new GdxRuntimeException("Error accepting socket.", e); } }
@Override public Socket accept(SocketHints hints) { try { return new ClientSocket(server.accept(), hints); } catch (Exception e) { throw new GdxRuntimeException("Error accepting socket.", e); } }
public Socket newClientSocket(Protocol protocol, String host, int port, SocketHints hints) { return nativeNet.newClientSocket(protocol, host, port, hints); }
public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) { return new NetJavaSocketImpl(protocol, host, port, hints); }
@Override public Socket newClientSocket(final Protocol protocol, final String host, final int port, final SocketHints hints) { return net.newClientSocket(protocol, host, port, hints); }
public NetJavaSocketImpl (java.net.Socket socket, SocketHints hints) { this.socket = socket; applyHints(hints); }
@Override public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) { throw new UnsupportedOperationException("Not implemented"); }
@Override public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) { return new NetJavaSocketImpl(protocol, host, port, hints); }
public ClientSocket(java.net.Socket socket, SocketHints hints) { this.socket = socket; applyHints(hints); }
@Override public Socket newClientSocket(Protocol protocol, String host, int port, SocketHints hints) { return new ClientSocket(protocol, host, port, hints); }
/** Creates a new TCP client socket that connects to the given host and port. * * @param host the host address * @param port the port * @param hints additional {@link SocketHints} used to create the socket. Input null to use the default setting provided by the * system. * @return GdxRuntimeException in case the socket couldn't be opened */ public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints);