1、编写服务器端程序
import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class HelloServer { public static void main(String[] args) throws Exception{ ServerSocket server = new ServerSocket(9999); //所有服务器都必须有端口 System.out.println("等待客户连接"); Socket client = server.accept(); PrintStream out = new PrintStream(client.getOutputStream()); //实例化打印流对象,输出信息 out.println("hello world"); out.close(); client.close(); server.close(); } }
2、编写客户端程序
import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class HelloServer { public static void main(String[] args) throws Exception{ ServerSocket server = new ServerSocket(9999); //所有服务器都必须有端口 System.out.println("等待客户连接"); Socket client = server.accept(); PrintStream out = new PrintStream(client.getOutputStream()); //实例化打印流对象,输出信息 out.println("hello world"); out.close(); client.close(); server.close(); } }
将客户端程序拷贝到d盘,并将第一行的导入包删除,在命令窗口执行。
3、Echo程序
import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class EchoServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(8888); Socket client = server.accept(); //等待连接 //得到客户端输入数据以及向客户端输出数据的对象 Scanner scanner = new Scanner(client.getInputStream()); PrintStream out = new PrintStream(client.getOutputStream()); boolean flag = true; while (flag){ if (scanner.hasNext()){ String str = scanner.next().trim(); if (str.equalsIgnoreCase("byebye")){ flag=false; }else { out.println("Echo:"+str); } } } client.close(); server.close(); } }
客户端:
import java.io.PrintStream; import java.net.Socket; import java.util.Scanner; public class EchoClient { public static void main(String[] args) throws Exception{ Socket client = new Socket("localhost",8888); Scanner input = new Scanner(System.in); //键盘输入数据 Scanner scanner = new Scanner(client.getInputStream()); input.useDelimiter(" "); scanner.useDelimiter(" "); //换行为分隔符 PrintStream out = new PrintStream(client.getOutputStream()); boolean flag =true; while (flag){ System.out.println("输入信息:"); if (input.hasNext()){ String str = input.next().trim(); out.println(str); if ("byebye".equalsIgnoreCase(str)){ flag = false; } if (scanner.hasNext()){ System.out.println(scanner.next()); } } } client.close(); } }
4、EchoThread
import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; class EchoThread implements Runnable{ private Socket client = null; public EchoThread(Socket client){ this.client = client; } @Override public void run() { try{ //得到客户端输入数据以及向客户端输出数据的对象 Scanner scanner = new Scanner(client.getInputStream()); PrintStream out = new PrintStream(client.getOutputStream()); boolean flag = true; while (flag){ if (scanner.hasNext()){ String str = scanner.next().trim(); if (str.equalsIgnoreCase("byebye")){ out.println("***关闭***下次再回***"); flag=false; }else { out.println("Echo:"+str); } } } client.close(); }catch (Exception e){ System.err.println(e); } } } public class EchoServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(8888); boolean flag =true; while (flag){ Socket client = server.accept(); //等待连接 new Thread(new EchoThread(client)).start(); } } }