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