zoukankan      html  css  js  c++  java
  • 用socket通讯写出客户端和服务器端的通讯

    Server.java:源代码

       import java.net.*;
    import java.io.*;

    public class Server {

               public Server()

               {

                      BufferedReader br = null;

                      PrintWriter pw = null;

                      try

                      {

                             ServerSocket server = new ServerSocket(8888);//建立服务器端

                             Socket socket = server.accept();//监听客户端

                             //得到该连接的输入流

                             br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                             //得到该连接的输出流

                             pw = new PrintWriter(socket.getOutputStream(),true);

                             //先读后写

                             String data = br.readLine();

                             System.out.println(data);//输出到控制台

                             pw.println(data);//转发给客户端

                      }catch(Exception e)

                      {

                             e.printStackTrace();

                      }

                      finally

                      {

                             try

                             {

                                    //关闭读写流

                                    br.close();

                                    pw.close();

                             }catch(Exception e)

                             {}

                      }

               }

               public static void main(String[] args)

               {

                      Server server = new Server();

               }

    }

    Client.java:源代码

    import java.net.*;
    import java.io.*;
    class Client

    {

         public Client()

         {

                BufferedReader br = null;

                PrintWriter pw = null;
                Socket socket=null;

                try

                {

                socket = new Socket("localhost",8888);//与服务器建立连接,服务器要先启
               

                
                
               

                boolean status = true;
                int j=0;
                   while(status){
                       try{
                       
                           System.out.println("abc");
                        //status = false;sendUrgentData
                           socket.sendUrgentData(0xFF);
                           Thread.sleep(2000);
                           if(j>0){
                           status=false;
                           }
                           j++;
                       }catch(Exception e1){
                        //e1.printStackTrace();
                           System.out.println("服务器断开!!");
                           //status = true;
                           Thread.sleep(2000);
                           socket.close();
                           j=0;
                           socket = new Socket("localhost",8888);//与服务器建立连接,服务器要先启
                       }
                   }
                   System.out.println("服务器连接了!!");
                //得到Socket的输入与输出流

                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                pw = new PrintWriter(socket.getOutputStream(),true);

                //先写后读

                pw.println("Client:你好!");

                String data = null;

                while(true)

                {

                 Thread.sleep(100);
                      if(( data = br.readLine())!=null){

                       if(data!=null) break;
                      }

                }

                System.out.println(data);

                }catch(Exception e)

                {

                       e.printStackTrace();

                }

                finally

                {

                       try

                       {

                              br.close();
                              pw.close();
                              if(socket!=null)
                              socket.close();

                       }catch(Exception e)

                       {}

                }

         }

         public static void main(String[] args)

         {

                Client c = new Client();

         }
    }

  • 相关阅读:
    【SQL】通过Sql实现根据分组合并指定列内容的查询 SamWang
    [转]SQLServer中全文搜索与Like的差异分析
    【笔记】《C#高效编程改进C#代码的50个行之有效的办法》第1章C#语言习惯(1)属性的特性以及索引器(SamWang)
    [转]SqlServer全文索引实例
    [转]visual studio2005中文版无法打开项目文件:*.csproj,提示“此安装不支持该项目类型”的解决办法
    [转]李天平:程序人生成长发展中的一些感悟
    【改进】C# WinForm捕获全局异常 SamWang
    Windows 7 虚拟无线网卡(microserof virtual wifi miniport adapter)功能的使用
    VB.Net 串口通信示例
    VB.Net 串口通信用法
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1783077.html
Copyright © 2011-2022 走看看