JAVA网络编程样例
2024-10-21 19:36:19
这篇文章主要用来总结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
信号后断开连接,如图所示
最新文章
- 基于 HTML5 的 WebGL 技术构建 3D 场景(一)
- Android 颜色渲染PorterDuff及Xfermode详解
- 【Python】将4*4数组旋转90度新数组
- hdu 1847 Good Luck in CET-4 Everybody!(简单博弈SG)
- java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.InjectionMetadata.<;init>;(Ljava/lang/Class;)V
- Windows下免费、开源邮件服务器hMailServer
- 2. shell之shell配置文件
- 浅谈Javase内存流程图
- 使用ChineseLunisolarCalendar 对象由年份获得生肖名,Datetime.now.tostring获得星期几
- Python自动化运维之18、Python操作 MySQL、pymysql、SQLAchemy
- Android UI ActionBar功能-在 Action Bar 上添加按钮
- 当使用javac编译源文件时,如何查找import导入的类
- SpringtMVC中配置 <;mvc:annotation-driven/>; 与 <;mvc:default-servlet-handler/>; 的作用与源码解析
- Linux下jetty报java.lang.OutOfMemoryError: PermGen space及Jetty内存配置调优解决方案
- tp5.0.7 修复getshell漏洞
- VScode快捷键、Chrome快捷键知识小总结和状态码
- 公众号对接绑定视频教程<;推荐>;【申明:来源于网络】
- 深入了解JVW
- 【文档】使用Sphinx + reST编写文档
- 转:VMWare服务器虚拟化--转自CSDN
热门文章
- PHP Redis - Hash (哈希)
- delete、truncate、drop的区别
- 4组-Alpha冲刺-总结
- Educational Codeforces Round 3 个人总结A-D
- C++ 函数参数与按值传递
- C语言初级阶段6——自定义数据类型
- [Python]Running multiprocessing
- [C# 学习笔记]运用 GDI+ 的 Matrix 进行显示图形的平移和缩放
- windows server 2008 创建计划任务不能正常执行
- java xml转为json的两种方法