private final void socketrun() { do { if (link.socketport != 0) { try { Socket socket = new Socket(InetAddress.getByName(getCodeBase().getHost()), link.socketport); socket.setSoTimeout(30000); socket.setTcpNoDelay(true); link.s = socket; } catch (Exception _ex) { link.s = null; } link.socketport = 0; } if (link.runme != null) { Thread thread = new Thread(link.runme); thread.setDaemon(true); thread.start(); link.runme = null; } if (link.iplookup != null) { String s = "unknown"; try { s = InetAddress.getByName(link.iplookup).getHostName(); } catch (Exception _ex) { } link.host = s; link.iplookup = null; } try { Thread.sleep(100L); } catch (Exception _ex) { } } while (true); }
private void passTcpFileDescriptor( LocalSocket fdSocket, OutputStream outputStream, String socketId, String dstIp, int dstPort, int connectTimeout) throws Exception { Socket sock = new Socket(); sock.setTcpNoDelay(true); // force file descriptor being created if (protect(sock)) { try { sock.connect(new InetSocketAddress(dstIp, dstPort), connectTimeout); ParcelFileDescriptor fd = ParcelFileDescriptor.fromSocket(sock); tcpSockets.put(socketId, sock); fdSocket.setFileDescriptorsForSend(new FileDescriptor[] {fd.getFileDescriptor()}); outputStream.write('*'); outputStream.flush(); fd.detachFd(); } catch (ConnectException e) { LogUtils.e("connect " + dstIp + ":" + dstPort + " failed"); outputStream.write('!'); sock.close(); } catch (SocketTimeoutException e) { LogUtils.e("connect " + dstIp + ":" + dstPort + " failed"); outputStream.write('!'); sock.close(); } finally { outputStream.flush(); } } else { LogUtils.e("protect tcp socket failed"); } }
private void setSocketParameters(Socket client_sock) throws SocketException { if (log.isTraceEnabled()) log.trace( "[" + local_addr + "] accepted connection from " + client_sock.getInetAddress() + ":" + client_sock.getPort()); try { client_sock.setSendBufferSize(send_buf_size); } catch (IllegalArgumentException ex) { if (log.isErrorEnabled()) log.error("exception setting send buffer size to " + send_buf_size + " bytes", ex); } try { client_sock.setReceiveBufferSize(recv_buf_size); } catch (IllegalArgumentException ex) { if (log.isErrorEnabled()) log.error("exception setting receive buffer size to " + send_buf_size + " bytes", ex); } client_sock.setKeepAlive(true); client_sock.setTcpNoDelay(tcp_nodelay); if (linger > 0) client_sock.setSoLinger(true, linger); else client_sock.setSoLinger(false, -1); }
/** Constructor declaration */ public JICPConnection(TransportAddress ta, int timeout) throws IOException { // #MIDP_EXCLUDE_BEGIN // For some reason the local address or port may be in use while (true) { try { // #PJAVA_EXCLUDE_BEGIN sc = new Socket(); bindSocket(sc); sc.setTcpNoDelay(true); sc.connect(new InetSocketAddress(ta.getHost(), Integer.parseInt(ta.getPort())), timeout); // #PJAVA_EXCLUDE_END /*#PJAVA_INCLUDE_BEGIN sc = new Socket(ta.getHost(), Integer.parseInt(ta.getPort())); #PJAVA_INCLUDE_END*/ is = sc.getInputStream(); os = getOutputStream(); break; } catch (BindException be) { // Do nothing and try again } } // #MIDP_EXCLUDE_END /*#MIDP_INCLUDE_BEGIN String url = "socket://"+ta.getHost()+":"+ta.getPort(); sc = (StreamConnection) Connector.open(url, Connector.READ_WRITE, false); is = sc.openInputStream(); os = getOutputStream(); #MIDP_INCLUDE_END*/ }
void serviceClientApp(final Socket socket) throws Throwable { socket.setTcpNoDelay(true); final InputStream clientInput = socket.getInputStream(); clientOutput = socket.getOutputStream(); patchNumber = 1; mainFilePath = readPath(clientInput, false); byte ok[] = new byte[] {1, 0, 0, 0}; clientOutput.write(ok); executablePath = readPath(clientInput, true); new Thread( new Runnable() { public void run() { try { while (true) { int bundleLoaded = readInt(clientInput); } } catch (IOException e) { } finally { try { socket.close(); } catch (IOException e) { } clientOutput = null; } } }) .start(); }
public static void startServer() { try { System.out.println("Starting server..."); serverSocket = new ServerSocket(7788); System.out.println("Server Started... Address: " + serverSocket.getInetAddress()); while (true) { socket = serverSocket.accept(); for (int i = 0; i < 10; i++) { if (user[i] == null) { System.out.println("User " + (i + 1) + " connected from " + socket.getInetAddress()); socket.setTcpNoDelay(false); out = new ObjectOutputStream(socket.getOutputStream()); in = new ObjectInputStream(socket.getInputStream()); out.writeInt(i); out.flush(); User theUser = new User(out, in, i); user[i] = theUser; Thread thread = new Thread(user[i]); thread.start(); break; } } } } catch (IOException e) { e.printStackTrace(); } }
public TcpSocketPeer(Socket socket) { this.socket = socket; // turn off Nagle's algorithm since we should // always be doing buffering in the virtual machine try { socket.setTcpNoDelay(true); } catch (Exception e) { } }
/** * Applies the current settings to the given socket. * * @param s Socket to apply the settings to * @return Socket the input socket */ protected Socket applySettings(Socket s) { try { s.setKeepAlive(SO_KEEPALIVE); s.setOOBInline(OOBINLINE); s.setReuseAddress(SO_REUSEADDR); s.setTcpNoDelay(TCP_NODELAY); s.setOOBInline(OOBINLINE); s.setReceiveBufferSize(SO_RCVBUF); s.setSendBufferSize(SO_SNDBUF); s.setSoTimeout(SO_TIMEOUT); s.setSoLinger(SO_LINGER, LINGER); } catch (SocketException e) { throw new RuntimeException(e); } return s; }
protected void setSocketParameters(Socket client_sock) throws SocketException { try { client_sock.setSendBufferSize(server.send_buf_size); } catch (IllegalArgumentException ex) { server.log.error( "%s: exception setting send buffer to %d bytes: %s", server.local_addr, server.send_buf_size, ex); } try { client_sock.setReceiveBufferSize(server.recv_buf_size); } catch (IllegalArgumentException ex) { server.log.error( "%s: exception setting receive buffer to %d bytes: %s", server.local_addr, server.recv_buf_size, ex); } client_sock.setKeepAlive(true); client_sock.setTcpNoDelay(server.tcp_nodelay); if (server.linger > 0) client_sock.setSoLinger(true, server.linger); else client_sock.setSoLinger(false, -1); }
/** * Method declaration * * @return */ private Channel init() { try { mSocket.setTcpNoDelay(true); mInput = new DataInputStream(new BufferedInputStream(mSocket.getInputStream())); mOutput = new DataOutputStream(new BufferedOutputStream(mSocket.getOutputStream())); String user = mInput.readUTF(); String password = mInput.readUTF(); Channel c; try { mServer.trace(mThread + ":trying to connect user " + user); return mDatabase.connect(user, password); } catch (SQLException e) { write(new Result(e.getMessage()).getBytes()); } } catch (Exception e) { } return null; }
SocketConnection(Socket socket) throws IOException { this.socket = socket; socket.setTcpNoDelay(true); socketInput = socket.getInputStream(); socketOutput = socket.getOutputStream(); }
public static void main(String[] args) throws IOException { int n; String aux[] = new String[32]; int linie, coloana; Data.setACC(10); Data.setBRK(10); Data.setMaxSPD(100); Data.setgetSteeringCar(0.5); try { // create socket socket = new Socket("127.0.0.1", 6666); // we attempt to bypass nagle's algorithm socket.setTcpNoDelay(true); // initialize our communication class comm = new NetworkCommunication(socket); comm.initStreams(); // send initial packet, aka the team's name comm.writeInt(H_TEAM_NAME); comm.writeInt(1); comm.writeString(args[0]); // comm.writeString("void"); } catch (UnknownHostException e) { System.out.println("could not connect to server"); System.exit(1); } catch (IOException e) { System.out.println("No I/O"); System.exit(1); } System.out.println("Sent team name....entering main loop"); while (true) { try { int header = comm.readInt(); switch (header) { case H_MAP_DIM: // 2 { Data.setMapWidth(comm.readInt()); Data.setMapHeigh(comm.readInt()); Data.Map = new int[Data.getMapHeigh()][Data.getMapWidth()]; int nr_data_recieve = Data.getMapHeigh() * Data.getMapWidth() / 32 + 1; for (int i = 0; i < nr_data_recieve; i++) { n = comm.readInt(); for (int j = 0; j < 32; j++) { if ((n & (1 << j)) == 0) aux[31 - j] = "0"; else aux[31 - j] = "1"; } for (int k = 0; k < 32; k++) { linie = (32 * i + k) / Data.getMapWidth(); coloana = (32 * i + k) - (linie * Data.getMapWidth()); if (Data.getMapHeigh() > linie && Data.getMapWidth() > coloana) Data.Map[linie][coloana] = Integer.parseInt(aux[k]); } } System.out.println("Received map dimentions packet"); } ; break; case H_INITIAL_INFO: // 3 { Data.setStartPointX(comm.readInt()); Data.setStartPointY(comm.readInt()); Data.setWidth_meters(comm.readInt()); Data.setHeight_meters(comm.readInt()); Data.setDirection(comm.readInt()); Data.setNumberOfLaps(comm.readInt()); Data.setMaximumLapTime(comm.readInt()); Data.setCarAngle(comm.readDouble()); // trimite-m la server car configuration comm.writeInt(H_CAR_CONFIG); comm.writeInt(Data.getACC()); comm.writeInt(Data.getBRK()); comm.writeInt(Data.getMaxSPD()); comm.writeDouble(Data.getSteeringCar()); System.out.println("Received initial information packet"); } ; break; case H_CAR_CONFIRM: // 4 { Data.setACC(comm.readInt()); Data.setBRK(comm.readInt()); Data.setMaxSPD(comm.readInt()); Data.setgetSteeringCar(comm.readDouble()); System.out.println("Received car confirm packet"); // System.out.println(Data.getACC()+" "+Data.getBRK()+" "+Data.getSteeringCar()); // pornim masina :)) Engine.StartPosition(100, 0, Data.getCarAngle()); comm.writeInt(H_DRIVE_INFO); comm.writeDouble(Data.getAccelerationPercentage()); comm.writeDouble(Data.getBrakePercentage()); comm.writeDouble(Data.getWantedAngle()); comm.writeInt(0); // drop queue System.out.println("Send driving info1"); } ; break; case H_POS_CONFIRM: // 6 { Data.setCurrent_X_Meters(comm.readDouble()); Data.setCurrent_Y_Meters(comm.readDouble()); Data.setCurrentSpeed(comm.readDouble()); Data.setCurrentAngleInRadians(comm.readDouble()); Data.setCurrentDirection(comm.readInt()); System.out.println("Received position confirmation packet"); // System.out.println(Data.getCurrent_X_Meters()+"-"+Data.getCurrent_Y_Meters()+"pozitia masinii"); // System.out.println(Data.getCurrentSpeed()+"-"+Data.getCurrentDirection()+"-"+Engine.FromRadiusToAngle(Data.getCurrentAngleInRadians())); Engine.evalPosition(); comm.writeInt(H_DRIVE_INFO); comm.writeDouble(Data.getAccelerationPercentage()); comm.writeDouble(Data.getBrakePercentage()); comm.writeDouble(Data.getWantedAngle()); comm.writeInt(0); // drop queue // System.out.println("Send driving info2"); } ; break; case H_END_RACE: { System.out.println("Received end race packet"); } ; break; default: { System.out.println("Unknown packet"); // System.out.println(header); } } } catch (IOException e) { System.out.println("No I/O"); System.exit(1); } } }
public void setTcpNoDelay(boolean on) throws SocketException { mSocket.setTcpNoDelay(on); }
Rconnection(String host, int port, RSession session) throws RSrvException { try { if (connected) s.close(); s = null; } catch (Exception e) { throw new RSrvException(this, "Cannot connect: " + e.getMessage()); } if (session != null) { host = session.host; port = session.port; } connected = false; this.host = host; this.port = port; try { s = new Socket(host, port); // disable Nagle's algorithm since we really want immediate replies s.setTcpNoDelay(true); } catch (Exception sce) { throw new RSrvException(this, "Cannot connect: " + sce.getMessage()); } try { is = s.getInputStream(); os = s.getOutputStream(); } catch (Exception gse) { throw new RSrvException(this, "Cannot get io stream: " + gse.getMessage()); } rt = new Rtalk(is, os); if (session == null) { byte[] IDs = new byte[32]; int n = -1; try { n = is.read(IDs); } catch (Exception sre) { throw new RSrvException(this, "Error while receiving data: " + sre.getMessage()); } try { if (n != 32) { throw new RSrvException(this, "Handshake failed: expected 32 bytes header, got " + n); } String ids = new String(IDs); if (ids.substring(0, 4).compareTo("Rsrv") != 0) throw new RSrvException( this, "Handshake failed: Rsrv signature expected, but received \"" + ids + "\" instead."); try { rsrvVersion = Integer.parseInt(ids.substring(4, 8)); } catch (Exception px) { } // we support (knowingly) up to 103 if (rsrvVersion > 103) throw new RSrvException( this, "Handshake failed: The server uses more recent protocol than this client."); if (ids.substring(8, 12).compareTo("QAP1") != 0) throw new RSrvException( this, "Handshake failed: unupported transfer protocol (" + ids.substring(8, 12) + "), I talk only QAP1."); for (int i = 12; i < 32; i += 4) { String attr = ids.substring(i, i + 4); if (attr.compareTo("ARpt") == 0) { if (!authReq) { // this method is only fallback when no other was specified authReq = true; authType = AT_plain; } } if (attr.compareTo("ARuc") == 0) { authReq = true; authType = AT_crypt; } if (attr.charAt(0) == 'K') { Key = attr.substring(1, 3); } } } catch (RSrvException innerX) { try { s.close(); } catch (Exception ex01) { } ; is = null; os = null; s = null; throw innerX; } } else { // we have a session to take care of try { os.write(session.key, 0, 32); } catch (Exception sre) { throw new RSrvException(this, "Error while sending session key: " + sre.getMessage()); } rsrvVersion = session.rsrvVersion; } connected = true; lastError = "OK"; }