i have started working on python chat, using sockets.
I am now having a problem with connecting many clients to the server, because if they connect to the same port they won't be able to communicate live, because each client would wait in line until the port will be free. Now my idea was to choose (on the server side) how many clients I want first, then open that range of ports using a simple for function and threads. Now my problem is that on my client size I am using try, when the "try" point is connecting to the port. At first I thought if somebody already connected to some port it will throw an error so the client will just jump to next port, but I forgot about that line thing. Any ideas?
Never mind i figures it out. My mistake was that i opened new socket with every thread, while should have opened that once in main() func, then do the accept in the thread. Thank you all
Related
I have a client written in python communicate with a server written in Go via TCP Socket. Currently, I create a new socket object and then connect to Go server every time. More specifically, suppose my go server listens on localhost:4040, whenever I connect to from python server I has a different source address (localhost:6379, 6378, ...) . I wonder if there is a way to leverage on old connections (like a connection pool) rather than creating the new one every time. If so, how to determine the connection has finished and became idle , do I need an extra ACK message for that? Thanks.
I have hardware which has the ability to receive data/commands via ethernet or serial.
I am doing socket programming in python to send commands to the hardware. Everything works fine, but once I close the socket (it closes successfully) and then when I try to reinit and create the socket in a different program, it throws me CONNECTION REFUSED
The only workaround for now is to remove the ethernet cable from the network switch and plug back in. and then it works and again once socket is closed and then want to reopen it, Connection refused error pops up.
Since the server code is running on proprietary hardware, I don't have access to it. I can only configure the port and ip address of the hardware.
Here is the snapshot of the program with the error message
and also the wireshark snapshot
and when I removed the ethernet wire and reconnected again , it can connect properly
see this snapshot.. so not sure where is gng wrong
Please let me know if you have any questions
This happens because the server is not running on that ip and or port.
This error is common. Try check through this:
Ensure that there are no other identical addresses. This is
important.
Make sure that the server is running before booting up the client.
Make sure the client has access to the server and the server can accept connections.
Make sure that the maximum connection setting is high enough to allow an ideal amount of connections. If this is not enabled then all
other connections get booted
Also when you said that the only way for you to get it to work is to reconnect your Ethernet cable, this is probably because you have a closed connection. You must set a loop so that the connection can be kept open
In my GO code, I am establishing a TCP connection as below:
conn, err1 := net.Dial("tcp", <remote_address>)
if err1 == nil {
buf := make([]byte, 256)
text, err := conn.Read(buf[:])
if err == io.EOF {
//remote connection close handle
fmt.Println("connection got reset by peer")
panic(err)
}
}
To simulate the other end, I am running a python script on a different computer, which opens a socket and sends some random data to the socket above lines of codes are listening to. Now my problem is, when I am killing this python code by pressing ctrl+C, the remote connection closed event is recognised finely by above code and I get a chance to handle that.
However, if I simply turn off the remote computer (where the python script is running) my code doesn't get notified at all.
In my case, the connection should always be opened and should be able to send the data randomly, and only if the remote machine gets powered off, my GO code should get notified.
Can someone help me in this scenario, how would I get notification when the remote machine hosting the socket itself gets powered off? How would I get the trigger remotely in my GO code?
PS - This seems to be a pretty common problem in real time, though not in the testing environment.
There is no way to determine the difference between a host that is powered off and a connection that has been broken, so you treat them the same way.
You can send a heartbeat message on your own, and close the connection when you reach some timeout period between heartbeat packets. The timeout can either be set manually by timing the packets, or you can use SetReadDeadline before each read to terminate the connection immediately when the deadline is reached.
You can also use TCP Keepalive to do this for you, using TCPConn.SetKeepAlive to enable it and TCPConn.SetKeepAlivePeriod to set the interval between keepalive packets. The time it takes to actually close the connection will be system dependent.
You should also set a timeout when dialing, since connecting to a down host isn't guaranteed to return an ICMP Host Unreachable response. You can use DialTimeout, a net.Dialer with the Timeout parameter set, or Dialer.DialContext.
Simply reading through the stdlib documentation should provide you with plenty of information: https://golang.org/pkg/net/
You need to add some kind of heartbeat message. Then, looking at GO documentation, you can use DialTimeout instead of Dial, each time you receive the heartbeat message or any other you can reset the timeout.
Another alternative is to use TCP keepalive. Which you can do in Python by using setsockopt, I can't really help you with GO but this link seems like a good description of how to enable keepalive with it:
http://felixge.de/2014/08/26/tcp-keepalive-with-golang.html
I am running a Graphite server to monitor instruments at remote locations. I have a "perpetual" ssh tunnel to the machines from my server (loving autossh) to map their local ports to my server's local port. This works well, data comes through with no hasstles. However we use a flaky satellite connection to the sites, which goes down rather regularly. I am running a "data crawler" on the instrument that is running python and using socket to send packets to the Graphite server. The problem is, if the link goes down temporarily (or the server gets rebooted, for testing mostly), I cannot re-establish the connection to the server. I trap the error, and then run socket.close(), and then re-open, but I just can't re-establish the connection. If I quit the python program and restart it, the connection comes up just fine. Any ideas how I can "refresh" my socket connection?
It's hard to answer this correctly without a code sample. However, it sounds like you might be trying to reuse a closed socket, which is not possible.
If the socket has been closed (or has experienced an error), you must re-create a new connection using a new socket object. For this to work, the remote server must be able to handle multiple client connections in its accept() loop.
I need to be able to connect to a raw serial connection that's connected to a modem.
The modem has an ip address and port.
This program works for one instance but is expensive for the amount of licenses I'd need and I'd prefer to code something:
http://www.serial-port-redirector.com/
It connects a remote ip to a local virtual com port.
I think it should be possible to do the same thing using pyserial but I'm struggling to understand how to do it.
This page kind of half explains it:
http://pyserial.sourceforge.net/examples.html#miniterm
But I'm still lost. If anyone could help me understand how to use rfc2217 in python it'd rock.
Thanks very much!
+anything for either linux/windows would be good.