Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Chat (Client-Server)
#1
Hi guys. I have the following code (in Python 3.7.3):

###################################################################################################
### Librerias ###
#################
import socket
import threading
import sys
###################################################################################################
### Servidor ###
################
class Server:
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	connections = []
	def __init__(self):
		self.sock.bind(('0.0.0.0', 10000))
		self.sock.listen(1)

	def handler(self,c,a):
		while True:
			data = c.recv(1024)
			for connection in self.connections:
				connection.send(data)
			if not data:
				print(str(a[0]) + ':' + str(a[1]), "Desconectado")
				self.connections.remove(c)
				c.close()
				break
	def run(self):
		while True:
			c,a = self.sock.accept()
			cThread = threading.Thread(target=self.handler, args=(c,a))
			cThread.daemon = True
			cThread.start()
			self.connections.append(c)
			print(str(a[0]) + ':' + str(a[1]), "Conectado")
###################################################################################################
### Cliente ###
###############
class Client:
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

	def sendMsg(self):
		while True:
			self.sock.send(bytes(input(""),'utf-8'))

	def __init__(self):
		self.sock.connect((address, 10000))

		iThread = threading.Thread(target=self.sendMsg)
		iThread.daemon = True
		iThread.start()
		
		while True:
			data = self.sock.recv(1024)
			if not data:
				break
		print(data)
###################################################################################################
### Ejecucion ###
#################
if (len(sys.argv) > 1):
	client = Client(sys.argv(1))
else:
	server = Server()
	server.run()
###################################################################################################
The Server code runs OK:

İmage


But the Client code does not:

İmage


I am following this Video Tutorial:





Thank you very much for your help.
Quote
#2
sys.argv stores the values in list structure, hence to access it we have use index

###################################################################################################
### Ejecucion ###
#################
if (len(sys.argv) > 1):
    client = Client(sys.argv(1))
Modify that to, client = Client(sys.argv[1])
Quote
#3
(Aug-26-2019, 05:04 AM)Malt Wrote: sys.argv stores the values in list structure, hence to access it we have use index

###################################################################################################
### Ejecucion ###
#################
if (len(sys.argv) > 1):
    client = Client(sys.argv(1))
Modify that to, client = Client(sys.argv[1])

Thank you. Problem solved but now I get another error: Doh

İmage
Quote
#4
You are creating an instance of class Client with one argument
client = Client(sys.argv[1])
And your Client class initialization is defined as:
class Client:
    def __init__(self):
        self.sock.connect((address, 10000))
It seems you want to create the Client instance with the IP address
so the __init__ needs to be modified:
class Client:
    def __init__(self, address):
        self.sock.connect((address, 10000))
Malt likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  simple udp server/client cardmaker 2 169 Nov-26-2019, 12:36 AM
Last Post: micseydel
  Slow websockets server -> client pattagghiu 0 203 Sep-17-2019, 09:28 AM
Last Post: pattagghiu
  Python server(Django web site)/client(Python app) connection Junior_Pythoneer 5 418 Jul-05-2019, 05:41 PM
Last Post: noisefloor
  How to combine data taken from server between client and GUI? bescf 9 907 Apr-02-2019, 11:48 AM
Last Post: bescf
  tcp server/client port connection issue valshev 2 2,172 Sep-01-2018, 02:50 PM
Last Post: valshev
  Send data BMP180 between client and server trought module socket smalhao 0 780 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Sending/Receiving Multiple Message from Server or Client Lyperion 0 1,474 Jul-30-2018, 07:52 AM
Last Post: Lyperion
  Need help to make a server/client for DNS Eddie05 1 951 Jun-12-2018, 10:04 AM
Last Post: j.crater
  Simple send and recive string Server/Client Epilepsy 1 1,098 May-01-2018, 08:17 PM
Last Post: ThiefOfTime
  python socketio-client-2 and nod js socketio server communication is not success vmyadhu2 2 3,563 Oct-02-2017, 04:22 AM
Last Post: vmyadhu2

Forum Jump:


Users browsing this thread: 1 Guest(s)