Lua/NSE socket connection problem - sockets

I can telnet to a certain host and port no problem and issue commands. However when i try to script a socket connection (using nmap NSE and Lua) to the same host and port, it fails with the following error message:
|_sockettest: Trying to receive through a closed socket
the socket connect part of my code is here:
local msg
local response
msg = "hello\n"
local socket = nmap.new_socket()
socket:set_timeout(150000)
socket:send(msg)
response,data = socket:receive()
return data
I think the data is sending ok. The server should just echo back what i sent. Does anyone know what the problem could be?

You need to call socket:connect before receiving (and before sending). Seriously, read that code you wrote. Where did you specify to whom you're sending ?

Related

Arduino Ethernet Server

I have a small issue with ethernet server. I'd like to make Arduino server listening on port and catch connected devices but the problem is thats devices don't send anything first. just connecting and waiting for "welcome message" from the serwer.
Unfortunately I con not use this command
EthernetClient client = server.available();
Do you have any idea how to get client instance/object without sanding any message from client.
Thanks
server.available() should return only a client which has data waiting, otherwise you would get always the first client only.
You can use server.accept(). I returns a every client connection only once. See the AdvancedChatServer example.

TortoiseSVN A request to send or receive data was disallowed because the socket is not connected

Who can show me the reason error TortoiseSVN, when I connect SVN-Repo to update resource
The error detail:
I has already checked the
SVN server is running
Ping to IP server ok
I attached message error:
Error: Unable to connect to a repository at URL 'https://........'
Error running context: A request to send or receive data was
disallowed because the socket is not connected and (when sending on a
datagram socket using a sendto call) no address was supplied.
Hope Everyone help me show the way to fix it.
I have already found the problem, my server svn disabled svn-ports
Error running context: A request to send or receive data was
disallowed because the socket is not connected and (when sending on a
datagram socket using a sendto call) no address was supplied.

Why is one endpoint of this TCP connection sending a packet with the RST flag?

I'm writing an application that attempts to do the following:
create a TCP server listening on an available port
create a TCP socket that connects to the server
have the server socket write data to the client
have the server socket close its end of the connection
have the client write a message to the server
Here's where the problem lies. When I attempt to run the application, the TCP exchange goes like this:
The first three packets establish the three-way handshake, and the fourth and fifth packets are the transmission of the data written by the server and its acknowledgement.
As expected, the server socket sends a packet with the FIN flag set to indicate that it is closing its end of the connection. The client acknowledges this and then attempts to write its data to the socket. The server immediately sends an RST packet, terminating the connection prematurely.
Why does this happen?
Note: the above capture was done on Windows 8.1.
The sender cannot send data after a [FIN]. Such an action will result in the receiver issuing an [RST].
The FIN probably indicates that the server has fully closed the connection in both directions. In this case if it receives any further data on the connection it will issue an RST. This suggests an application protocol error on your part. If the server sends a reply and then closes the socket, the client can't send anything else via that connection.
Possibly you need your server to call shutdown() with SHUT_WR and then read something else from the client before closing the socket. Or possibly you're just doing it wrong.

NSStream receive NSStreamEventEndEncountered during opening

I have a client - server app that uses NSStream to connect. sometimes when trying to open a connection, one side of the connection gets NSStreamEventEndEncountered when first trying to send a message.
I use the bridge between CFStream and NSStream. My server create a socket with:
_ipv4cfsock = CFSocketCreate(kCFAllocatorDefault,PF_INET, SOCK_STREAM,IPPROTO_TCP, kCFSocketAcceptCallBack, handleConnect, &info);
In the handleConnect callback function, CFStreamCreatePairWithSocketis used to get two CFStream.
My client use CFStreamCreatePairWithSocketToHost to connect to the host.
The client connects (Creates NSStreams) and then sends a ping. When my server receives a connection it opens a NSStream, and when it recieve a ping it sends a pong.
When one of the connection closes the other should get a NSStreanEventEndEncountered as I have set kCFStreamPropertyShouldCloseNativeSocket to true on the streams.
I get several different results: I used NSLog to see what was happening.
-Most of the time the connection opens and works as expected.
Server did recieve new connection
Server recieved ping
Server Sent pong
Client recieved pong
-The connection closes (NSStreamEventEndEncountered) when trying to send the pong. The client doesn't recieve the pong but recieve NSStreamEventEndEncountered
Server did recieve new connection
Server recieved ping
Server recieved pong
Server closed
Client closed
-The connection closes (NSStreamEventEndEncountered) when trying to send the ping. The server doesn't recieve the ping or NSStreamEventEndEncountered
Server did recieve new connection
Client closed
-An error is recieved when trying to send the ping:
Server did recieve new connection;
Error recieved: The operation couldn’t be completed. Connection reset by peer
Both the client and the server are on my computer. Why does the stream get a NSStreamEventEndEncountered when trying to write?
I found the issue, I had a buffer that was sent as soon as my connection opened. If no data was in this buffer, an empty buffer was sent, which is an end signal for the streams.
From Apple Doc:
If the other end of the connection closes the connection:
Your connection delegate’s stream:handleEvent: method is called with
streamEvent set to NSStreamEventHasBytesAvailable. When you read from
that stream, you get a length of zero (0).
Your connection delegate’s stream:handleEvent: method is called with streamEvent set to
NSStreamEventEndEncountered.
When either of these two events occurs,
the delegate method is responsible for detecting the end-of-file
condition and cleaning up.

What does "connection reset by peer" mean?

What is the meaning of the "connection reset by peer" error on a TCP connection? Is it a fatal error or just a notification or related to the network failure?
It's fatal. The remote server has sent you a RST packet, which indicates an immediate dropping of the connection, rather than the usual handshake. This bypasses the normal half-closed state transition. I like this description:
"Connection reset by peer" is the TCP/IP equivalent of slamming the phone back on the hook. It's more polite than merely not replying, leaving one hanging. But it's not the FIN-ACK expected of the truly polite TCP/IP converseur.
This means that a TCP RST was received and the connection is now closed. This occurs when a packet is sent from your end of the connection but the other end does not recognize the connection; it will send back a packet with the RST bit set in order to forcibly close the connection.
This can happen if the other side crashes and then comes back up or if it calls close() on the socket while there is data from you in transit, and is an indication to you that some of the data that you previously sent may not have been received.
It is up to you whether that is an error; if the information you were sending was only for the benefit of the remote client then it may not matter that any final data may have been lost. However you should close the socket and free up any other resources associated with the connection.
one of the reasons for seeing this error and having trouble connecting to the server is that you enabled the firewall in the UNIX machine and forgot to add a rule to accept ssh connection. search in your WPS provider and you will find a way to connect to you machine and add this rules:
ufw allow ssh && ufw allow 22