zoukankan      html  css  js  c++  java
  • Socket 服务器和客户端通信


    //服务器端
    package com.svse.service; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket;
    public class Socket { public static void main(String[] args) { Socket socket=new Socket(); socket.start(); } public void start(){ ServerSocket serverSocket; BufferedReader reader; try { serverSocket=new ServerSocket(9898); java.net.Socket socket=serverSocket.accept(); reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); String receivedMsg; while ((receivedMsg=reader.readLine())!=null) { System.out.println(receivedMsg); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

    //客户端
    package com.svse.client; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter;
    public class Socket { public static void main(String[] args) { Socket socket=new Socket(); socket.start(); } public void start(){ BufferedReader inputReader; BufferedWriter writer; try { java.net.Socket socket=new java.net.Socket("127.0.0.1", 9898); writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); inputReader=new BufferedReader(new InputStreamReader(System.in)); String inputContent; while (!(inputContent=inputReader.readLine()).equals("bye")) { writer.write(inputContent+" "); writer.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

    PS.懒,不要学我,要按规范。

  • 相关阅读:
    【转】Intel Atom手机处理器“上窜下跳”
    神奇HVXC的MOS 分
    Skype 将支持 WebRTC 标准
    一篇文章算市值
    算法经典趣题三色旗
    Java基础(2)Java三大版本/体系
    一天一个 Linux 命令(27):mkfs 命令
    Java基础(1)Java特性及优势
    一天一个 Linux 命令(26):fdisk 命令
    关于Java中的整数类型值比较的疑问
  • 原文地址:https://www.cnblogs.com/stareblankly/p/5007575.html
Copyright © 2011-2022 走看看