Scala:如何在数组中启动演员?
我正在尝试用 2 种语言(Java 和 Scala)编写一个简单的客户端/服务器聊天应用程序。 Java 版本可以使用,唯一的问题是翻译它。 在Java中我有这样的代码:
import java.net.*;
import java.io.*;
public class FileServer666 extends Thread{
static Socket clientSocket = null;
static ServerSocket serverSocket= null;
static clientThread t[] = new clientThread[10];
public static void main(String args[]) throws IOException
{
int port_number =1406;
try
{
serverSocket = new ServerSocket(port_number);
}catch(IOException e){System.out.println(e);}
System.out.println("Listening" +port_number);
while(true)
{
try
{
clientSocket=serverSocket.accept();
System.out.println("Akceptuje połaczenie od: "+clientSocket.getInetAddress());
for(int i=0; i<=9; i++)
{
if(t[i]==null)
{
(t[i] = new clientThread(clientSocket,t)).start();
break;
}
}
}catch(IOException e){System.out.println(e);}
}
}
}
这里我有一个问题。如何将此行翻译成 Scala:
(t[i] = new clientThread(clientSocket,t)).start();
你有什么建议吗?
I'm trying to write a simple client/server chat application in 2 languages - Java and Scala. Java version is working and the only problem is to translate it.
In Java i have code like this:
import java.net.*;
import java.io.*;
public class FileServer666 extends Thread{
static Socket clientSocket = null;
static ServerSocket serverSocket= null;
static clientThread t[] = new clientThread[10];
public static void main(String args[]) throws IOException
{
int port_number =1406;
try
{
serverSocket = new ServerSocket(port_number);
}catch(IOException e){System.out.println(e);}
System.out.println("Listening" +port_number);
while(true)
{
try
{
clientSocket=serverSocket.accept();
System.out.println("Akceptuje połaczenie od: "+clientSocket.getInetAddress());
for(int i=0; i<=9; i++)
{
if(t[i]==null)
{
(t[i] = new clientThread(clientSocket,t)).start();
break;
}
}
}catch(IOException e){System.out.println(e);}
}
}
}
Here I have a problem. How to translate this line into Scala:
(t[i] = new clientThread(clientSocket,t)).start();
Do you have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你坚持直接翻译,并且你的问题只是分配不会在 scala 中返回值,那么就这样做
If you keep to a direct translation and your problem is just that assigning does not return a value in scala, then just do
简单地说:
其中
ClientActor
是您的演员。Simply:
where
ClientActor
is your actor.