How to use ConnectEx/DisconnectEx pointer on Windows? - c++

I got connectex/disconnectex working I tested they work but how do I reuse socket with them? I saw Reusing socket descriptor on connection failure but they say to loop the whole socket again and I don't want that (they say to go back to creating socket)? I just want to make a new socket once recv fails because host is offline (I'm doing a tcp client).
Thank you.

Related

How to fix the local side port# when making a socket with Boost Asio?

I'm trying to use Boost Asio to make some code that talks to a remote system over UDP. The remote system in question is a bit of a pain in that once you connect to it once, if you close the connection (like you end your program) and then try to connect again, if it doesn't see the same port# used on my end of that connection it will refuse it.
I'm trying to figure out how I can specify the port that gets used on my side of the connection, but nothing I'm trying seems to work and I'm getting a bit frustrated. I have no control over the remote endpoint, so I really need to get this working on my end.
Here's the relevant code I'm using to try to make the connection and send my data over:
boost::asio::io_service io_context;
socket= new udp::socket(io_context);
remoteendpoint = boost::asio::ip::udp::endpoint(
ip::address::from_string(remoteipaddress_), 50200);
localendpoint = boost::asio::ip::udp::endpoint(
ip::address::from_string(localipaddress_), 50103);
socket->bind(localendpoint,ec);
socket->open(boost::asio::ip::udp::v4());
int sent = socket->send_to(boost::asio::buffer(buf, len),remoteendpoint, 0, ec);
When I go run my code, the remote side is reporting back that it's seeing the connection coming in from a different port# (on my side, which I'm trying to set to 50103) every time I run. I really need it to ALWAYS see it as 50103.
ie: How do I ensure that the socket is ALWAYS (every time I run my program):
LocalIP:50103 <------> RemoteIP:50200
Ideas??? I'm stumped...
Thanks!
Ok, I THINK I fixed it. Needed to do the bind() call AFTER the socket->open(). Remote side now consistently is reporting that my side is coming from port 50103 which is what I wanted.
Seems a bit weird that you have to open before binding, but ok.

how to keep UDP socket connection open between 2 hosts

I'm working on a simple chatroom based on C++ and UDP, and I'm using this as a base. Every time client-server are saying "hello" to each other, both of them are ending their processes and nothing else, but I'd like to keep the socket open after that, so I can send something else and/or something like that, but haven't found a way to do so, so how do I do such thing? Haven't found much info on what I need, so any help appreciated. Thanks in advance.
You don't need to send a pulse or a heartbeat to keep the socket open. The socket will remain open as long as the program is running or you call close on it.
You can wrap your send and receive in an infinite loop but you should note that the example code you linked to is waaaay too simple for a chat client: you will need to handle errors like the underlying connection being offline ( for example, the interface being disconnected/ brought down, when the send and recv calls will return an error with associated errno ). You should look into using the select, poll and epoll system calls to detect errors and deal with them.

interface is down but netstat still shows the connection established? [duplicate]

This question already has answers here:
Java socket API: How to tell if a connection has been closed?
(9 answers)
Closed 5 years ago.
When I'm using e.g. PuTTY and my connection gets lost (or when I do a manual ipconfig /release on Windows), it responds directly and notifies my connection was lost.
I want to create a Java program which monitors my Internet connection (to some reliable server), to log the date/times when my internet fails.
I tried use the Socket.isConnected() method but that will just forever return "true". How can I do this in Java?
Well, the best way to tell if your connection is interrupted is to try to read/write from the socket. If the operation fails, then you have lost your connection sometime.
So, all you need to do is to try reading at some interval, and if the read fails try reconnecting.
The important events for you will be when a read fails - you lost connection, and when a new socket is connected - you regained connection.
That way you can keep track of up time and down time.
Even though TCP/IP is "connection oriented" protocol, normally no data is sent over an idle connection. You can have a socket open for a year without a single bit sent over it by the IP stack. In order to notice that a connection is lost, you have to send some data on the application level.(*) You can try this out by unplugging the phone cable from your ADSL modem. All connections in your PC should stay up, unless the applications have some kind of application level keepalive mechanism.
So the only way to notice lost connection is to open TCP connection to some server and read some data from it. Maybe the most simple way could be to connect to some FTP server and fetch a small file - or directory listing - once in a while. I have never seen a generic server which was really meant to be used for this case, and owners of the FTP server may not like clients doing this.
(*) There is also a mechanism called TCP keepalive but in many OS's you have to activate it for all applications, and it is not really practical to use if you want to notice loss of connection quickly
If the client disconnects properly, a read() will return -1, readLine() returns null, readXXX() for any other X throws EOFException. The only reliable way to detect a lost TCP connection is to write to it. Eventually this will throw an IOException 'connection reset', but it takes at least two writes due to buffering.
Why not use the isReachable() method of the java.net.InetAddress class?
How this works is JVM implementation specific but:
A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.
If you want to keep a connection open continually so you can see when that fails you could connect to server running the ECHO protocol yourself rather than having isReachable() do it for you and read and write data and wait for it to fail.
You might want to try looking at the socket timeout interval. With a short timeout (I believe the default is 'infinite timeout') then you might be able to trap an exception or something when the host becomes unreachable.
Okay so I finally got it working with
try
{
Socket s = new Socket("stackoverflow.com",80);
DataOutputStream os = new DataOutputStream(s.getOutputStream());
DataInputStream is = new DataInputStream(s.getInputStream());
while (true)
{
os.writeBytes("GET /index.html HTTP/1.0\n\n");
is.available();
Thread.sleep(1000);
}
}
catch (IOException e)
{
System.out.println("connection probably lost");
e.printStackTrace();
}
Not as clean as I hoped but it's not working if I leave out the os.writeBytes().
You could ping a machine every number of seconds, and this would be pretty accurate. Be careful that you don't DOS it.
Another alternative would be run a small server on a remote machine and keep a connection to it.
Its probably simpler to connect to yahoo/google or somewhere like this.
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yc = yahoo.openConnection();
int dataLen = yc.getContentLength() ;
Neil
The isConnected()method inside Socket.java class is a little misleading. It does not tell you if the socket is currently connected to a remote host (like if it is unclosed). Instead, it tells you whether the socket has ever been connected to a remote host. If the socket was able to connect to the remote host at all, this method returns true, even after that socket has been closed. To tell if a socket is currently open, you need to check that isConnected() returns true and isClosed() returns false.
For example:
boolean connected = socket.isConnected() && !socket.isClosed();

is there a way to reuse the socket addersses in C++/Linux

I have a while loop for retrying to connect to a device.
problem is, every time it does retry it is using the same socket descriptor(closing and opening again), is this safe?
while(retry)
create socket
read(use socket created before)
if read fails
close socket and retry
i want a new socket fd to connect with the server and read again. Is reusing the same one safe in case read has failed?
If you actually close the socket then I would suggest to create a new socket since the fd used for to describe the socket is now invalid so your reuse of the same file descriptor could cause errors.
Normally though it's better to close the old and create a new one.
since you called bind or connect on the socket, you can't change the address.
you must close the socket and create the new one.

Reconnect a socket with Boost asio in Windows

I'm having trouble when connecting a socket to an endpoint after being connected to another.
This is the situation:
a) The boost::asio::ip::tcp::socket is connected to a remote host (say pop.remote1.com).
b) The transmission ends, and the socket is closed:
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
socket_.close(error);
Then, when trying to connect to another host (say pop.remote2.com) using the same process that in a), the proccess returns without error, but the socket remains closed.
Note that when using pop.remote2.com as the first connection, the things run Ok, and the same problem arises if try to connect to pop.remote1.com after closing.
In both situations there are not pending processes in the attached io_service.
The questions are:
Is that reconnection admissible?
Is that the supposed correct process?
Thanks in advance.
P.D:
I tried to open the socket before the reconnection, but the result remains the same. That is, the result is the same if after closing the previous connection with.
socket_.shutdown(...);
socket_.close(...);
is used
socket_.open(...);
socket_.async_connect( ... );
or just
socket_.async_connect( ... );
A final thought:
After spent some time on the problem, and do some debug with MS Visual Studio, I think that simply that is not possible, at least in Asio v. 1.45.0; Windows 32 and VC++.
Perhaps the question is that here -at Boost librarys- all people think in and use objects, and if sometime need reconnect, simply delete the apropriate object, and do a new connection... creating a new object!
That was the solution that I do in my application with good results, athought with some extra code.
HTH to some else.
Is that reconnection admissible?
yes
Is that the supposed correct process?
yes and no. If you aren't opening the socket for subsequent connections after you close it for the previous one, you'll need to do that. Ex:
socket_.open();
socket_.async_connect( ... );