TCP基本操作

服务端
/** 导包 */
public  class ServerDemo{
    public static void main(String[] args) throws Exception{
    //1.创建服务,监听一个窗口
    ServerSocket ss = new ServerSocket(4372);   
    Socket s = ss.accept();   

    //输入流
      DataInputStream dis = new DataInputStream(s.getInputStream());
      DataOutputStream dos = new DataOutputStream(s.getOutputStream());
      while(true) {
                  String msg = dis.readUTF();
                  System.out.println("服务器已收到!" +msg);
       //输出流
                  dos.writeUTF( msg);
                  dos.flush();
       }
    }
}

   客户端
/** 导包 */

 public  class ClientDemo {
          public static void main(String[] args) throws UnknownHostException, IOException {                                                                 Socket s = new Socket("localhost", 4372);  

                  new Thread(new SendDemo(s)).start();
                  new Thread(new ReceDemo(s)).start();  
        } 
   }
 
/**
 *  接受类
 */
/** 导包 */

public class ReceiveDemo implements Runnable{

    //输入流
    private  DataInputStream dis;
    //线程标识
    private boolean isRunning = true;
  public ReceiveDemo() {
  }
 public ReceiveDemo(Socket  socket) {
   try {
     dis = new DataInputStream(socket.getInputStream());
   } catch (IOException e) {
    e.printStackTrace();
    isRunning = false;
    CloseUtil.closeAll(dis);
   }
  }
  /**
   * 接受数据方法
   * @return
   */
  public String receive() {
   String msg = "";
   try {
     msg = dis.readUTF();
   } catch (IOException e) {
    e.printStackTrace();
    isRunning = false;
    CloseUtil.closeAll(dis);
   }
   return msg;
  }
  @Override
  public void run() {
   while (isRunning) {
    receive();
    }
   }  
 }

/**
 * 发送类
 */
/** 导包 */

public class SendDemo implements Runnable {

   //控制台输入流
   private BufferedReader br ;
   //管道输出流
   private DataOutputStream dos;
   //线程是否正常运行,默认为true
   private boolean isRunning = true;
  
   public SendDemo (){
    br = new BufferedReader(new InputStreamReader(System.in));
   }
  
   public SendDemo (Socket s) {
    this();
    try {
    dos = new DataOutputStream(s.getOutputStream());
  } catch (IOException e) {
   e.printStackTrace();
   isRunning  = false;
   CloseUtil.closeAll(dos,br);
  }
   }
   /**
    * 从控制台接受数据
    */
   private String getMsgFromConsle() {
    try {
    return  br.readLine();
  } catch (IOException e) {   
   e.printStackTrace();
  }
    return "";
   }
   /**
    * 输出数据
    */
    private void sendMsg() {
     String msg = getMsgFromConsle();
     if(msg != null && !msg.equals("")) {
      try {
     dos.writeUTF(msg);
    dos.flush();

   } catch (IOException e) {
    e.printStackTrace();
    isRunning  = false;
    CloseUtil.closeAll(dos,br);
   }
     }
    }
 @Override
 public void run() {
  while(isRunning) {
   sendMsg();
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值