Scala : how to start an actor in array?
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开发者_StackOverflow have a problem. How to translate this line into Scala:
(t[i] = new clientThread(clientSocket,t)).start();
Do you have any suggestions?
If you keep to a direct translation and your problem is just that assigning does not return a value in scala, then just do
t(i) = new ClientThread(clientSocket, t)
t(i).start
Simply:
t(i) = ( new ClientActor( clientSocket, t) ).start
where ClientActor
is your actor.
精彩评论