Erlang/OTP Forums

Author Message

<  Erlang  ~  simple TCP problem

dijxtra
Posted: Tue May 10, 2011 2:28 am Reply with quote
Joined: 07 Mar 2011 Posts: 6
I'm trying to write a TCP server: http://pastebin.com/aCeZh3mW

It works, except for the fact that client desn't get the result. Server receives the value, evaluates it, prints the result, but telnet just hangs outputs nothing. Any ideas where to start looking?

Regards,
dijxtra
View user's profile Send private message Send e-mail
rvirding
Posted: Fri May 13, 2011 4:03 pm Reply with quote
User Joined: 30 Aug 2006 Posts: 452 Location: Stockholm, Sweden
Could you give some more details? Which system are you running on and which client are you using?

I would say that the probable cause is that you are using the wrong socket. You save and then use the listen socket for communication. This is wrong, the only thing you do with a listen socket is to accept connection requests on it. You should be saving the socket returned from gen_tcp:accept and using that for selecting input and sending output.

So I would have something like:

Code:
-record(interface_state, {port,lsock,sock,buffer}).

...

exec_call(State) ->
    Socket = State#interface_state.sock,    %Use right socket
    Buffer = State#interface_state.buffer,
    ...
    gen_tcp:send(Socket, io_lib:fwrite("~p~n", [Result])),
    io:format("~w~n", [Result]).

...

handle_info(timeout, #interface_state{lsock = LSock} = State) ->
    {ok, Sock} = gen_tcp:accept(LSock),
    {noreply, State#interface_state{sock=Sock}}.

A few other points:

- It is very unsafe to assume how tcp packets are delivered, in this case to assume that your commands will arrive as a stream of packets with the last packet containing only \r\n. Tcp is stream based so there are no guarantees at all.

- You shouldn't suspend behaviours the way you do in gen_tcp:accept as they use messages to interact with the rest of OTP. So your server will be completely unresponsive while it is waiting for connect request. If you want to use a gen_server here it would probably be better to have a top-level process which does listen/accept and then start a gen_server to handle the requests.

_________________
Robert Virding, Erlang Solutions Ltd.
View user's profile Send private message Visit poster's website MSN Messenger
zhuyifan1129
Posted: Sun May 15, 2011 6:21 am Reply with quote
Guest
Laughing Laughing I'm so like your style.I agree to your opinion.Hoping
more your better article!Pearl Earrings
dijxtra
Posted: Sun May 15, 2011 9:12 am Reply with quote
Joined: 07 Mar 2011 Posts: 6
rvirding wrote:
I would say that the probable cause is that you are using the wrong socket.

That's it! Thanks!
rvirding wrote:

A few other points:

Thanks for the hints. I'm aware this code is horrible, I'm working on fixking it Smile

Thank you for your response!
View user's profile Send private message Send e-mail
raymondb
Posted: Thu Jun 09, 2011 3:39 am Reply with quote
Joined: 08 Jun 2011 Posts: 4
I do not think this is a common problem and would probably be because of a wrong setting in your ip protocol. Probably a wrong socket as what is already suggested.

I have used it for quite a while now and luckily have not run into those types of problems.
View user's profile Send private message
Emmarogers
Posted: Sat Sep 17, 2011 11:18 am Reply with quote
Joined: 17 Sep 2011 Posts: 1 Location: 23306 Three Notch Rd California, MD 20619
Great work rvirding, i never thought that the problem could be with the socket..

_________________
SaaS Companies
View user's profile Send private message
wuji
Posted: Thu Aug 16, 2012 8:01 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
30 years in prison if convicted on that charge. The The [h2]imitation designer *beep*[/h2] The maximum sentence for aggravated battery is 15 years.After three
of testimony from Brewer, his parents and several other boys boys cheap polo ralph lauren boys who'd witnessed the attack, a six-person jury in the
County Courthouse began deliberations Monday afternoon, but adjourned after three three cheap jordans three hours without a verdict. Tuesday's deliberations were delayed when
said they could not understand what was being said in in cheap replica *beep* in an audio recording of a conversation between Bent and
alleged accomplices taped by police following their arrest.At noon on on imitation designer *beep* on Tuesday, the jury sent a note to Broward County
Michael Robinson asking for a transcript of the recording, but but cheap polo shirts but Robinson replied that no transcript was available.Prosecutors argued that
View user's profile Send private message
dongdongwu
Posted: Thu Sep 20, 2012 5:23 am Reply with quote
User Joined: 19 Sep 2012 Posts: 236
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word.
View user's profile Send private message

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