本文共 4843 字,大约阅读时间需要 16 分钟。
// 1.创建Socket连接,告知 的IP地址以及端口号 Socket socket = new Socket("127.0.0.1", 4242); // 2.创建InputStreamReader,用于读取socket输入流 InputStreamReader stream = new InputStreamReader(socket.getInputStream()); // 3.使用BufferedReader链接输入流 BufferedReader br = new BufferedReader(stream); // 4.读出数据 String line = null; while ((line = br.readLine()) != null) { System.out.println("Today's advice is: " + line); } // 5. 关闭输入流BufferedReader br.close(); 2.2 向Scoket写入数据步骤: // 1.创建Socket连接,告知Server的IP地址以及端口号 Socket socket = new Socket("127.0.0.1", 4242); // 2.创建PrintWriter对象,用以接收socket输出流 PrintWriter writer = new PrintWriter(socket.getOutputStream()); // 3.使用PrintWriter对象写出输出数据 String advice ="Today's advice"; writer.println(advice); // 4. 关闭连接 writer.close(); 3. 服务器端(Server)Socket的使用 // 1.创建一个SercerSocket,使用4242端口监听客户端请求 ServerSocket serverSocket = new ServerSocket(4242); System.out.println("The server is started, listening on port 4242"); while (true) { // 2.ServerSocket的accept()在等待用户连接的时候闲置;在用户连接上来的时候,返回一个Socket来与客户端通信 Socket socket = serverSocket.accept(); // 3.创建PrintWriter对象,用以接收socket输出流 PrintWriter writer = new PrintWriter(socket.getOutputStream()); // 4.使用PrintWriter对象写出输出数据 String advice = "notifier's blog"; writer.println(advice); // 5. 关闭连接 writer.close(); } |
public void function() { doSomething(); //以下方法需要同步化 synchronized (this) { doCriticalStuff(); moreCriticalStuff(); } doSomeOtherThing(); } |
/** * @author notifier * @create 2010-9-25 上午10:12:10 * @version 1.0 */ public class DailyAdviceClient { public static void main(String[] args) { DailyAdviceClient client = new DailyAdviceClient(); client.receiveMsg(); } public void receiveMsg() { try { // 1.创建Socket连接,告知Server的IP地址以及端口号 Socket socket = new Socket("127.0.0.1", 4242); // 2.创建InputStreamReader,用于读取socket输入流 InputStreamReader stream = new InputStreamReader(socket .getInputStream()); // 3.使用BufferedReader链接输入流 BufferedReader br = new BufferedReader(stream); // 4.读出数据 String line = null; while ((line = br.readLine()) != null) { System.out.println("Today's advice is: " + line); } // 5. 关闭输入流BufferedReader br.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
/** * @author notifier * @create 2010-9-25 下午07:06:54 * @version 1.0 */ public class SimpleChatServer { // 保存客户端列表 private ArrayList clientList = new ArrayList();; public static void main(String[] args) { new SimpleChatServer().startUp(); } /** * 负责服务器端的启动 * */ public void startUp() { try { // 创建服务器端ServerSocket连接,监听端口号5000 ServerSocket serverSocket = new ServerSocket(5000); // 轮询等待客户端请求 while(true) { // 等待客户端请求,无请求则闲置;有请求到来时,返回一个对该请求的socket连接 Socket clientSocket = serverSocket.accept(); // 将该客户端加入到列表中 PrintWriter writer = new PrintWriter(clientSocket.getOutputStream()); clientList.add(writer); // 创建ClientHandler对象,通过socket连接通信 Thread t = new Thread(new ClientHandler(clientSocket)); t.start(); System.out.println("有Client连进来"); } }catch(Exception e) { e.printStackTrace(); } } /** * 客户端处理类, 主要负责: * 1.接收客户端发来的消息 * 2.将消息转发其他客户端 * @author sdniu * @create 2010-9-26 上午10:00:18 * @version 1.0 */ public class ClientHandler implements Runnable { private BufferedReader reader; private Socket socket; /** * ClientHandler的构造函数 * @param clientSocket */ public ClientHandler(Socket clientSocket) { try { // 得到socket连接 socket = clientSocket; // 得到客户端发来的消息 InputStreamReader isReader = new InputStreamReader(socket.getInputStream()); reader = new BufferedReader(isReader); } catch (IOException e) { e.printStackTrace(); } } public void run() { String message; try { while((message = reader.readLine()) != null) { System.out.println("客户端消息: " + message); // 将客户端发来的消息转发所有客户端 notifyAllClients(message); } } catch (IOException e) { e.printStackTrace(); } } } /** * * @param message */ public void notifyAllClients(String message) { // 得到客户端列表的迭代器,语法格式为 Iterator it = clientList.iterator(); Iterator it = clientList.iterator(); while(it.hasNext()) { try { // 得到的Iterator别忘了强制转换回PrintWriter PrintWriter writer = (PrintWriter) it.next(); writer.println(message); writer.flush(); } catch (Exception e) { e.printStackTrace(); } } } } |
转载地址:http://gkxtx.baihongyu.com/