Re: Sockets: code works locally but fails over LAN



n00m wrote:
import socket, thread
host, port = '192.168.0.3', 1434
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((host, 1433))
s1.bind((host, port))
s1.listen(1)
cn, addr = s1.accept()

def VB_SCRIPT():
    while 1:
        data = cn.recv(4096)
        if not data: return
        s2.send(data)
        print 'VB_SCRIPT:' + data + '\n\n'

def SQL_SERVER():
    while 1:
        data = s2.recv(4096)
        if not data: return
        cn.send(data)
        print 'SQL_SERVER:' + data + '\n\n'

Several suggestions:

1. Use repr(data) instead of just 'data' above, to see better the actual bytes without the possibility of control characters like \r and \b screwing things up.

2. I'm not at all sure that accessing the same socket object simultaneously from two threads is safe. You might consider creating a pair of Queue objects to safely communicate the information between the two threads. That, of course, poses the problem of how do you wait on data to arrive from the socket and from the Queue at the same time. One approach is to use non-blocking sockets or timeouts, while the other is to use a pre-existing asynchronous framework such as, say, Twisted, and avoid reinventing the wheel (and making all the same mistakes that other programmers have made zillions of times before you).

It's also possible this is not remotely related to your problem, but I suspect without knowing more about SQL Server, VB, and your own setup I'd be guessing wildly anyway...

-Peter
.



Relevant Pages

  • Re: Problem with socket
    ... Be aware that those port numbers are part of the IANA-assigned range. ... socket operations on sockets for which there are no handles... ... The result of using comma lists is ... you have used the completely meaningless word "crash" to describe your ...
    (microsoft.public.vc.mfc)
  • RE: call is blocked in recvfrom() and no further proceedings in Win CE
    ... In windows CE, I'm able to send a request but I'm unable to receive it. ... Create another socket & bind with server IP address. ... > My program has to send request to service through port 5070(in this port only ...
    (microsoft.public.windowsce.embedded)
  • Re: ISA Event
    ... applying ISA SP1 resolved the issue. ... So it's worth asking - is this SBS ... > Web Proxy service failed to bind its socket to 192.168.4.9 port 443. ...
    (microsoft.public.backoffice.smallbiz2000)
  • Re: Freebsd IP Forwarding performance (question, and some info) [7-stable, current, em, smp]
    ... INADDR_ANY and a specific port. ... sendtoon a specific address and port on a socket that has been bound to ... bogus exit code) and adds a sleep after send failure. ... unless HZ is too small or the queue is too large. ...
    (freebsd-net)
  • Re: RMI binding to SAME port but DIFFERENT IP address on SAME host
    ... A socket is identified by its IP address, its port number, and its ... via the Sockets API. ... > port number for each listening interface, ...
    (comp.lang.java.programmer)