服务器与客户端通信

 import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * 可以使用PrintStream 可 PrintWriter 来封装OutputStream
 * @author MSSOFT
 *
 */
public class ServerDemo {
 public static void main(String[] args) throws IOException{
  ServerDemo server = new ServerDemo();
  server.start();
 }

 public void start() throws IOException{
  ServerSocket ss = new ServerSocket(8000);
  while(true){
   System.out.println("等待客户连接。。。。");
   Socket s = ss.accept();//当连接到一个客户时,就会通过accept()方法返回一个socket对象。
   System.out.println("客户连接成功: "+s.getInetAddress());
   new Service(s).start();//派发服务线程,处理客户的服务
  }
 }
 
 class Service extends Thread{
  Socket s;
  public Service(Socket s){
   this.s = s;
  }
  public void run(){
   try{
   //in代表客户端送来的信息
   InputStream in = s.getInputStream();
   //out 代表向客户端发送的消息
   OutputStream os = s.getOutputStream();
   PrintStream out = new PrintStream(os,true);
//   out.write("你想吃点啥啊?\n".getBytes());
   out.println("你想吃点啥啊?");
   out.flush();
   Scanner s = new Scanner(in);
//   byte[] buf = new byte[1024];
   while(true){
    String str = s.nextLine().trim();
//    int n = in.read(buf);
//    String str = new String(buf,0,n);
    System.out.println("client input data :  "+str);
    if(str.equals("粗面")){
//     out.write("没有。\n".getBytes());
     out.println("没有.");
    }else if(str.equals("包子")){
//     out.write("有,给你。\n".getBytes());
     out.println("有,给你,抓住。");
//     out.flush();
    }else {
//     out.write("你说啥?\n".getBytes());
     out.println("你说啥呢,亲?");
//     out.flush();
    }
   }
//   s.close();
   }catch(Exception e){
//    e.printStackTrace();
    RuntimeException re = new RuntimeException("出错了", e);
    throw re;
   }
  }
 }
}

 

 

客户端代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ClientDemo {
 public static void main(String[] args) throws IOException{
  new ClientDemo().open();
 }

 public void open() throws IOException {
  // new Socket("localhost",8000)会发起向localhost:8000的连接
  // localhost是服务器的域名/ip,localhhost在IP协议中是当前计算机的域名,
  // 会映射到127.0.0.1
  Socket s = new Socket("localhost", 8000);
  // 如果连接成功,会创建Socket的实例对象S,不成功会抛出异常。
  // in 代表服务器端到客户端的流
  InputStream in = s.getInputStream();
  new Writer(in).start();
  //  
  OutputStream out = s.getOutputStream();
  new Reader(out).start();
 }

 // Reader负责 将读取控制台的信息,写到服务器端(out)
 class Reader extends Thread {
  OutputStream out;
  public Reader(OutputStream out) {
   this.out = out;
  }
  public void run() {
   Scanner sc = new Scanner(System.in);
   try {
    while (true) {
     String s = sc.nextLine();
//     System.out.println("s=" + s);
     out.write(s.getBytes());
     out.write("\n".getBytes());
     out.flush();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 // Writer负责读取服务器送来的的信息(in),写到客户端控制台
 class Writer extends Thread {
  InputStream in;

  public Writer(InputStream in) {
   this.in = in;
  }

  public void run() {
//   byte[] buf = new byte[1024];
   BufferedReader reader = new BufferedReader(
     new InputStreamReader(in));
   try {
    while (true) {
     String s = reader.readLine();
//     int n = in.read(buf);
//     System.out.println(new String(buf,0,n));
     System.out.println(s);
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值