List of usage examples for java.net Socket getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:Main.java
public static void main(String[] argv) throws Exception { Socket t = new Socket("127.0.0.1", 7); DataInputStream dis = new DataInputStream(t.getInputStream()); PrintStream ps = new PrintStream(t.getOutputStream()); ps.println("Hello"); String str = dis.readUTF();/*w w w . j a v a 2 s . c om*/ if (str.equals("Hello")) System.out.println("Alive!"); else System.out.println("Dead"); t.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();/* ww w . j a v a 2s . c om*/ 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:BufferedSocketClient.java
public static void main(String args[]) throws Exception { int cTosPortNumber = 1777; String str;//from ww w . j a va2 s .co 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:Main.java
public static void main(String[] argv) throws Exception { String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8"); Socket socket = new Socket("127.0.0.1", 8080); String path = "/servlet"; BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")); wr.write("POST " + path + " HTTP/1.0\r\n"); wr.write("Content-Length: " + data.length() + "\r\n"); wr.write("Content-Type: application/x-www-form-urlencoded\r\n"); wr.write("\r\n"); wr.write(data);/* w w w . j av a2s .c o m*/ wr.flush(); BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while ((line = rd.readLine()) != null) { System.out.println(line); } wr.close(); rd.close(); }
From source file:delete_tcp.java
public static void main(String ar[]) throws IOException { ServerSocket ss = new ServerSocket(9999); Socket s = ss.accept(); DataInputStream in = new DataInputStream(s.getInputStream()); DataOutputStream out = new DataOutputStream(s.getOutputStream()); String id = in.readUTF();/*from ww w .j ava2 s . com*/ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); EmployeeJDBCTemplate employeeJDBCTemplate = (EmployeeJDBCTemplate) context.getBean("employeeJDBCTemplate"); System.out.println("Deleting Records..."); employeeJDBCTemplate.delete(Integer.parseInt(id)); out.writeUTF("Success"); s.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;// ww w. j av a2 s . c om 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(); } }
From source file:com.cncounter.test.httpclient.ProxyTunnelDemo.java
public final static void main(String[] args) throws Exception { ProxyClient proxyClient = new ProxyClient(); HttpHost target = new HttpHost("www.google.com", 80); HttpHost proxy = new HttpHost("localhost", 1080); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd"); Socket socket = proxyClient.tunnel(proxy, target, credentials); try {/* ww w .j a v a2 s .c o m*/ Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET); out.write("GET / HTTP/1.1\r\n"); out.write("Host: " + target.toHostString() + "\r\n"); out.write("Agent: whatever\r\n"); out.write("Connection: close\r\n"); out.write("\r\n"); out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET)); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } } finally { socket.close(); } }
From source file:com.dlmu.heipacker.crawler.client.ProxyTunnelDemo.java
public final static void main(String[] args) throws Exception { ProxyClient proxyClient = new ProxyClient(); HttpHost target = new HttpHost("www.yahoo.com", 80); HttpHost proxy = new HttpHost("localhost", 8888); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd"); Socket socket = proxyClient.tunnel(proxy, target, credentials); try {//www . jav a2 s . co m Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET); out.write("GET / HTTP/1.1\r\n"); out.write("Host: " + target.toHostString() + "\r\n"); out.write("Agent: whatever\r\n"); out.write("Connection: close\r\n"); out.write("\r\n"); out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET)); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } } finally { socket.close(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String serverName = args[0];//ww w . j a v a2s.co m int port = Integer.parseInt(args[1]); try { System.out.println("Connecting to " + serverName + " on port " + port); Socket client = new Socket(serverName, port); System.out.println("Just connected to " + client.getRemoteSocketAddress()); OutputStream outToServer = client.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer); out.writeUTF("Hello from " + client.getLocalSocketAddress()); InputStream inFromServer = client.getInputStream(); DataInputStream in = new DataInputStream(inFromServer); System.out.println("Server says " + in.readUTF()); client.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:SSLSimpleClient.java
public static void main(String[] args) throws Exception { SocketFactory sf = SSLSocketFactory.getDefault(); Socket s = sf.createSocket(args[0], Integer.parseInt(args[1])); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream()); pw.println("from java2s."); pw.flush();/*from w w w .j a v a 2 s . c o m*/ System.out.println(br.readLine()); s.close(); }