多线程Socket编程

服务端代码:

package server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerThread{

 private ServerSocket ss ;
 private Socket s ;
 private BufferedReader in ;
 private BufferedReader sys ;
 private PrintWriter out ;
 public static int PORT = 10000 ;
 public ServerThread(){
  try{
   ss = new ServerSocket(PORT) ;
   s = ss.accept() ;

   //读取网络流的BufferedReader
   in = new BufferedReader(new InputStreamReader(s.getInputStream())) ;

  //输入网络流PrintWriter
   out = new PrintWriter(s.getOutputStream());

  //控制台流
   sys = new BufferedReader(new InputStreamReader(System.in)) ;
  }catch(Exception ex){
   ex.printStackTrace() ;
  }
 }

//读取客户端流方法
 public void read()throws Exception{
   while(true){
    String str = in.readLine() ;
    System.out.println("客户端回复:\n"+str) ;
    if(str.equals("end")){
     break ;
    }
   }
  
 }

//往网络流输入方法
 public void write()throws Exception{
   while(true){
    String str = sys.readLine() ;
    out.println(str) ;
    out.flush() ;
   }
 }
 public static void main(String []args)throws Exception{
  ServerThread s = new ServerThread() ;
  new ReadThread(s).start() ;
  new WriteThread(s).start() ;
 }
}

//创建一个输入网络流线程的类
class WriteThread extends Thread{
  
  private ServerThread server ;
   public WriteThread(ServerThread server){
    this.server = server ;
   }
   public void run(){
    try{
     server.write() ;
    }catch(Exception ex){
     ex.printStackTrace() ;
    }
   }
}

//创建一个读取网络流线程的类
class ReadThread extends Thread{
   
   private ServerThread server ;
   public ReadThread(ServerThread server){
    this.server = server ;
   }
   public void run(){
    try{
     server.read() ;
    }catch(Exception ex){
     ex.printStackTrace() ;
    }
   }
}

客户端代码:

package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class ClinetThread {

   private BufferedReader in ;
   private BufferedReader sys ;
   private PrintWriter out ;
   private Socket s ;
   public static int PORT = 10000 ;
   public ClinetThread(){
    try{
     s = new Socket(InetAddress.getLocalHost(),PORT) ;
     in = new BufferedReader(new InputStreamReader(s.getInputStream())) ;
     sys = new BufferedReader(new InputStreamReader(System.in)) ;
     out = new PrintWriter(s.getOutputStream()) ;
    }catch(Exception ex){
     ex.printStackTrace() ;
    }
  
   }
   public void read()throws Exception{
    while(true){
     String read = in.readLine() ;
     if(read.equals("end")){
      break ;
     }
     System.out.println("服务端回复:\n"+read) ;
    }
   }
   public void write()throws Exception{
    while(true){
     String write = sys.readLine() ;
     out.println(write) ;
     out.flush() ;
    }
   }
   public static void main(String []args){
    ClinetThread client = new ClinetThread() ;
    new ReadThread(client).start() ;
    new WriteThread(client).start() ;
   }
}
class ReadThread extends Thread{
 ClinetThread client ;
 public ReadThread(ClinetThread client){
  this.client = client ;
 }
 public void run(){
  try{
   client.read() ;
  }catch(Exception ex){
   ex.printStackTrace() ;
  }
 }
}
class WriteThread extends Thread{
 ClinetThread client ;
 public WriteThread(ClinetThread client){
  this.client = client ;
 }
 public void run(){
  try{
   client.write() ;
  }catch(Exception ex){
   ex.printStackTrace() ;
  }
 }
}

你可能感兴趣的:(多线程Socket编程)