一、基础的架子
假设Executor是服务端,Driver是客户端
代码:
package test import java.io.InputStream import java.net.{ServerSocket, Socket} object Executor { def main(args: Array[String]): Unit = { //启动服务器,接收数据 val server = new ServerSocket(9999) println("服务器启动,等待接收数据") //等待客户端连接 val client: Socket = server.accept() val in: InputStream = client.getInputStream val i: Int = in.read() println("接收到客户端发送的数据"+i) in.close() client.close() server.close() } }
package test import java.io.OutputStream import java.net.Socket object Driver { def main(args: Array[String]): Unit = { //连接服务器 val client = new Socket("localhost", 9999) val out: OutputStream = client.getOutputStream out.write(2) out.flush() out.close() client.close() } }
运行结果: