在java中,使用Socket完成TCP(传输控制协议)程序开发,使用此类可以方便地建立可靠的,双向的,持续的,点对点通信连接
服务端: ServerSocket 程序段:Socket
ServerSocket 1、建立服务端 2、accept()方法建立Socket
Socket 建立客户端指定要连接的服务端和连接端口
实例1:创建TCP服务端与程序端 服务端向程序端发送 Hello
服务端TCP:
import java.io.*; import java.net.*; public class TestServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(8888); System.out.println("建立服务端"); Socket client = server.accept(); System.out.println("客户端使用端口:" + client.getLocalPort());// OutputStream os = client.getOutputStream(); PrintStream psteam = new PrintStream(os); psteam.println("hello,client"); os.close();//关闭打印流 client.close();//client是否需要关闭?@需要的 server.close();//关闭服务端 } }
客户端TCP:
import java.io.*; import java.net.*; public class TestClient { public static void main(String[] args) throws Exception { Socket client = new Socket("192.168.0.121",8888); System.out.println("建立客户端,连入服务器"); System.out.println("服务端监听端口:" + client.getLocalPort());//getLocalPort方法取得使用端口 InputStream is = client.getInputStream(); BufferedReader bfreader = new BufferedReader(new InputStreamReader(is)); String str = bfreader.readLine(); System.out.println(str); bfreader.close(); client.close(); } }
结果:
上面的服务端每次执行完毕后都会退出,先通过循环accept方法,使服务端在一个客户端执行完毕后,可以重新等待其他客户端连接:
服务端TCP:
import java.io.*; import java.net.*; public class TestServer { public static void main(String[] args) throws Exception { ServerSocket ser = new ServerSocket(8888); while(true) { boolean flag = true; BufferedReader bur = null; PrintStream prs = null; Socket client = ser.accept(); System.out.println("有客户端连入"); bur = new BufferedReader( new InputStreamReader(client.getInputStream())); prs = new PrintStream(client.getOutputStream()); String str = null; while (flag) { str = bur.readLine(); if (str == null) { flag = false; } else if (str.equalsIgnoreCase("exit")) { flag = false; }else { prs.println("Echo" +"-----"+ str); } } System.out.println("客户端退出"); bur.close(); prs.close(); client.close(); } //ser.close();这一句不用写@错误:无法访问语句无法访问语句 } }
客户端TCP:
import java.io.*; import java.net.*; public class TestClient { public static void main(String[] args) throws Exception { Socket client = new Socket("192.168.0.121",8888); BufferedReader bur1 = new BufferedReader(new InputStreamReader(System.in)); BufferedReader bur2 = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintStream prs = new PrintStream(client.getOutputStream()); boolean flag = true; String str1 = null; String str2 = null; System.out.println("输入信息: "); while (flag) { str1 = bur1.readLine(); prs.println(str1); if (str1 == null || str1.equalsIgnoreCase("exit")) { flag = false; } else { str2 = bur2.readLine(); System.out.println(str2); } } bur1.close(); bur2.close(); prs.close(); client.close(); } }
结果:
上面程序可以实现循环客户端访问,但只能一个客户端进行访问,所以需要多线程
主程序:
import java.net.*; public class TestServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(8888); Socket client = null;//!!!!!!!!!! while (true) { client = server.accept();//出现重复定义了怎么办?变量名重复怎么办?在循环语句里面不要定义变量 new Thread(new ThreadServer(client)).start(); } } }
服务端TCP:
import java.io.*; import java.net.*; public class ThreadServer implements Runnable { private Socket client; public ThreadServer(Socket client) { this.client = client; } public void run() { try { PrintStream prs = new PrintStream(client.getOutputStream()); BufferedReader bur = new BufferedReader(new InputStreamReader(client.getInputStream())); boolean flag = true; String str = null; System.out.println("客户端登入"); while (flag) { str = bur.readLine(); if (str == null || str.equalsIgnoreCase("exit")) { flag = false; } else { prs.println( "------" + "Echo" + str); } } prs.close(); bur.close(); client.close(); System.out.println("客户端登出"); }catch (IOException e) { e.printStackTrace(); } } }
结果: