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();
    }
    }
    }

    *结束一定要关流

  • 相关阅读:
    如何选择开源许可证?(转)
    gdb的使用(转)
    git的使用
    2017-3-13 leetcode 4 11 15
    2017-3-12 leetcode 167 209 216
    2017-3-11 leetcode 217 219 228
    2017-3-10 leetcode 229 238 268
    1175: 零起点学算法82——find your present
    1174: 零起点学算法81——求整数绝对值
    1173: 零起点学算法80——求实数绝对值
  • 原文地址:https://www.cnblogs.com/joyous-day/p/7794218.html
Copyright © 2011-2022 走看看