socket通信 博客分类: java javasockettcp
程序员文章站
2024-03-14 22:39:41
...
客户端:
package c; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Client { public static PrintWriter out; public static void main(String[] args) throws UnknownHostException, IOException { System.out.println("客户端启动............"); Socket socket = new Socket(InetAddress.getLocalHost(),4215); out = new PrintWriter(socket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); while(true){ out.println(new Scanner(System.in).next()); out.flush(); System.out.println("服务器:"+in.readLine()); } } }
服务器端:
package s; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Server { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(4215); Socket socket = ss.accept(); System.out.println("服务器启动"); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream()); while(true){ System.out.println("客户端:"+in.readLine()); out.println(new Scanner(System.in).next()); out.flush(); } } }
推荐阅读
-
UDP通信 博客分类: java javaudpsocket
-
jar包用途 博客分类: java javajar
-
socket通信 博客分类: java javasockettcp
-
socket通信 博客分类: java javasockettcp
-
java中使用redis 博客分类: javaweb webjavaredisdbcache
-
UDP通信 博客分类: java javaudpsocket
-
java字节码操作 博客分类: interviewJ2SE 字节码javaasmjavassitcglib
-
linux安装java邮件服务器james 博客分类: 开源 linuxmailemail邮件james
-
解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法 博客分类: java 语言maven
-
java 注解的几大作用及使用方法详解(完) 博客分类: 编程语言 注解 java 注解