List of usage examples for java.net Socket getInputStream
public InputStream getInputStream() throws IOException
From source file:Main.java
public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 12900); System.out.println("Started client socket at " + socket.getLocalSocketAddress()); BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in)); String promptMsg = "Please enter a message (Bye to quit):"; String outMsg = null;/*from www.ja v a2s .c o m*/ System.out.print(promptMsg); while ((outMsg = consoleReader.readLine()) != null) { if (outMsg.equalsIgnoreCase("bye")) { break; } // Add a new line to the message to the server, // because the server reads one line at a time. socketWriter.write(outMsg); socketWriter.write("\n"); socketWriter.flush(); // Read and display the message from the server String inMsg = socketReader.readLine(); System.out.println("Server: " + inMsg); System.out.println(); // Print a blank line System.out.print(promptMsg); } socket.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 {//from w w w. ja v a2s.com // 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:Finger.java
public static void main(String[] arguments) throws Exception { StringTokenizer split = new StringTokenizer(arguments[0], "@"); String user = split.nextToken(); String host = split.nextToken(); Socket digit = new Socket(host, 79); digit.setSoTimeout(20000);//from w w w. j a va 2 s . com PrintStream out = new PrintStream(digit.getOutputStream()); out.print(user + "\015\012"); BufferedReader in = new BufferedReader(new InputStreamReader(digit.getInputStream())); boolean eof = false; while (!eof) { String line = in.readLine(); if (line != null) System.out.println(line); else eof = true; } digit.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String hostname = "localhost"; Socket connection = null; connection = new Socket(hostname, DEFAULT_PORT); Writer out = new OutputStreamWriter(connection.getOutputStream(), "8859_1"); out.write("\r\n"); out.flush();/*w w w. j a v a2s. com*/ InputStream raw = connection.getInputStream(); BufferedInputStream buffer = new BufferedInputStream(raw); InputStreamReader in = new InputStreamReader(buffer, "8859_1"); int c; while ((c = in.read()) != -1) { if ((c >= 32 && c < 127) || c == '\t' || c == '\r' || c == '\n') { System.out.write(c); } } connection.close(); }
From source file:HttpMirror.java
public static void main(String args[]) { try {//from www.ja v a 2 s .c o 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: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 va 2 s . c om*/ 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:BufferedSocketClient.java
public static void main(String args[]) throws Exception { int cTosPortNumber = 1777; String str;/*w w w . jav a 2s. c o m*/ ServerSocket servSocket = new ServerSocket(cTosPortNumber); System.out.println("Waiting for a connection on " + cTosPortNumber); Socket fromClientSocket = servSocket.accept(); PrintWriter pw = new PrintWriter(fromClientSocket.getOutputStream(), true); BufferedReader br = new BufferedReader(new InputStreamReader(fromClientSocket.getInputStream())); while ((str = br.readLine()) != null) { System.out.println("The message: " + str); if (str.equals("bye")) { pw.println("bye"); break; } else { str = "Server returns " + str; pw.println(str); } } pw.close(); br.close(); fromClientSocket.close(); }
From source file:net.socket.bio.TimeClient.java
/** * @param args/*w w w . jav a 2s .c om*/ */ 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:AnotherBeerServer.java
public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); System.out.println("Listening"); Socket sock = ssock.accept(); ssock.close(); // no more connects PrintStream ps = new PrintStream(sock.getOutputStream()); // ask for count ps.print("count? "); BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream())); // read and parse it String line = input.readLine(); ps.println(""); int count = Integer.parseInt(line); for (int i = count; i >= 0; i--) { ps.println(i + " Java Source and Support."); }// ww w . j ava 2 s. com ps.close(); sock.close(); }
From source file:HTTPServer.java
public static void main(String[] args) throws Exception { ServerSocket sSocket = new ServerSocket(1777); while (true) { System.out.println("Waiting for a client..."); Socket newSocket = sSocket.accept(); System.out.println("accepted the socket"); OutputStream os = newSocket.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(newSocket.getInputStream())); String inLine = null;//from www . j a v a 2s . c o m while (((inLine = br.readLine()) != null) && (!(inLine.equals("")))) { System.out.println(inLine); } System.out.println(""); StringBuffer sb = new StringBuffer(); sb.append("<html>\n"); sb.append("<head>\n"); sb.append("<title>Java \n"); sb.append("</title>\n"); sb.append("</head>\n"); sb.append("<body>\n"); sb.append("<H1>HTTPServer Works!</H1>\n"); sb.append("</body>\n"); sb.append("</html>\n"); String string = sb.toString(); byte[] byteArray = string.getBytes(); os.write("HTTP/1.0 200 OK\n".getBytes()); os.write(new String("Content-Length: " + byteArray.length + "\n").getBytes()); os.write("Content-Type: text/html\n\n".getBytes()); os.write(byteArray); os.flush(); os.close(); br.close(); newSocket.close(); } }