Java Socket 网络编程
1. 简单的由服务端发送内容到客户端:
Server:
package test; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class SocketServer { private static final int PORT = 8088; public static void main(String[] args) { ServerSocket server = null; try { server = new ServerSocket(PORT); while (true) { Socket client = server.accept(); new Thread(new Server(client)).start(); } } catch (IOException e) { e.printStackTrace(); } } } class Server implements Runnable { private Socket client; public Server(Socket client) { this.client = client; } public void run() { DataOutputStream output = null; try { output = new DataOutputStream(client.getOutputStream()); output.writeUTF("XXOOXXOOXXOO"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (output != null) output.close(); output = null; } catch (IOException e) {} } } }
client:
package test; import java.io.*; import java.net.Socket; import java.net.UnknownHostException; public class SSLClient extends Socket { private static final int PORT = 8088; public static void main(String[] args) { Socket socket = null; try { socket = new Socket("localhost", PORT); DataInputStream in = new DataInputStream(socket.getInputStream()); String res = in.readUTF(); System.out.println(res); if (in != null) in.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) {} } } } }
2. server 和 client 沟通通讯:
server:
package test; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class SocketServer { private static final int PORT = 8088; public static void main(String[] args) { // ServerSocket server = null; try { ServerSocket server = new ServerSocket(PORT); Socket socket = server.accept(); // new Thread(new Server(socket)).start(); InputStream msg = socket.getInputStream(); InputStreamReader read = new InputStreamReader(msg); BufferedReader br = new BufferedReader(read); String info = null; while ((info = br.readLine())!= null) { System.out.println("I am server, the client says: " + info); } socket.shutdownInput(); OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.write("Welcome!"); pw.flush(); pw.close(); os.close(); br.close(); read.close(); msg.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
client:
package test; import java.io.*; import java.net.Socket; import java.net.UnknownHostException; public class SSLClient extends Socket { private static final int PORT = 8088; public static void main(String[] args) { Socket socket = null; try { socket = new Socket("localhost", PORT); OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.write("user: admin; passwd: 123"); pw.flush(); socket.shutdownOutput(); InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String info = null; while((info=br.readLine()) != null){ System.out.println("I am client, server says: " + info); } br.close(); is.close(); pw.close(); os.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) {} } } } }