0

After that I have connected to the server from input, how do I change my server in the chat?

I just updated the code with something that could work though it needs some more work, anyone?

def send(event=None): # event is passed by binders.
 """Handles sending of messages."""
 global HOST
 global PORT
 global ADDR
 
 msg = my_msg.get()
 my_msg.set("") # Clears input field.
 msg_list1 = msg.split()
 
 try:
 if msg_list1 [0] == "/connect":
 try: 
 HOST = msg_list1[1]
 PORT = int(msg_list1[2])
 ADDR = (HOST,PORT)
 client_socket.connect(ADDR)
 receive_thread = Thread(target=receive)
 receive_thread.start()
 
 except TypeError:
 msg_list_tk.insert(tt.END, "Error: please write '/connect ADDR PORT' to connect to server\n")
 
 if msg_list1 [0] == "/connectnew":
 HOST = msg_list1[1]
 PORT = int(msg_list1[2])
 ADDR = (HOST,PORT)
 client_socket_2.connect(ADDR)
 receive_thread = Thread(target=receive)
 receive_thread.start()
 except:
 msg_list_tk.insert(tt.END, "Error: please write '/connect ADDR PORT' to connect to server\n")
 elif msg == "/q":
 root.quit()
 client_socket.send(b"/q")
 
 elif msg == "/disconnect":
 client_socket.close()
 
 else:
 client_socket.send(bytes(msg, "utf8"))
 except:
 msg_list_tk.insert(tt.END, "Wrong input\n")
asked Feb 14, 2021 at 21:55
10
  • What happens when you call client_socket.connect again with a new address (or use a second client)? Commented Feb 14, 2021 at 22:12
  • Do you mean what happens when I try to connect to a server after the first one in the current program? Commented Feb 14, 2021 at 22:21
  • 1
    Note: We don't understand what you are connecting to. But closing the current client_socket, or making a new one and calling connect() on it with a different server address is the answer to "how to change the server" Commented Feb 14, 2021 at 22:22
  • Right now I am basically connecting to my own computer if that would be in your interest(127.0.0.1 5005), how would that look like in the code(your suggestion)? Commented Feb 14, 2021 at 22:29
  • Well, do you want to stop the receive_thread and completely disconnect, or just have more than one server connection open and getting messages? Commented Feb 14, 2021 at 22:36

1 Answer 1

1

A TCP socket is only usable for a single TCP connection. If you want a second connection, you need to create a new socket and call connect() on that (i.e. you can't call connect() on your old socket a second time).

answered Feb 15, 2021 at 2:28
Sign up to request clarification or add additional context in comments.

4 Comments

So it is basically a new client?
@sasasa It's a new socket. Clients are something you made up in your program, not something to do with sockets, so that's a question for yourself.
client_socket = socket(AF_INET, SOCK_STREAM) client_socket_2 = socket(AF_INET, SOCK_STREAM)
Something like this? I might need some help:)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.