标签:
Socket是建立在TCP/IP上的API,连接需实现三次握手。一旦连接建立,服务端和客户端便可开始相互发送数据,直到一方断开连接为止。
Http连接使用的是“请求—响应”的方式,每次请求都需要服务器回送响应,在请求结束后,会主动释放连接。从建立连接到关闭连接的过程称为“一次连接”。
Socket:长连接。多用于操作频繁(握手耗时),点对点通讯,且连接数不太多情况,如数据库连接。通过心跳连接确定是否存活,方便清理死连接。
Http:短连接。多用于并发数多的情况,如Web网站。这样可以节省服务器的资源,减小压力。
Socket实例:
1 package com.hailin.demo; 2 3 import java.awt.BorderLayout; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import java.io.DataInputStream; 7 import java.io.DataOutputStream; 8 import java.io.IOException; 9 import java.net.Socket; 10 import java.util.Date; 11 12 import javax.swing.JFrame; 13 import javax.swing.JLabel; 14 import javax.swing.JPanel; 15 import javax.swing.JScrollPane; 16 import javax.swing.JTextArea; 17 import javax.swing.JTextField; 18 19 public class Client extends JFrame { 20 21 private JTextField jtf = new JTextField(); 22 private JTextArea jta = new JTextArea(); 23 24 private DataOutputStream toServer; 25 private DataInputStream fromServer; 26 27 public static void main(String[] args) { 28 new Client(); 29 } 30 31 public Client() { 32 33 JPanel p = new JPanel(); 34 p.setLayout(new BorderLayout()); 35 p.add(new JLabel("Enter radius: "), BorderLayout.WEST); 36 p.add(jtf, BorderLayout.CENTER); 37 jtf.setHorizontalAlignment(JTextField.RIGHT); 38 39 setLayout(new BorderLayout()); 40 add(p, BorderLayout.NORTH); 41 add(new JScrollPane(jta), BorderLayout.CENTER); 42 43 jtf.addActionListener(new ButtonListener()); 44 45 setTitle("Client"); 46 setSize(500, 300); 47 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 48 setVisible(true); 49 50 try { 51 52 Socket socket = new Socket("10.197.32.98", 8000); 53 54 fromServer = new DataInputStream(socket.getInputStream()); 55 toServer = new DataOutputStream(socket.getOutputStream()); 56 57 } catch (IOException ex) { 58 System.err.println(ex); 59 jta.append("Server isn‘t started at " + new Date() + ‘\n‘); 60 jta.append(ex.toString() + ‘\n‘); 61 } 62 63 } 64 65 private class ButtonListener implements ActionListener { 66 public void actionPerformed(ActionEvent e) { 67 try { 68 69 double radius = Double.parseDouble(jtf.getText().trim()); 70 71 toServer.writeDouble(radius); 72 toServer.flush(); 73 74 double area = fromServer.readDouble(); 75 76 jta.append("Radius is " + radius + ‘\n‘); 77 jta.append("Area recevied from the server is " + area + ‘\n‘); 78 79 } catch (IOException ex) { 80 System.err.println(ex); 81 jta.append("Server is closed at " + new Date() + ‘\n‘); 82 jta.append(ex.toString() + ‘\n‘); 83 } 84 } 85 } 86 87 }
package com.hailin.demo; import java.awt.BorderLayout; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class MultiThreadServer extends JFrame{ private JTextArea jta = new JTextArea(); public static void main(String[] args) { new MultiThreadServer(); } public MultiThreadServer() { setLayout(new BorderLayout()); add(new JScrollPane(jta), BorderLayout.CENTER); setTitle("MultiThreadServer"); setSize(500, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); try { ServerSocket serverSocket = new ServerSocket(8000); jta.append("Server started at " + new Date() + ‘\n‘); int clientNo = 1; while (true) { Socket socket = serverSocket.accept(); jta.append("Starting thread fo client " + clientNo + " at " + new Date() + ‘\n‘); InetAddress inetAddress = socket.getInetAddress(); jta.append("Client " + clientNo + "‘s host name is " + inetAddress.getHostName() + ‘\n‘); jta.append("Client " + clientNo + "‘s IP address is " + inetAddress.getHostAddress() + ‘\n‘); HandleAClient task = new HandleAClient(socket, clientNo); new Thread(task).start(); clientNo++; } } catch (IOException ex) { System.err.println(ex); } } class HandleAClient implements Runnable { private Socket socket; private int clientNo; public HandleAClient(Socket socket, int clientNo) { this.socket = socket; this.clientNo = clientNo; } public void run() { try { DataInputStream inputFromClient = new DataInputStream(socket.getInputStream()); DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream()); while (true) { double radius = inputFromClient.readDouble(); double area = radius * radius * Math.PI; outputToClient.writeDouble(area); jta.append("Radius received from client: " + radius + ‘\n‘); jta.append("Area found: " + area + ‘\n‘); } } catch (IOException ex) { System.err.println(ex); jta.append("Client " + clientNo + " is closed unexpectly at " + new Date() + ‘\n‘); jta.append(ex.toString() + ‘\n‘); } } } }
标签:
原文地址:http://www.cnblogs.com/RussellWestbrook/p/4259370.html