import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; //================================================= // File Name : HelloServer_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : HelloServer_demo public class HelloServer_demo { public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 ServerSocket server = null; //声明ServerSocket对象 Socket client = null; //一个Socket对象表示一个客户端 PrintStream out = null; //声明打印流对象,以向客户端输出 server = new ServerSocket(8888); //表示服务器在8888端口上等待客户端的访问 System.out.println("服务器运行,等待客户端连接"); client = server.accept(); //程序阻塞,等待客户端连接 String str = "HelloWord"; //要向客户端输出信息 out = new PrintStream(client.getOutputStream()); //实例化打印流对象,输出信息 out.println(str); out.close(); client.close(); server.close(); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; //================================================= // File Name : HelloClient_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : HelloClient_demo public class HelloClient_demo { public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 Socket client = null; //声明Socket对象 client = new Socket("localhost",8888); //指定连接的主机和端口 BufferedReader buf = null; //声明BufferedReader对象,接受信息 //指定客户端的输入流 buf = new BufferedReader(new InputStreamReader(client.getInputStream())); String str = buf.readLine(); System.out.println("服务器输出的内容是"+str); client.close(); buf.close(); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; //================================================= // File Name : EchoServer_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : EchoServer_demo public class EchoServer_demo { public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 ServerSocket server = null; //声明ServerSocket对象 Socket client = null; //一个Socket对象表示一个客户端 PrintStream out = null; //声明打印流对象,以向客户端输出 BufferedReader buf = null; //声明BufferedReader对象,接受信息 server = new ServerSocket(8888); //表示服务器在8888端口上等待客户端的访问 boolean f = true; while(f){ System.out.println("服务器运行,等待客户端连接:"); client = server.accept(); //接收客户端连接 //得到客户端的输入信息 buf = new BufferedReader(new InputStreamReader(client.getInputStream())); //实例化客户端的输出流 out = new PrintStream(client.getOutputStream()); boolean flag = true; while(flag){ String str = buf.readLine(); //在此处不断地接收信息 if(str == null || "".equals(str)){ flag = false; }else{ if("bye".equals(str)){ flag = false; }else{ out.print("ECHO"+str); } } } out.close(); client.close(); } server.close(); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; //================================================= // File Name : EchoClient_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : EchoClient_demo public class EchoClient_demo { public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 Socket client = null; //声明Socket对象 client = new Socket("localhost",8888); //指定连接的主机和端口 BufferedReader buf = null; //声明BufferedReader对象,接受信息 PrintStream out = null; //输出流,向服务器端发送信息 BufferedReader input = null; //声明BufferedReader对象 //从键盘接收数据 input = new BufferedReader(new InputStreamReader(System.in)); //向服务器端输出信息 out = new PrintStream(client.getOutputStream()); //接收服务器端输入信息 buf = new BufferedReader(new InputStreamReader(client.getInputStream())); boolean flag = true; while(flag){ System.out.print("输入信息:"); String str = input.readLine(); //从键盘接收数据 out.println(str); if("bye".equals(str)){ flag = false; }else{ String echo = buf.readLine(); System.out.println(echo); } } client.close(); buf.close(); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; //================================================= // File Name : EchoClient_demo //------------------------------------------------------------------------------ // Author : Common // 类名:ActionHandle // 属性: // 方法: class EchoThreadServer implements Runnable{ private Socket client = null; public EchoThreadServer(Socket client){ this.client = client; } @Override public void run() { // TODO 自动生成的方法存根 PrintStream out = null; //声明打印流对象,以向客户端输出 BufferedReader buf = null; //声明BufferedReader对象,接受信息 try{ //得到客户端的输入信息 buf = new BufferedReader(new InputStreamReader(client.getInputStream())); //实例化客户端的输出流 out = new PrintStream(client.getOutputStream()); boolean flag = true; while(flag){ String str = buf.readLine(); //在此处不断地接收信息 if(str == null || "".equals(str)){ flag = false; }else{ if("bye".equals(str)){ flag = false; }else{ out.print("ECHO"+str); } } } out.close(); client.close(); }catch(Exception e){ } } } //主类 //Function : EchoClient_demo public class EchoThreadServer_demo { public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 ServerSocket server = null; Socket client = null; server = new ServerSocket(8888); boolean f = true; while(f){ System.out.println("服务器运行,等待客户端连接:"); client = server.accept(); new Thread(new EchoThreadServer(client)).start(); } server.close(); } }