1

I had my Windows computer connect to my linux python server.

The server works very well for linux users, but I want it to run on Windows as well. I did some research and found out that the select.select() function on Windows works without a stdin pipe, but when I do it it, it doesn't send any data to the server, only receives.

Code:

 __author__ = 'root'
import socket
import datetime
import getpass          # Get username
import sys
import threading
import thread
import select

def client():

   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
   address = "192.168.1.17"
   sock.bind((str("192.168.1.231"), int(2222)))
   sock.connect(("192.168.1.17", int(23657)))
   sock.setblocking(0)
   sock.send(getpass.getuser()+"[$ID$]zivofek")

   socket_list = [sys.stdin, sock]
   partner = raw_input("Chat partner: ")

   while 1:
       try:
           message = raw_input()
           sock.sendall(message)
           data = sock.recv(1024)
           print data
       except:
           continue

def get_time():
   return datetime.datetime.strftime(datetime.datetime.now(), '%H:%M:%S')

def main():
   client()

if __name__ == "__main__":
   main()

Any idea of how to make both receiving and sending at the same time in Windows?

martineau
  • 119,623
  • 25
  • 170
  • 301
Idan Ofek
  • 87
  • 1
  • 2
  • 13
  • `select` by itself doesn't send anything. Do you mean you don't get "write" events? Again, posting the code that exhibits the problem is the best option. – Nikolai Fetissov May 26 '15 at 19:24
  • Actually- i don't have a client code in windows. I basically wants to make the client to be able to receive and send to the server.. in any way. In python it worked with simple select- when writable it writes, when readable it reads. Im looking for a way to implement it in windows, by far I only managed to do one at a time, not both. – Idan Ofek May 26 '15 at 19:30
  • Tried python `socket.sendall()` function? – Nikolai Fetissov May 26 '15 at 19:37
  • To be honest I didn't, tried now- Raises an error: A non-blocking socket operation could not be completed immediately. Do you mind to give an example? Thank you very much for your help! – Idan Ofek May 26 '15 at 19:57
  • Do you mind showing your code? We are not free tutors here, you are supposed to do your own work :) – Nikolai Fetissov May 26 '15 at 20:06
  • Further, TCP socket is always writable unless its send buffer is full, which usually means slow receiver. So don't just always add your socket to the "write" select set, but only when you get `EAGAIN` on a non-blocking socket. – Nikolai Fetissov May 26 '15 at 20:11
  • So if i understand you right- that should work? – Idan Ofek May 26 '15 at 20:21
  • Yes, it should. `select()` is not broken :) – Nikolai Fetissov May 26 '15 at 20:24

2 Answers2

0

Do not set the socket non-blocking unless you really want to multiplex events from different sources (i.e. when not using select()).

Here just normal blocking sendall() will do. Also check return length of the recv() - it returns when some data is available, and receiving zero bytes means the other end closed the connection.

Again, don't bind() or set SO_REUSEADDR on client TCP sockets.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
0
socket_list = [sys.stdin, sock]

Assuming the purpose of the above socket_list is to pass it in to select() ... this isn't going to work. The Windows implementation of select() only works with sockets, and sys.stdin is not a socket.

There's no easy fix for this problem(*); that said, one way to paper over the problem is by reading from sys.stdin in a separate thread, and having that thread send() the data to one end of a connected socket pair, and place the other end of the pair into the list you are passing to select(). Alternatively, you could put a timeout into select() so that select() returns every so often to let your even loop check to see if there are any bytes available on stdin (although this mechanism will either be CPU-inefficient or have high latency, depending on the timeout value you pass to select() -- whether or not that is acceptable will depend on your particular usage scenario)

(*) Okay, there is one easy fix: don't bother trying to run the program in Windows... ;)

Community
  • 1
  • 1
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234