7

I am writing a simple socket-based chat program that allows the server to send and receive message to the client. The client can send message to server but when I try sending a message from the server, it crashes saying that 'file' objects has attribute 'recv'.

Server.py

import socket
import os
import select
import sys

def prompt():
    sys.stdout.write('<You> ')
    sys.stdout.flush()

try:
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except:
    print 'Failed to create socket'
    sys.exit()

PORT = 9050
HOST = '127.0.0.1'
RECV_BUFFER = 4096

server_socket.bind((HOST, PORT))
server_socket.listen(10)

input = [server_socket, sys.stdin]

print 'Chat Program'
prompt()

while 1:

    inputready, outputready, exceptready = select.select(input,[],[])

    for sock in inputready:

        if sock == server_socket:
            client, address = server_socket.accept()
            input.append(client)
            #data = sock.recv(RECV_BUFFER)
            #if data:
                #sys.stdout.write(data)
        else:
            data = sock.recv(RECV_BUFFER)
            if data:
                sys.stdout.write(data)
            else:
                msg = sys.stdin.readline()
                server_socket.send('\r<Server>: ' + msg)
                prompt()



server_socket.close()

Client.py

import socket
import os
import select
import sys

def prompt():
   sys.stdout.write('<You> ')
   sys.stdout.flush()

HOST = '127.0.0.1'
PORT = 9050
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print 'Failed to create socket'
    sys.exit()

s.connect((HOST, PORT))

print 'Connected to remote host. Start sending messages'
prompt()

while 1:

    socket_list = [sys.stdin, s]

    read_sockets, write_sockets, error_sockets = select.select(socket_list, [], [])

    for sock in read_sockets:
        if sock == s:
            data = sock.recv(4096)
            if not data:
                print '\nDisconnected from chat server'
                sys.exit()
            else:
                sys.stdout.write(data)
                prompt()
        else:
            msg = sys.stdin.readline()
            s.send('\r<Client>: ' + msg)
            prompt()
1
  • This isn't peer to peer because it has a server Commented Feb 23, 2014 at 6:00

2 Answers 2

4

Well, in your server you are doing (abbreviated)

input = [server_socket, sys.stdin]
inputready, outputready, exceptready = select.select(input,[],[])
for sock in inputready:
    if sock == server_socket:
        ...
    else:
        data = sock.recv(RECV_BUFFER)

So when something comes in on sys.stdin, its not server_socket so it goes to the else and tries to recv, but its not a socket. stdin should be using read not recv. Structure like the one below makes the most sense to me.

if sock == server_socket:
    ...
elif sock == sys.stdin:
    data = sock.readline()
    for s in input:
        if s not in (server_socket, sys.stdin):
            s.send(data)
else:
    ...
Sign up to request clarification or add additional context in comments.

4 Comments

so I should have a if statement that hands something from the keyobard like: if sock == stdin: msg = sys.stdin.readline() server_socket.send() prompt()
sure, any way to keep the stdin processing seperate
alright now I am getting broken pipe error [Errno 32] from the server_socket.send('\r<Server>: ' + msg). Do I need to connect to the client from the server? Thanks
you want to send on connected clients, not the listening socket. adjusted answer to show how
0

I tried the similar type of program from the link provided below, but problem was select() was not supported in windows operating system.

File objects on Windows are not acceptable, but sockets are. On
Windows, the underlying select() function is provided by the WinSock
library, and does not handle file descriptors that don’t originate
from WinSock.

http://www.binarytides.com/code-chat-application-server-client-sockets-python/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.