Python Forum
Socket won't connect, giving me a typeerror
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Socket won't connect, giving me a typeerror
#1
I am trying to establish a connection to server.py but client.py outputs this error

Error:
Traceback (most recent call last): File "C:\Users\Nathan\Desktop\Coding\Langs\Python\Projects\Chatting Program\Client.py", line 15, in <module> clientsocket.connect((host, port)) # Connects to the server TypeError: an integer is required (got type str)
Here is my code...

from socket import *
import socket

## -- Server (Room) -- ##
host = input("Host: ")
port = input("Port: ")
#int(port)

username = input("Username: ")
username = "<" + username + ">"
print(f"Connecting under nick \"{username}\"")

clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Creates socket
clientsocket.connect((host, port)) # Connects to the server

while True:
    Csend = input("<MSG> ") # Input message
    Csend = f"{username} {Csend}" # Add username to message
    clientsocket.send(Csend) # Send message to ONLY the server
I have tried converting host and port into str, int, and float, but it only successfully converts into str. Any help would be greatly appreciated.
Reply
#2
It seems that you did not convert to int (or did it badly) because below code works properly.
clientsocket.connect((host, int(port)))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  socket.connect question about parenthesis pace 1 2,321 Jan-30-2021, 08:17 AM
Last Post: Gribouillis
  python socket connect only sometimes espDino 0 1,502 Jul-15-2020, 12:13 PM
Last Post: espDino

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020