Concurrency Erlang中的并发
我是erlang的新手,我正在尝试实现一个注册/登录服务器。我有一个注册新用户的功能,它运行良好:Concurrency Erlang中的并发,concurrency,erlang,distributed,Concurrency,Erlang,Distributed,我是erlang的新手,我正在尝试实现一个注册/登录服务器。我有一个注册新用户的功能,它运行良好: reg(Sock) -> receive {tcp, _, Usr} -> io:format("User: ~p ~n",[Usr]) end, gen_tcp:send(Sock, [Usr]), receive {tcp, _, Pass} -> io:format(" a pass ~p~n",[Pass]) e
reg(Sock) ->
receive
{tcp, _, Usr} ->
io:format("User: ~p ~n",[Usr])
end,
gen_tcp:send(Sock, [Usr]),
receive
{tcp, _, Pass} ->
io:format(" a pass ~p~n",[Pass])
end,
gen_tcp:send(Sock, [Pass]),
receive
{tcp, _, Msg} ->
case Msg of
<<"condutor\n">> ->
condutor ! {register, {Usr, Pass}};
<<"passageiro\n">> ->
io:format("passageiro~n")
end
end.
Java客户端:
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.err.println(
"Usage: java EchoClient <host name> <port number>");
System.exit(1);
}
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try (
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =
new BufferedReader(
new InputStreamReader(System.in))
) {
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}
import java.io.*;
导入java.net。*;
公共类EchoClient{
公共静态void main(字符串[]args)引发IOException{
如果(参数长度!=2){
System.err.println(
“用法:java EchoClient”);
系统出口(1);
}
字符串hostName=args[0];
int portNumber=Integer.parseInt(args[1]);
试一试(
Socket echoSocket=新套接字(主机名、端口号);
打印输出=
新的PrintWriter(echoSocket.getOutputStream(),true);
缓冲读取器=
新的缓冲读取器(
新的InputStreamReader(echoSocket.getInputStream());
缓冲读取器标准=
新的缓冲读取器(
新的InputStreamReader(System.in))
) {
字符串用户输入;
而((userInput=stdIn.readLine())!=null){
out.println(用户输入);
System.out.println(“echo:+in.readLine());
}
}捕获(未知后异常e){
System.err.println(“不知道主机”+主机名);
系统出口(1);
}捕获(IOE异常){
System.err.println(“无法获取到的连接的I/O”+
主机名);
系统出口(1);
}
}
}
发送“OK\n”作为对“registo”的响应。您的java代码正在使用readline
,因此您需要一个行终止符。客户端是否正在等待对其registo消息的答复?如果您在修改后包含整个程序,这将有所帮助。另外,当服务器接收到它不期望的消息时,最好在receive的末尾添加一个catch all子句进行调试,例如抛出({error,{unknown_message,Msg}})@lynhidey看起来像这样,但我不明白为什么,因为我发送了一个回复(gen\u tcp:send)对于我认为的每一个请求……我都编辑了原始帖子。@Paolo Sousa您已将代码更改为发送“OK”以响应“Regito”。您是否测试了该更改?这是否得到了回应?
-module(server2).
-export([server/1]).
server(Port) ->
{ok, LSock} = gen_tcp:listen(Port, [binary, {packet, line}, {reuseaddr, true}]),
Condutor = spawn(fun()-> regUtente([]) end),
register(condutor, Condutor),
acceptor(LSock).
acceptor(LSock) ->
{ok, Sock} = gen_tcp:accept(LSock),
spawn(fun() -> acceptor(LSock) end),
io:format("Ligação estabelecida~n"),
% reg(Sock). --- Caling reg directly, without passing through gestor WORKS FINE.
gestor(Sock).
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.err.println(
"Usage: java EchoClient <host name> <port number>");
System.exit(1);
}
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try (
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =
new BufferedReader(
new InputStreamReader(System.in))
) {
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}