1
votes

It's my client:

#CLIENT
import socket
conne = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conne.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
i=0
while True:
    conne.connect ( ('127.0.0.1', 3001) )
    if i==0:
        conne.send(b"test")
        i+=1
    data = conne.recv(1024)
    #print(data)
    if data.decode("utf-8")=="0":
        name = input("Write your name:\n")
        conne.send(bytes(name, "utf-8"))
    else:
        text = input("Write text:\n")
        conne.send(bytes(text, "utf-8"))
    conne.close()

It's my server:

#SERVER

import socket

counter=0
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 3001))
sock.listen(10)

while True:
    conn, addr = sock.accept()
    data = conn.recv(1024)
    if len(data.decode("utf-8"))>0:
        if counter==0:
            conn.send(b"0")
            counter+=1
        else:
            conn.send(b"1")
            counter+=1
    else:
        break
        print("Zero")
        conn.send("Slava")
    conn.close()
))

After starting Client.py i get this error:

Traceback (most recent call last): File "client.py", line 10, in conne.connect ( ('127.0.0.1', 3001) ) OSError: [Errno 9] Bad file descriptor

Problem will be created just after first input. This program - chat. Server is waiting for messages. Client is sending.

2
Full traceback error please - Ofer Sadan
once i found the same error, it was because of closing the Python file from "the outside", i.e. not from the file object's close(), you may check the same. - ggupta
it's full error - Yehor Naumenko
I ran both your programs, the error isn't when starting "Client.py"at all, it's after the input of name, so the line responsible for the error is probably conne.send(bytes(name, "utf-8")). I can't help any more than that sadly, but you might want to edit your question with that information. Also, you should write a sentence or two on what this program is supposed to achieve - Ofer Sadan
@ggupta i am contorrilng server's work. - Yehor Naumenko

2 Answers

6
votes

There are a number of problems with the code, however, to address the one related to the traceback, a socket can not be reused once the connection is closed, i.e. you can not call socket.connect() on a closed socket. Instead you need to create a new socket each time, so move the socket creation code into the loop:

import socket

i=0
while True:
    conne = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    conne.connect(('127.0.0.1', 3001))
    ...

Setting socket option SO_BROADCAST on a stream socket has no affect so, unless you actually intended to use datagrams (UDP connection), you should remove the call to setsockopt().

At least one other problem is that the server closes the connection before the client sends the user's name to it. Probably there are other problems that you will find while debugging your code.

-1
votes

Check if 3001 port is still open.

You have given 'while True:' in the client script. Are you trying to connect to the server many times in an infinite loop?