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

     }
}

原文地址:https://www.cnblogs.com/liufei88866/p/1783077.html