| Author |
Message |
< Erlang ~ Problem in two way communicate between server and client. |
| nandan21 |
Posted: Thu Nov 05, 2009 4:46 am |
|
|
|
Joined: 23 Oct 2009
Posts: 8
|
Hiii,
I am learning erlang programming language, i m new to it.
In socket programming, i tried two way communicate between server and client but whatever client send, server get it but when server send message to client,then it is giving error..
Plz help me out
Below is the server and client code
Server code
-module(echo).
-export([listen/1]).
-define(TCP_OPTIONS,[list, {packet, 0}, {active, false}, {reuseaddr, true}]).
listen(Port) ->
{ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
{ok, Socket} = gen_tcp:accept(LSocket),
do_echo(Socket).
do_echo(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
io:format("Mess::~n~p",[Data]),
gen_tcp:send(Socket, "Yes,i m server"),
do_echo(Socket);
{error, closed} ->
ok
end.
Client code
-module(cl).
-export([client/0]).
client() ->
{ok, Sock} = gen_tcp:connect("localhost", 5678,
[binary, {packet, 0}]),
ok = gen_tcp:send(Sock, "R u server"),
{ok, Bin} = do_recv(Sock, []),
ok = gen_tcp:close(Sock).
do_recv(Sock, Bs) ->
case gen_tcp:recv(Sock, 0) of
{ok, B} ->
io:format("Data=~n~p",[B]),
do_recv(Sock, [Bs, B]);
{error, closed} ->
{ok, list_to_binary(Bs)}
end.
compiling by
c(server).
c(client).
running by
server:listen(5678).
client:client().
client is giving this error
** exception error: no case clause matching {error,einval}
in function cl:do_recv/2
in call from cl:client/0
Please suggest something.
Thanks in advance  |
|
|
| Back to top |
|
| flodis |
Posted: Thu Nov 05, 2009 4:08 pm |
|
|
|
User
Joined: 09 Jul 2008
Posts: 27
|
In your client, you probably need to put the socket into non-active mode.
Code:
{ok, Sock} = gen_tcp:connect("localhost", 5678,
[binary, {packet, 0}, {active, false}]),
Also when posting code, put the code between the "[code]" tag, makes it much more readable. |
|
|
| Back to top |
|
| nandan21 |
Posted: Fri Nov 06, 2009 7:38 am |
|
|
|
Joined: 23 Oct 2009
Posts: 8
|
thanks alot.....its working now...
but one question why we need socket in non active mode? |
|
|
| Back to top |
|
| uwiger |
Posted: Fri Nov 06, 2009 5:57 pm |
|
|
|
User
Joined: 03 Jul 2006
Posts: 604
Location: Sweden
|
nandan21 wrote: why we need socket in non active mode?
Because gen_tcp:recv/2 expects it. If you have {active,true}, messages will be delivered automatically to the controlling process, and gen_tcp:recv/2 makes no sense. This is implied in the manual, but not spelled out plainly. |
_________________ http://www.erlang-consulting.com |
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|