List of usage examples for java.net Socket getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:net.javacrumbs.mocksocket.SampleTest.java
@Test public void testConditionalData() throws Exception { byte[] dataToWrite = new byte[] { 5, 4, 3, 2 }; byte[] mockData = new byte[] { 1, 2, 3, 4 }; expectCall().andWhenRequest(data(is(dataToWrite))).thenReturn(data(mockData)); Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234); IOUtils.write(dataToWrite, socket.getOutputStream()); byte[] data = IOUtils.toByteArray(socket.getInputStream()); socket.close();//from w ww .j av a 2 s. c o m assertThat(data, is(mockData)); }
From source file:eu.smeny.jpapercut.smtp.MailServer.java
private void refuseConnection(Socket client) { try {/* w w w .j av a 2s .c om*/ OutputStream output = client.getOutputStream(); PrintWriter writer = new PrintWriter(output); String hostname = retrieveHostname(); writer.write("421 " + hostname + " Service not available, too much connections"); writer.flush(); client.close(); } catch (IOException ioe) { logger.error("IO Error while refusing connection", ioe); } }
From source file:dualcontrol.CryptoClientDemo.java
private void call(Properties properties, MockableConsole console, String hostAddress, int port, byte[] data) throws Exception { Socket socket = SSLContexts.create(false, "cryptoclient.ssl", properties, console).getSocketFactory() .createSocket(hostAddress, port); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeShort(data.length);/*from w w w. j a va2 s. c o m*/ dos.write(data); dos.flush(); DataInputStream dis = new DataInputStream(socket.getInputStream()); byte[] ivBytes = new byte[dis.readShort()]; dis.readFully(ivBytes); byte[] bytes = new byte[dis.readShort()]; dis.readFully(bytes); if (new String(data).contains("DECRYPT")) { System.err.printf("INFO CryptoClientDemo decrypted %s\n", new String(bytes)); } else { System.out.printf("%s:%s\n", Base64.encodeBase64String(ivBytes), Base64.encodeBase64String(bytes)); } socket.close(); }
From source file:de.iew.imageupload.actions.UploadImageAction.java
public void actionPerformed(ActionEvent ev) { try {//from ww w. ja va 2s . c om Socket socket = new Socket("squeeze", 8124); FileInputStream fin = new FileInputStream(this.fileToUpload); OutputStream out = socket.getOutputStream(); IOUtils.copy(fin, out); out.flush(); fin.close(); out.close(); socket.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.esri.geoevent.test.performance.tcp.ProducerTcpSocketServer.java
/** * Send the event in a round robin fashion to distribute the load * * @param message to send/*from w w w . jav a2 s . c om*/ */ public void sendEvent(String message) { Socket socket = clients.pop(); try { OutputStream out = socket.getOutputStream(); out.write(message.getBytes()); out.flush(); } catch (Exception error) { error.printStackTrace(); } clients.push(socket); }
From source file:com.fuzhepan.arpc.client.ProxyHandler.java
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { log.debug("invoke was called!"); if (method.getName().equals("toString")) { return "toString method was called"; }/*w ww .j ava 2 s . c om*/ RpcContext rpcContext = new RpcContext(interfaceName, method.getName(), method.getParameterTypes(), args); //get service info and load balance List<HostPortPair> serviceList = RpcConfig.getServiceList(serviceName); if (serviceList == null || serviceList.size() == 0) throw new ClassNotFoundException("not find service : " + serviceName); int index = requestCount.get() % serviceList.size(); if (requestCount.get() > 100) requestCount.set(0); else requestCount.getAndIncrement(); HostPortPair hostPort = serviceList.get(index); Socket socket = new Socket(hostPort.host, hostPort.port); ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); objectOutputStream.writeObject(rpcContext); objectOutputStream.flush(); ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); Object response = objectInputStream.readObject(); objectInputStream.close(); objectOutputStream.close(); socket.close(); Class methodReturnType = method.getReturnType(); return methodReturnType.cast(response); }
From source file:TimeServer.java
public void run() { Socket client = null; while (true) { if (sock == null) return; try {/*from www.j a va 2 s. c o m*/ client = sock.accept(); BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()); PrintWriter os = new PrintWriter(bos, false); String outLine; Date now = new Date(); os.println(now); os.flush(); os.close(); client.close(); } catch (IOException e) { System.out.println("Error: couldn't connect to client."); System.exit(1); } } }
From source file:org.mule.module.http.functional.listener.HttpListenerPersistentConnectionsTestCase.java
private void sendRequest(Socket socket, HttpVersion httpVersion) throws IOException { PrintWriter writer = new PrintWriter(socket.getOutputStream()); writer.println("GET / " + httpVersion); writer.println("Host: www.example.com"); writer.println(""); writer.flush();/*from ww w . j a va2s .co m*/ }
From source file:LCModels.MessageTransmitter.java
@Override public void run() { try {/* w w w . j a v a2s . com*/ // Socket connects to the ServerSocket, ServerSocket receives a Socket, what? haha// //send data as points // Socket s = new Socket(hostname,targetPort); // Socket z = new Socket(hostname, 48500, InetAddress.getByName("127.0.0.1"), targetPort); Socket client = new Socket(hostname, targetPort); /* Get server's OutputStream */ OutputStream outToServer = client.getOutputStream(); /* Get server's DataOutputStream to write/send message */ DataOutputStream out = new DataOutputStream(outToServer); /* Write message to DataOutputStream */ out.writeUTF(transmitJSON.toString()); /* Get InputStream to get message from server */ InputStream inFromServer = client.getInputStream(); /* Get DataInputStream to read message of server */ DataInputStream in = new DataInputStream(inFromServer); /* Print message received from server */ System.out.println("Server says..." + in.readUTF()); client.close(); } catch (IOException ex) { Logger.getLogger(MessageTransmitter.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:epn.edu.ec.bibliotecadigital.cliente.Client.java
@Override public void run() { try {//from w w w .j a va 2 s .c o m clientSocketBalancer = new Socket(InetAddress.getByName(serverIP), portBalancer); DataInputStream dataInBalancer = new DataInputStream(clientSocketBalancer.getInputStream()); DataOutputStream dataOut = new DataOutputStream(clientSocketBalancer.getOutputStream()); dataOut.writeUTF((String) params[0]);//nombre de usuario String ipServer = dataInBalancer.readUTF(); int portServer = dataInBalancer.readInt(); clientSocketBalancer.close(); Socket clientSocket = new Socket(ipServer, portServer); dataOut = new DataOutputStream(clientSocket.getOutputStream()); dataOut.writeUTF(accion); dataOut.writeUTF((String) params[0]);//nombre de usuario InputStream in; DataInputStream dataIn; switch (accion) { case "bajar": dataOut = new DataOutputStream(clientSocket.getOutputStream()); dataOut.writeUTF((String) params[1]); dataIn = new DataInputStream(clientSocket.getInputStream()); boolean encontrado = dataIn.readBoolean(); if (!encontrado) { System.out.println("Libro con el cdigo: " + params[1] + " no encontrado"); break; } String fileName = dataIn.readUTF(); System.out.println( "Descargando libro " + fileName + " con cdigo " + params[1] + " en la carpeta Donwloads"); String home = System.getProperty("user.home"); in = clientSocket.getInputStream(); try { FileOutputStream out = new FileOutputStream(new File(home + "\\Downloads\\" + fileName)); byte[] bytes = new byte[64 * 1024]; int count; while ((count = in.read(bytes)) > 0) { out.write(bytes, 0, count); } out.close(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(in); } break; case "subir": dataOut = new DataOutputStream(clientSocket.getOutputStream()); dataOut.writeUTF(((File) params[1]).getName()); OutputStream out = clientSocket.getOutputStream(); try { byte[] bytes = new byte[64 * 1024]; in = new FileInputStream((File) params[1]); int count; while ((count = in.read(bytes)) > 0) { out.write(bytes, 0, count); } in.close(); } finally { IOUtils.closeQuietly(out); } break; case "obtenerLista": ObjectInputStream inFromClient = new ObjectInputStream(clientSocket.getInputStream()); System.out.println("Libros disponibles: \n"); List<Libro> libros = (List<Libro>) inFromClient.readObject(); System.out.println("\tCdigo\tNommbre\n"); for (Libro lbr : libros) { System.out.println("\t" + lbr.getCodigolibro() + "\t" + lbr.getNombre()); } inFromClient.close(); break; case "verificar": break; } dataOut.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException ex) { Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); } }