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

    *结束一定要关流

  • 相关阅读:
    修改mysql root账户登录密码
    taglib的uri问题
    encoding/path可能引起无数奇怪的问题
    查看JSTL的doc解决问题
    matlab 读取nc
    matlab fread
    用matlab将nc数据读出来,写成二进制文件,然后用grads画图
    matlab 三维绘制
    flex label 换行
    Struts2的使用以及Spring整合Struts2
  • 原文地址:https://www.cnblogs.com/joyous-day/p/7794218.html
Copyright © 2011-2022 走看看