<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<TITLE>Message</TITLE>
<META content="MSHTML 6.00.2900.2523" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2>Hello!</FONT></SPAN></DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial size=2>This is my first
week programming Erlang, so please be patient:</FONT></SPAN></DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial size=2>I'm trying to
make an Erlang program that communicate over one open socket by using
gen_tcp. There is a server on the other end that I don't have any control over,
so I've also made a simulator for it in Erlang for the time being. The server
interface specifies that the client, my program, initiates communication by
sending a command, and then keeps the socket open and waits for the server to
reply. </FONT></SPAN></DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial size=2>I have managed to
get as far as my client sends a command, my server simulator (run on same PC,
but in other shell) receives that message and sends a response, where
gen_tcp:send returns ok. The problem arise when the client tries to use
gen_tcp:recv. This is where it receives {error, einval}.</FONT></SPAN></DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial size=2>How do you suggest I
overcome this problem?</FONT></SPAN></DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial size=2>CLIENT
CODE</FONT></SPAN><SPAN class=423075314-09122004><FONT face=Arial
size=2>:</FONT></SPAN></DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2>-module(interface).<BR>-export([request/2]).<BR>-include("interface.hrl").</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial size=2>request(Msisdn,
Port) -><BR>%%% Build request from input data %%%<BR>
Request = build_request(Msisdn),<BR>%%% Send request and wait for response
%%% <BR> R</FONT></SPAN><SPAN
class=423075314-09122004><FONT face=Arial size=2>esponse = get_response(Request,
Port),<BR> <BR>build_request(Msisdn) -><BR>"0045006" ++ Msisdn ++
"46708998851FFFFFFFFF00".</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2>get_response(Request, Port) -><BR>%%% Setup connection %%% <BR>
case gen_tcp:connect("localhost",
Port,<BR>
[list, {packet, 0},{reuseaddr, true}]) of<BR> {ok, Sock}
-><BR> %%% Send request
%%%
<BR> case gen_tcp:send(Sock, Request)
of<BR> ok
-><BR>
io:format("Message sent
OK~n");<BR>
{error, Reason}
-><BR>
io:format("Send failed(~p)~n",
[Reason])<BR>
end,<BR> %%% Receive response
%%%<BR> Response =
receive_response(Sock),<BR> case
gen_tcp:close(Sock)
of<BR> ok
-><BR>
io:format("Closing
connection~n");<BR>
{error, Reason2}
-><BR>
io:format("Cannot close (~p)~n",
[Reason2])<BR>
end,<BR>
Response;<BR> {error, Reason}
-><BR> io:format("Cannot connect to
server (~p)~n", [Reason])<BR> % ToDo:
handle this problem<BR> end.<BR> <BR>receive_response(Sock)
-><BR> io:format("Sock (~p)~n",
[Sock]),<BR> case gen_tcp:recv(Sock, 4)
of<BR> {ok, B}
-><BR> Size
=
list_to_integer(B),<BR>
io:format("Receiving message of size (~p).~n",
[Size]);<BR> {error, Reason}
-><BR> Size
= 0,<BR>
io:format("Cannot receive Size (~p)~n", [Reason])<BR>
end,<BR> case gen_tcp:recv(Sock, Size)
of<BR> {ok, Message}
-><BR>
io:format("Receiving message (~p).~n",
[Message]),<BR>
Message;<BR> {error, Reason2}
-><BR>
io:format("Cannot receive (~p)~n", [Reason2])<BR>
end.<BR></FONT></SPAN></DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial size=2>SERVER SIMULATOR
CODE:</FONT></SPAN></DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2>-module(simulator).<BR>-export([start/1]).</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial size=2>start(Port)
-><BR> case gen_tcp:listen(Port, [list, {packet, 0},
<BR>
{active, false},{reuseaddr, true}]) of<BR> {ok,
LSock} -><BR> io:format("OK to
listen~n"),<BR> case
gen_tcp:accept(LSock)
of<BR> {ok, Sock}
-><BR>
io:format("OK to
accept~n"),<BR>
do_recv(Sock),<BR>
send_response("46708797851FFFFFFFFF", Sock),
<BR> case
gen_tcp:close(Sock)
of<BR>
ok
-><BR>
io:format("Closing
connection~n");<BR>
{error, Reason2}
-><BR>
io:format("Cannot close (~p)~n",
[Reason2])<BR>
end;<BR> {error, _}
-><BR>
io:format("Cannot accept for
server~n")<BR>
end; <BR> {error, Reason}
-><BR> io:format("Cannot listen for
server (~p)~n", [Reason])<BR> end.</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial size=2>do_recv(Sock)
-><BR> io:format("Socket used (~p)~n",
[Sock]),<BR> MessageSize =
receive_size(Sock),<BR> Message = receive_message(Sock,
MessageSize),<BR> io:format("Received message (~p)~n",
[Message]),<BR> Message.<BR>
<BR>receive_size(Sock) -><BR> case gen_tcp:recv(Sock, 4)
of<BR> {ok, B}
-><BR> Size
=
list_to_integer(B),<BR>
io:format("Receiving message of size (~p)~n",
[Size]),<BR>
Size;<BR> {error, Reason}
-><BR>
io:format("Cannot receive (~p)~n", [Reason])<BR>
end.</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2>receive_message(Sock, MessageSize) -><BR> case
gen_tcp:recv(Sock, MessageSize) of<BR>
{ok, Message}
-><BR>
Message;<BR> {error, Reason}
-><BR>
io:format("Cannot receive (~p)~n", [Reason])<BR>
end.</FONT></SPAN></DIV>
<DIV><SPAN class=423075314-09122004><FONT face=Arial
size=2><BR>send_response(Msisdn, Sock) -><BR> Response =
"0069009" ++ Msisdn ++ <BR> "46708889951FFFFFFFFFNot Enough
funds 08",<BR> case
gen_tcp:send(Sock, Response) of<BR> ok
-><BR>
io:format("Message sent (~p).~n",
[Response]);<BR> {error, Reason}
-><BR>
io:format("Cannot send (~p)~n", [Reason])<BR>
end.<BR></DIV></FONT></SPAN>
<BR>
This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.<BR>
</BODY></HTML>