1:服务器端程序 EchoServer
package com.capinfotech.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
private int port = 8000;
private ServerSocket serverSocket;
public EchoServer() throws IOException {
serverSocket = new ServerSocket(port); //初始化ServerSocket
System.out.println("服务器启动");
}
/*
* 组装数据
*/
public String echo(String msg) {
return "echo:" + msg;
}
/*
* 获得PrintWriter对象
*/
private PrintWriter getWriter(Socket socket) {
try {
OutputStream socketOut = socket.getOutputStream();
PrintWriter pw = new PrintWriter(socketOut, true); //参数true表示每写一行,PrintWriter缓存就自动溢出,把数据写到目的地
return pw;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*
* 获得BufferedReader对象
*/
private BufferedReader getReader(Socket socket) throws IOException {
InputStream is = socket.getInputStream();
return new BufferedReader(new InputStreamReader(is));
}
public void service() throws IOException {
while(true) {
Socket socket = null;
try {
socket = serverSocket.accept(); //等待客户连接
//输出客户端的IP和端口号
System.out.println("New Connection accepted" + socket.getInetAddress() + ":" + socket.getPort()); PrintWriter pw = getWriter(socket);
BufferedReader br = getReader(socket);
String msg = null;
while((msg = br.readLine()) != null) {
System.out.println(msg);
pw.println(echo(msg));
if(msg.equals("bye")) {
break;
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(socket != null) {
socket.close();
}
}
}
}
public static void main(String[] args) {
try {
new EchoServer().service();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2:客户端程序EchoClient.java
package com.capinfotech.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class EchoClient {
private String localhost = "localhost";
private int port = 8000;
private Socket socket;
public EchoClient() throws UnknownHostException, IOException {
socket = new Socket(localhost, port); //初始化Socket
}
private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true); //参数true表示每写一行,PrintWriter缓存就自动溢出,把数据写到目的地
}
private BufferedReader getReader(Socket socket) throws IOException {
InputStream socketIn = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(socketIn));
return br;
}
public void talk() throws IOException {
try {
PrintWriter pw = getWriter(socket);
BufferedReader br = getReader(socket);
BufferedReader br_in = new BufferedReader(new InputStreamReader(System.in)); //获得系统的输入流(和标准输入有关)
String msg = null;
while((msg = br_in.readLine()) != null) {
pw.println(msg);
System.out.println(br.readLine()); //输出服务器端传过来的程序
if(msg.equals("bye")) {
break;
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(socket != null) {
socket.close();
}
}
}
public static void main(String[] args) throws UnknownHostException, IOException {
new EchoClient().talk();
}
}