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

📄 java_socket.txt

📁 linux下java_socket详解
💻 TXT
字号:
server:   
    
  import   java.net.*;   
  import   java.io.*;   
  import   java.lang.*;   
    
  public   class   myServer{   
          public   static   void   main(String   args[]){   
                  ServerSocket   server;   
                  Socket   socket;   
                  String   s;   
                  InputStream   Is;   
                  OutputStream   Os;   
                  DataInputStream   DIS;   
                  PrintStream   PS;   
    
                  try{   
                          //在端口注册服务   
                          server=new   ServerSocket(9000);   
                          socket=server.accept();   //监听窗口,等待连接   
    
                          System.out.println("server   ok");   
                          System.out.println("************************************************");   
                          System.out.println("");   
    
                          //获得对应Socket的输入/输出流   
                          Is=socket.getInputStream();   
                          Os=socket.getOutputStream();   
                          //建立数据流   
                          DIS=new   DataInputStream(Is);   
                          PS=new   PrintStream(Os);   
                          DataInputStream   in=new   DataInputStream(System.in);   
                          while(true){   
                                  System.out.println("");   
                                  System.out.println("please   wait   client's   message...");   
                                  System.out.println("");   
                                  s=DIS.readLine();   //读入从client传来的字符串   
                                  System.out.println("client   said:"+s);   //打印字符串   
                                  if(s.trim().equals("BYE"))break;   //如果是"BYE",就退出   
                                  System.out.print("you   say:");   
                                  s=in.readLine();   //读取用户输入的字符串   
                                  PS.println(s);   //将读取得字符串传给client   
                                  if(s.trim().equals("BYE"))break;   //如果是"BYE",就退出   
    
                          }   
    
                          //关闭连接   
                          DIS.close();   //关闭数据输入流   
                          PS.close();   //关闭数据输出流   
                          Is.close();   //关闭输入流   
                          Os.close();   //关闭输出流   
                          socket.close();   //关闭sockey   
                  }   
                  catch(Exception   e){   
                          System.out.println("Error:"+e);   
                  }   
          }   
  }   
    
    
  client:   
    
  import   java.net.*;   
  import   java.io.*;   
  import   java.lang.*;   
    
  public   class   myClient{   
          public   static   void   main(String   args[]){   
                  String   host   =   "";   
    
                  Socket   socket;   
                  String   s="";   
                  String   len;   
                  InputStream   Is;   
                  OutputStream   Os;   
                  DataInputStream   DIS;   
                  DataOutputStream   DOS;   
                  PrintStream   PS;   
                  try{   
                          socket=new   Socket(host,9000);   
    
                          System.out.println("client   ok");   
                          System.out.println("************************************************");   
                          System.out.println("");   
    
                          //获得对应socket的输入/输出流   
                          Is=socket.getInputStream();   
                          Os=socket.getOutputStream();   
                          //建立数据流   
                          DIS=new   DataInputStream(Is);   
                          DOS=new   DataOutputStream(Os);   
                          PS=new   PrintStream(Os);   
                          DataInputStream   in=new   DataInputStream(System.in);   
                          while(true){   
                                  System.out.print("you   say:");   
                                  s=in.readLine();   //读取用户输入的字符串   
                                  PS.println(s);   //将读取得字符串传给server   
                                  //DOS.write(s.getBytes());   
                                  if(s.trim().equals("BYE"))break;   //如果是"BYE",就退出   
                                  else   
                                  {   
                                          System.out.println("");   
                                          System.out.println("please   wait   server's   message...");   
                                          System.out.println("");   
                                  }   
                                  s=DIS.readLine();   //从服务器获得字符串   
                                  System.out.println("server   said:"+s);   //打印字符串   
                                  if(s.trim().equals("BYE"))break;   //如果是"BYE",就退出   
    
                          }   
    
                          //关闭连接   
                          DIS.close();   //关闭数据输入流   
                          PS.close();   //关闭数据输出流   
                          Is.close();   //关闭输入流   
                          Os.close();   //关闭输出流   
                          socket.close();   //关闭socket   
                  }   
                  catch(Exception   e){   
                          System.out.println("Error:"+e);   
                  }   
          }   
  }   

⌨️ 快捷键说明

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