⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lesson10.java~6~

📁 孙鑫讲解的java教程ppt 有益于java的学习
💻 JAVA~6~
字号:
package lesson10;

import java.net.*;
import java.io.*;
public class Lesson10 extends Thread
{
  private Socket s;
  public Lesson10(Socket s)
  {
    this.s=s;
  }
  public void run()
  {
    try {
      OutputStream os=s.getOutputStream();
      BufferedOutputStream bos=new BufferedOutputStream(os);
      InputStream is=s.getInputStream();
//      os.write("Hello,welcome you!".getBytes());
      bos.write("Hello,welcome you!".getBytes());
      byte[] buf=new byte[100];
      int len=is.read(buf);
      System.out.println(new String(buf,0,len));
      //os.close();
      //is.close();
      //s.close();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  public static void main(String[] args)
  {
    if(args.length>0)
      server();
    else
      client();
  }
  public static void server()
  {
    try {
      ServerSocket ss=new ServerSocket(6000);
      while(true)
      {
        Socket s = ss.accept();
        new Lesson10(s).start();
      }
      //ss.close();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  public static void client()
  {
    try {
      Socket s=new Socket(InetAddress.getByName(null),6000);
      OutputStream os=s.getOutputStream();
      InputStream is=s.getInputStream();
      byte[] buf=new byte[100];
      int len=is.read(buf);
      System.out.println(new String(buf,0,len));
      os.write("Hello,this is wangwu".getBytes());
      os.close();;
      is.close();
      s.close();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

  }
}
















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -