标签:
第一个例子创建了一个java工程,用来测试Socket的连接功能,通过浏览器可访问,地址为:127.0.0.1:端口号
MyServerSocket.java,
package com.jikexueyuan.testmysetversocket.main; public class MyServerSocket { public static void main(String[] args) { new ServerListener().start(); } }
ServerListener.java
1 package com.jikexueyuan.testmysetversocket.main; 2 3 import java.io.IOException; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 7 import javax.swing.JOptionPane; 8 9 public class ServerListener extends Thread { 10 11 @Override 12 public void run() { 13 //1-65535 14 try { 15 ServerSocket serverSocket = new ServerSocket(12345); 16 while (true) { 17 //block 18 Socket socket = serverSocket.accept(); 19 //建立连接 20 JOptionPane.showMessageDialog(null, "有客户端链接到了本机的12345端口"); 21 //将socket传递给新的线程 22 ChatSocket cs = new ChatSocket(socket); 23 cs.start(); 24 ChatManager.getChatManager().add(cs); 25 } 26 27 } catch (IOException e) { 28 e.printStackTrace(); 29 } 30 } 31 }
ChatSocket.java
1 package com.jikexueyuan.testmysetversocket.main; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 import java.io.UnsupportedEncodingException; 9 import java.net.Socket; 10 11 public class ChatSocket extends Thread { 12 13 Socket socket; 14 15 public ChatSocket(Socket s){ 16 this.socket = s; 17 } 18 19 public void out(String out) { 20 try { 21 socket.getOutputStream().write((out+"\n").getBytes("UTF-8")); 22 } catch (UnsupportedEncodingException e) { 23 e.printStackTrace(); 24 } catch (IOException e) { 25 System.out.println("断开了一个客户端链接"); 26 ChatManager.getChatManager().remove(this); 27 e.printStackTrace(); 28 } 29 } 30 31 @Override 32 public void run() { 33 out("你已经连接到本服务器了"); 34 try { 35 BufferedReader br = new BufferedReader( 36 new InputStreamReader( 37 socket.getInputStream(),"UTF-8")); 38 String line = null; 39 while ((line = br.readLine()) != null) { 40 System.out.println(line); 41 ChatManager.getChatManager().publish(this, line); 42 } 43 br.close(); 44 System.out.println("断开了一个客户端链接"); 45 ChatManager.getChatManager().remove(this); 46 } catch (UnsupportedEncodingException e) { 47 e.printStackTrace(); 48 } catch (IOException e) { 49 System.out.println("断开了一个客户端链接"); 50 ChatManager.getChatManager().remove(this); 51 e.printStackTrace(); 52 } 53 54 } 55 }
ChatManager.java
1 package com.jikexueyuan.testmysetversocket.main; 2 3 import java.util.Vector; 4 5 public class ChatManager { 6 7 private ChatManager(){} 8 private static final ChatManager cm = new ChatManager(); 9 public static ChatManager getChatManager() { 10 return cm; 11 } 12 13 Vector<ChatSocket> vector = new Vector<ChatSocket>(); 14 15 public void add(ChatSocket cs) { 16 vector.add(cs); 17 } 18 19 public void remove(ChatSocket cs) { 20 vector.remove(cs); 21 } 22 23 public void publish(ChatSocket cs,String out) { 24 for (int i = 0; i < vector.size(); i++) { 25 ChatSocket csChatSocket = vector.get(i); 26 if (!cs.equals(csChatSocket)) { 27 csChatSocket.out(out); 28 } 29 } 30 } 31 }
打开浏览器后,访问地址得到:
标签:
原文地址:http://www.cnblogs.com/UniqueColor/p/5725602.html