import javaioBufferedReader;
import javaioIOException;
import javaioInputStream;
import javaioInputStreamReader;
import javaioOutputStream;
import javaioPrintWriter;
import javanetSocket;
import javanetUnknownHostException;
public class EchoClient {
private String host = "localhost";
private int port = 5000;
private Socket socket = null;
public EchoClient() throws UnknownHostException, IOException {
socket = new Socket(host, port);
}
public static void main(String g[]) throws UnknownHostException,
IOException {
new EchoClient()talk();
}
private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socketgetOutputStream(); // 首先从socket对象中获得输出的空间,即输出流,
// 再传到PrintWriter类中。
return new PrintWriter(socketOut, true); // 创建PrintWriter类,参数一:输出流,
// 参数二:为boolean值,true表示每写一行,PrintWrinter缓存就自动溢出,把数据写到目的地
}
private BufferedReader getReader(Socket socket) throws IOException {
InputStream socketIn = socketgetInputStream();// 首先从socket对象中获得输入流,再传到InputStreamReader类中。
return new BufferedReader(new InputStreamReader(socketIn));// 创建BufferedReader类,参数为InputStreamReader的对象
}
public void talk() {
BufferedReader br;
try {
br = getReader(socket);
PrintWriter pw = getWriter(socket);
BufferedReader localReader = new BufferedReader(
new InputStreamReader(Systemin));//在控制台中输入
String msg = null;
while ((msg = localReaderreadLine()) != null) {
pwprintln("Client:"+msg);
Systemoutprintln(brreadLine());
if (msgequals("bye")) {
break;
}
}
} catch (IOException e) {
eprintStackTrace();
} finally {
try {
socketclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
}
------------------------------------------------------------------
服务端
import javaioBufferedReader;
import javaioIOException;
import javaioInputStream;
import javaioInputStreamReader;
import javaioOutputStream;
import javaioPrintWriter;
import javanetServerSocket;
import javanetSocket;
public class EchoServer {
private int port = 5000; // 端口
private ServerSocket serverSoket = null; // 创建一个服务器
public EchoServer() throws IOException {
serverSoket = new ServerSocket(port); // 实例化服务器,注意:记得输入端口号
Systemoutprintln("服务器启动");
}
public String echo(String msg) {
return "echo:" + msg;
}
private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socketgetOutputStream(); // 首先从socket对象中获得输出的空间,即输出流,
// 再传到PrintWriter类中。
return new PrintWriter(socketOut, true); // 创建PrintWriter类,参数一:输出流,
// 参数二:为boolean值,true表示每写一行,PrintWrinter缓存就自动溢出,把数据写到目的地
}
private BufferedReader getReader(Socket socket) throws IOException {
InputStream socketIn = socketgetInputStream();// 首先从socket对象中获得输入流,再传到InputStreamReader类中。
return new BufferedReader(new InputStreamReader(socketIn));// 创建BufferedReader类,参数为InputStreamReader的对象
}
public void service() {
while (true) {
Socket socket = null; // 创建一个客户端
try {
Systemoutprintln("获得连接之前");
socket = serverSoketaccept(); // 等待客户连接,accept()将返回一个socket对象,即客户连接者。
Systemoutprintln("获得连接之后");
Systemoutprintln("New connection accepted"
+ socketgetInetAddress() + ":" + socketgetPort());
BufferedReader br = getReader(socket);// 创建BufferedReader的对象并实例化
PrintWriter pw = getWriter(socket); // 创建PrintWriter的对象并实例化
String msg = null;
while ((msg = brreadLine()) != null) {//brreadLine(),本次中只能读一次。
Systemoutprintln(msg);
if (msgequals("bye")) { //如果为bye那么就退出
break;
}
BufferedReader tempbr = new BufferedReader(
new InputStreamReader(Systemin));//在控制台中输出
String tempS = tempbrreadLine();
if (tempSequals("bye")) { //如果为bye那么就退出
break;
}
pwprintln("Server:"+tempS); // 将所要回复的信息写到socket对象输出流中。
}
} catch (IOException e) {
eprintStackTrace();
} finally {
if (socket != null) {
try {
socketclose();//关闭客户端
} catch (IOException e) {
eprintStackTrace();
}
}
}
}
}
public static void main(String[] g) throws IOException {
new EchoServer()service();
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)