c++ - connect() breaks the socket when the connection is refused -


I was fighting an error in my program where I got the signal SIGPIPE when I tried To send () after connect () . The error occurred only when connect () was detected at least once "Connection refused". Here's the old buggy code:

  getaddrinfo ("127.0.0.1", "1443", and hint, and res); s = socket (res-> gt; ai_family, res-> ai_socktype, res-> ai_protocol); While (Connect (S, RAC-> AI_ADDR, RAC-> AI_ADDRRL)> == -1) {printf ("Error: Connection refused"); Sleep (2); } sprintf (msg, "test"); msg_len = strlen (msg); Send (s, msg, msg_len, 0);   

The way I decide it looks strange to me, and I do not understand why this works. Here is the fixed code:

  int connect_works = -1; Whereas (connect_work == -1) {getaddrinfo ("127.0.0.1", "1443", and hint, and;); S = socket (res-> gt; ai_family, res-> ai_socktype, res-> ai_protocol); Connect_work = connect (s, rac-> AI_ADDR, RAC-> AIIDDLLA); If (connect_work == -1) {printf ("error: connection denied"); Sleep (2); }} Sprintf (msg, "test"); msg_len = strlen (msg); Send (s, msg, msg_len, 0); To clarify, either the snippet works, if the first time the code is called  connect  that it withdraws the success, only the second The snippet works if  connect ()  has failed one or more times.  

In addition, this error occurs when Solaris is compiled or works either when compiled on Ubuntu.

Does someone help me understand why you should restart the socket if connect () fails?

The basic principle is that you can not reconnect a TCP socket once you accept it Or connect, even with the failure, you have to turn it off and create a new connection for the new connection.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -