这篇文章主要用来总结Java在网络编程中的知识点

下面是一个Java客户端与服务端通信的样例程序

//Server
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(6666);
System.out.println("Server start listening...");
for (; ; ) {
Socket sock = ss.accept();
System.out.println("Receviced package from " + sock.getRemoteSocketAddress());
Thread t = new Handler(sock);
t.start();
}
}
} class Handler extends Thread {
Socket sock; public Handler(Socket sock) {
this.sock = sock;
} @Override
public void run() {
try (InputStream input = this.sock.getInputStream()) {
try (OutputStream output = this.sock.getOutputStream()) {
handle(input, output);
}
} catch (Exception e) {
try {
this.sock.close();
} catch (IOException ioe) {
}
System.out.println("Client disconnected.");
}
} private void handle(InputStream input, OutputStream output) throws IOException {
var writer = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8));
var reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
writer.write("hello\n");
writer.flush();
for (; ; ) {
String s = reader.readLine();
if (s.equals("bye")) {
writer.write("bye\n");
writer.flush();
break;
}
writer.write("ok: " + s + "\n");
writer.flush();
}
}
}
//client
public class client {
public static void main(String[] args) throws IOException {
Socket sock = new Socket("localhost", 6666);
try (InputStream input = sock.getInputStream()){
try(OutputStream output = sock.getOutputStream()){
handle(input, output);
}
}
}
private static void handle(InputStream input, OutputStream output) throws IOException{
var writer = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8));
var reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
Scanner scanner = new Scanner(System.in);
System.out.println("[server]: "+ reader.readLine());
for(;;){
System.out.print(">>>");
String s = scanner.nextLine();
writer.write(s);
writer.newLine();
writer.flush();
String resp = reader.readLine();
System.out.println("<<<" + resp);
if(resp.equals("bye")) break;
}
}
}

实现功能为服务端与客户端相互通信,收到bye信号后断开连接,如图所示

最新文章

  1. 基于 HTML5 的 WebGL 技术构建 3D 场景(一)
  2. Android 颜色渲染PorterDuff及Xfermode详解
  3. 【Python】将4*4数组旋转90度新数组
  4. hdu 1847 Good Luck in CET-4 Everybody!(简单博弈SG)
  5. java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.InjectionMetadata.&lt;init&gt;(Ljava/lang/Class;)V
  6. Windows下免费、开源邮件服务器hMailServer
  7. 2. shell之shell配置文件
  8. 浅谈Javase内存流程图
  9. 使用ChineseLunisolarCalendar 对象由年份获得生肖名,Datetime.now.tostring获得星期几
  10. Python自动化运维之18、Python操作 MySQL、pymysql、SQLAchemy
  11. Android UI ActionBar功能-在 Action Bar 上添加按钮
  12. 当使用javac编译源文件时,如何查找import导入的类
  13. SpringtMVC中配置 &lt;mvc:annotation-driven/&gt; 与 &lt;mvc:default-servlet-handler/&gt; 的作用与源码解析
  14. Linux下jetty报java.lang.OutOfMemoryError: PermGen space及Jetty内存配置调优解决方案
  15. tp5.0.7 修复getshell漏洞
  16. VScode快捷键、Chrome快捷键知识小总结和状态码
  17. 公众号对接绑定视频教程&lt;推荐&gt;【申明:来源于网络】
  18. 深入了解JVW
  19. 【文档】使用Sphinx + reST编写文档
  20. 转:VMWare服务器虚拟化--转自CSDN

热门文章

  1. PHP Redis - Hash (哈希)
  2. delete、truncate、drop的区别
  3. 4组-Alpha冲刺-总结
  4. Educational Codeforces Round 3 个人总结A-D
  5. C++ 函数参数与按值传递
  6. C语言初级阶段6——自定义数据类型
  7. [Python]Running multiprocessing
  8. [C# 学习笔记]运用 GDI+ 的 Matrix 进行显示图形的平移和缩放
  9. windows server 2008 创建计划任务不能正常执行
  10. java xml转为json的两种方法