Scala 相当于 python echo 服务器/客户端示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6414942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:07:02  来源:igfitidea点击:

Scala equivalent of python echo server/client example?

pythonscalaecho

提问by Andriy Drozdyuk

All the "server" example in scala use actors, reactors etc...

scala 中的所有“服务器”示例都使用演员、反应器等......

Can someone show me how to write a dead simple echo server and client, just like the following python example of Serverand Client:

有人可以告诉我如何编写一个简单的回显服务器和客户端,就像下面的服务器客户端的python 示例:

# A simple echo server 
import socket 

host = '' 
port = 50000 
backlog = 5 
size = 1024 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind((host,port)) 
s.listen(backlog) 
while 1: 
    client, address = s.accept() 
    data = client.recv(size) 
    if data: 
        client.send(data) 
    client.close()

# A simple echo client 
import socket 

host = 'localhost' 
port = 50000 
size = 1024 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((host,port)) 
s.send('Hello, world') 
data = s.recv(size) 
s.close() 
print 'Received:', data

回答by Przemek Pokrywka

You can do following within standard library:

您可以在标准库中执行以下操作:

// Simple server
import java.net._
import java.io._
import scala.io._

val server = new ServerSocket(9999)
while (true) {
    val s = server.accept()
    val in = new BufferedSource(s.getInputStream()).getLines()
    val out = new PrintStream(s.getOutputStream())

    out.println(in.next())
    out.flush()
    s.close()
}

// Simple client
import java.net._
import java.io._
import scala.io._

val s = new Socket(InetAddress.getByName("localhost"), 9999)
lazy val in = new BufferedSource(s.getInputStream()).getLines()
val out = new PrintStream(s.getOutputStream())

out.println("Hello, world")
out.flush()
println("Received: " + in.next())

s.close()

If you don't mind using extra libraries, you might like Finagle.

如果您不介意使用额外的库,您可能会喜欢Finagle

回答by Leon Radley

I just wrote a blog post about using Akka IO and Iteratees to create a simple command based socket server.

我刚刚写了一篇关于使用 Akka IO 和 Iteratees 创建一个简单的基于命令的套接字服务器的博客文章。

Maybe it could be of interest.

也许它可能会引起兴趣。

http://leon.radley.se/2012/08/akka-command-based-socket-server/

http://leon.radley.se/2012/08/akka-command-based-socket-server/

回答by Marcelo

You would have to use Java Sockets. I found a nice example of a Scala Socket Server/Client at: http://www.scala-lang.org/node/55

您将不得不使用 Java 套接字。我在以下位置找到了一个很好的 Scala 套接字服务器/客户端示例:http: //www.scala-lang.org/node/55

回答by tenshi

You can use nettyjava library. Here is an example usage in Scala:

您可以使用nettyjava 库。这是 Scala 中的示例用法:

https://github.com/mcroydon/scala-echo-server

https://github.com/mcroydon/scala-echo-server

Generally you need to use Java Socket API. In this exampleJava Socket API are used, but the whole server is wrapped in Actor in order to process clients in separate thread and not to block acceptor thread (the same thing you will normally do in Java, but you will use threads directly).

通常你需要使用Java Socket API。在这个例子中使用了 Java Socket API,但是整个服务器被包裹在 Actor 中,以便在单独的线程中处理客户端而不是阻塞接受者线程(你通常会在 Java 中做同样的事情,但你将直接使用线程)。

回答by oxbow_lakes

Josh Suereth recently posted an example of an NIO echo server using scalaz Iteratees. Requires the scalazlibrary

Josh Suereth 最近发布了一个使用 scalaz Iteratees 的 NIO 回显服务器示例。需要scalaz