zoukankan      html  css  js  c++  java
  • Server and Client

    启动时,先启动server,在启动client

    Server

    package test;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class server {
    public static void main(String[] args) throws IOException {
    ServerSocket s =new ServerSocket(8888);
    Socket s1 = s.accept();
    InputStream inp=s1.getInputStream();
    OutputStream outp=s1.getOutputStream();
    byte[] b=new byte[1024];
    int len;
    len =inp.read(b);
    String str1=new String(b,0,len);
    System.out.println(str1);
    if(str1.equals("Hell Server")){
    outp.write("Hello client".getBytes());
    }
    len =inp.read(b);
    String str2=new String(b,0,len);
    System.out.println(str2);
    if(str2.equals("Bye")){
    outp.write("Bye".getBytes());
    s.close();
    }
    }
    }

    Client

    package test;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;

    public class client {
    public static void main(String[] args) throws UnknownHostException, IOException {
    Socket s=new Socket("192.168.123.177",8888);
    InputStream inp = s.getInputStream();
    OutputStream outp = s.getOutputStream();
    outp.write("Hell Server".getBytes());
    byte[] b=new byte[1024];
    int len=0;
    len =inp.read(b);
    String str1=new String(b,0 , len);
    System.out.println(str1);
    if (str1.equals("Hello client")) {
    outp.write("Bye".getBytes());
    }
    len =inp.read(b);
    String str2 = new String(b,0,len);
    System.out.println(str2);
    if(str2.equals("Bye")){
    s.close();
    }
    }
    }

    *结束一定要关流

  • 相关阅读:
    manjora20安装搜狗输入法
    manjora20使用体验
    deepin20体验
    deepin20搜狗输入法使用
    deepin20使用snap并设置代理
    C#服务器端使用office组件
    华为多屏互动看学英语
    ThinkPad S5立体声混响以及语音识别
    mate10碎屏机当成小电脑使用尝试
    刷机错误ERROR:STATUS_BROM_CMD__FAIL
  • 原文地址:https://www.cnblogs.com/joyous-day/p/7794218.html
Copyright © 2011-2022 走看看