Erlang/OTP Forums

Author Message

<  Erlang  ~  Problem in two way communicate between server and client.

nandan21
Posted: Thu Nov 05, 2009 4:46 am Reply with quote
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 Embarassed Crying or Very sad
View user's profile Send private message
flodis
Posted: Thu Nov 05, 2009 4:08 pm Reply with quote
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.
View user's profile Send private message
nandan21
Posted: Fri Nov 06, 2009 7:38 am Reply with quote
Joined: 23 Oct 2009 Posts: 8
thanks alot.....its working now...
but one question why we need socket in non active mode?
View user's profile Send private message
uwiger
Posted: Fri Nov 06, 2009 5:57 pm Reply with quote
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
View user's profile Send private message Visit poster's website

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

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