Java Socket 网络编程
Posted 一盏碧螺春
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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) {} } } } }
以上是关于Java Socket 网络编程的主要内容,如果未能解决你的问题,请参考以下文章