List of usage examples for java.net Socket Socket
public Socket(InetAddress address, int port) throws IOException
From source file:disko.flow.analyzers.socket.SocketTransmitter.java
public void writeMessage(Message message) { log.debug("Establishing connection to " + host + ":" + port); while (true) { Socket socket = null;/* w w w .j a va2 s . c o m*/ ObjectOutputStream out = null; try { socket = new Socket(host, port); out = new ObjectOutputStream(socket.getOutputStream()); out.writeObject(message); out.flush(); log.debug("Sent " + message); return; } catch (UnknownHostException e) { log.error("Error connecting to " + host + ":" + port, e); } catch (IOException e) { log.error("Error sending to " + host + ":" + port + " message " + message.getData(), e); } finally { if (out != null) try { out.close(); } catch (Throwable ignored) { log.error("While closing transmitter output.", ignored); } if (socket != null) try { socket.close(); } catch (Throwable ignored) { log.error("While closing transmitter socket.", ignored); } } try { Thread.sleep(1000); } catch (InterruptedException ignored) { break; } } }
From source file:com.dc.tes.License.java
private static String LicenseServerConnect(Message request) { String license = ""; Socket client = null;//from ww w . j a v a 2 s . c om try { client = new Socket(LICENSE_SERVER_IP, LICENSE_SERVER_PORT); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); license = "Lisence?"; log.error(license); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); license = "Lisence?"; log.error(license); } try { client.getOutputStream().write(request.Export()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); license = "?Lisence??"; log.error(license); } Message response = null; try { response = new Message(client.getInputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); license = "?Lisence??"; log.error(license); } if (response.getInteger(MessageItem.LICENSE.AVAILABLE) == 0) { license = "License???" + response.getString(MessageItem.LICENSE.MSG); log.error(license); } if (request.getInteger(MessageItem.LICENSE.SIGN) == 0) { license = response.getString(MessageItem.LICENSE.LICENSEFILE); } return license; }
From source file:org.bombusim.networking.NetworkSocketDataStream.java
public NetworkSocketDataStream(String server, int port) throws UnknownHostException, IOException { this.host = server; this.port = port; LimeLog.i("Socket", "Connecting to " + host + ":" + port, null); socket = new Socket(server, port); //keep-alive packets every 2 hours (by default) socket.setKeepAlive(true);//from w w w . ja v a2 s. c o m istream = socket.getInputStream(); ostream = socket.getOutputStream(); }
From source file:fitnesse.slim.SlimClient.java
private boolean tryConnect() { try {/* www . j av a 2s . com*/ client = new Socket(hostName, port); return true; } catch (IOException e) { return false; } }
From source file:com.esri.geoevent.test.tools.RunTcpInBdsOutTest.java
public void send(String server, Integer port, Long numEvents, Integer rate, String data_file) { BufferedReader br = null;//from w w w . ja v a2 s . co m ArrayList<String> lines = new ArrayList<>(); LocalDateTime st = null; try { // Read the file into String array br = new BufferedReader(new FileReader(data_file)); String line = null; while ((line = br.readLine()) != null) { lines.add(line); } Socket sckt = new Socket(server, port); OutputStream os = sckt.getOutputStream(); Integer cnt = 0; st = LocalDateTime.now(); Double ns_delay = 1000000000.0 / (double) rate; long ns = ns_delay.longValue(); if (ns < 0) { ns = 0; } int i = 0; int j = 0; while (i < numEvents) { i++; j++; if (j >= lines.size()) { j = 0; } line = lines.get(j) + "\n"; final long stime = System.nanoTime(); long etime = 0; do { etime = System.nanoTime(); } while (stime + ns >= etime); os.write(line.getBytes()); os.flush(); } LocalDateTime et = LocalDateTime.now(); if (st != null) { et = LocalDateTime.now(); Duration delta = Duration.between(st, et); Double elapsed_seconds = (double) delta.getSeconds() + delta.getNano() / 1000000000.0; send_rate = (double) numEvents / elapsed_seconds; } sckt.close(); os = null; } catch (Exception e) { System.err.println(e.getMessage()); send_rate = -1.0; } finally { try { br.close(); } catch (Exception e) { // } this.send_rate = send_rate; } }
From source file:general.Client.java
public void connectToServer() { isConnected = false;// w w w . j a v a 2 s . c o m try { sock = new Socket("127.0.0.1", DEFAULT_PORT); isConnected = true; } catch (IOException e) { e.printStackTrace(); } if (thread == null) { thread = new Thread(); thread.start(); } }
From source file:com.topekalabs.bigmachine.lib.app.ContinuationSerializationTest.java
@Ignore @Test//from w w w .j av a2 s. c o m public void networkSerializationTest() throws Exception { Socket outputSocket = null; ServerSocket inputSocket = null; outputSocket = new Socket("localhost", TEST_SOCKET); inputSocket = new ServerSocket(TEST_SOCKET); Continuation c = Continuation.startWith(new SimpleContinuationRunnable()); Kryo kryo = new Kryo(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); Output output = new Output(bos); kryo.writeObject(output, c); }
From source file:org.mule.module.http.functional.listener.HttpListenerPersistentConnectionsTestCase.java
protected void assertConnectionClosesAfterSend(DynamicPort port, HttpVersion httpVersion) throws IOException { Socket socket = new Socket("localhost", port.getNumber()); sendRequest(socket, httpVersion);/*from w w w. j a va2 s .c om*/ assertResponse(getResponse(socket), true); sendRequest(socket, httpVersion); assertResponse(getResponse(socket), false); socket.close(); }
From source file:SimpleFTP.java
/** * Connects to an FTP server and logs in with the supplied username and * password./*from w w w . ja v a 2s.c om*/ */ public synchronized void connect(String host, int port, String user, String pass) throws IOException { if (socket != null) { throw new IOException("SimpleFTP is already connected. Disconnect first."); } socket = new Socket(host, port); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String response = readLine(); if (!response.startsWith("220 ")) { throw new IOException( "SimpleFTP received an unknown response when connecting to the FTP server: " + response); } sendLine("USER " + user); response = readLine(); if (!response.startsWith("331 ")) { throw new IOException("SimpleFTP received an unknown response after sending the user: " + response); } sendLine("PASS " + pass); response = readLine(); if (!response.startsWith("230 ")) { throw new IOException("SimpleFTP was unable to log in with the supplied password: " + response); } // Now logged in. }
From source file:com.tomcat.monitor.zabbix.ZabbixSender.java
public void send(final String zabbixServer, final int zabbixPort, int zabbixTimeout, final String host, final String key, final String value) throws IOException { final byte[] response = new byte[1024]; final long start = System.currentTimeMillis(); final int TIMEOUT = zabbixTimeout * 1000; final StringBuilder message = new StringBuilder("<req><host>"); message.append(new String(Base64.encodeBase64(host.getBytes()))); message.append("</host><key>"); message.append(new String(Base64.encodeBase64(key.getBytes()))); message.append("</key><data>"); message.append(new String(Base64.encodeBase64(value.getBytes()))); message.append("</data></req>"); if (log.isDebugEnabled()) { log.debug("sending " + message); }/*from www . java 2 s. com*/ Socket zabbix = null; OutputStreamWriter out = null; InputStream in = null; try { zabbix = new Socket(zabbixServer, zabbixPort); zabbix.setSoTimeout(TIMEOUT); out = new OutputStreamWriter(zabbix.getOutputStream()); out.write(message.toString()); out.flush(); in = zabbix.getInputStream(); final int read = in.read(response); if (log.isDebugEnabled()) { log.debug("received " + new String(response)); } if (read != 2 || response[0] != 'O' || response[1] != 'K') { log.warn("received unexpected response '" + new String(response) + "' for key '" + key + "'"); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } if (zabbix != null) { zabbix.close(); } } log.info("send() " + (System.currentTimeMillis() - start) + " ms"); }