Python Forum
TCP socket, data transnission
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TCP socket, data transnission
#1
Hi guys, I was trying to get the user input from the command line (ubuntu to be precise) so like when a person enters the command to run the program with arguments (port number, buffer length & IP address) the program runs the client part of it. client then takes the length of buffer, takes the port number and ip from command line and assigns to the variables. it then generates a random number between 1 and 100 and sends to the port entered. Then when they enter the command to run the server side code with argument (port number only), the server listen to the port and receives the data and sends it back to the client.

the buffer length is to specify how much data the client will transmit to server.

the commands are as follows.

./clientServer.py –t –p 5001 –l 1000 –ip 127.0.0.1
where -t to run the client part of code
-p to indicate port number
-l for buffer length
-IP for loop back address. (in real environment the same code will run on 2 different machines, one will run the client part of code and one will run the server part of code)

./clientServer.py –r –p 5001
where -r to run the server part of code
-p for the port number



but i am having issues in running the file.
like it acknowledges the connection on server side but seems like client part is stuck in an infinite loop. can anyone point out and give me advice for that please?

so if anyone can help me i would really appreciate that and will be really thankful to you.



import socket
import time
import argparse
from random import randint


clientSock= socket.socket(socket.AF_INET, socket. SOCK_STREAM)

#here is the argv pasrse 

parser = argparse.ArgumentParser(description = 'sending data to server and back to client')
parser.add_argument('-t', action='store_true', help='transmission')
parser.add_argument('-r', action='store_true', help='recieving')
parser.add_argument('-ip', default=('127.0.0.1'), help='this is ip address')
parser.add_argument('-p', type=int, default=5001, help='this is port number')
parser.add_argument('-l', type=int, default=8192, help='this is buffer size')


args = parser.parse_args()

hostAddr = args.ip
port = args.p 
bufferLenght = args.l 


if args.t:
        clientSock.connect((hostAddr, port))
        buffcounter = 0
        dataOutBytes = ''
        for x in range (bufferLenght):
                number = randint(1,9)
                dataOutStr= bin(number).replace("0b", "")
                dataOutBytes+= dataOutStr
                        
        dataOutBytes = dataOutStr.encode()       
        numBytes= len(dataOutBytes)
        start = time.time()
        clientSock.sendall(dataOutBytes)

#============================================================================
        #loop until all data recieved 
        dataInBytes= b''
        while len(dataInBytes) < numBytes:
                dataInBytes+= clientSock.recv(numBytes-len(dataInBytes))

                if not dataInBytes:
                        break

        dataInStr= dataInBytes.decode()
        remoteAddr= clientSock.getpeername()
        print( "received", dataInStr, "back from server", remoteAddr)
        print ("it took ", time.time()-start)

        clientSock.close()
      
elif args.r:

	serverSock= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	serverSock.bind(('', port))
	serverSock.listen()

	print ("The server is ready to receive!")


	while True:

		connectSock, clientAddr= serverSock.accept()
		print( "Connection from", clientAddr)

		while True:   # keep receiving until client close
			
			message = connectSock.recv(1024)		
			
			if not message:
				break
			    
		connectSock.sendall(message)

	connectSock.close()
Reply


Messages In This Thread
TCP socket, data transnission - by Simba - Dec-06-2019, 02:16 PM
RE: TCP socket, data transnission - by DeaD_EyE - Dec-06-2019, 08:54 PM
RE: TCP socket, data transnission - by Simba - Dec-06-2019, 11:47 PM
RE: TCP socket, data transnission - by DeaD_EyE - Dec-07-2019, 04:16 AM
RE: TCP socket, data transnission - by Simba - Dec-07-2019, 03:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Socket to stream data and tranmit and revieve data kiyoshi7 0 2,414 Aug-11-2022, 10:52 PM
Last Post: kiyoshi7
  how to split socket data onto multiple clients for separate processing ConcealedFox70 0 1,973 Jan-11-2022, 08:26 PM
Last Post: ConcealedFox70
  socket without blocking loop and automatically retrieve data from the port RubenP 3 3,646 Jun-21-2020, 10:59 PM
Last Post: Gribouillis
  Soft Access Point & Socket Data Streaming groger57 1 2,570 Aug-01-2019, 02:53 PM
Last Post: groger57
  Send data BMP180 between client and server trought module socket smalhao 0 2,873 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Python socket : Error receive data quanglnh1993 1 13,075 Mar-14-2018, 11:59 AM
Last Post: avorane
  Socket won't receive data suddenly with multiple clients SquareRoot 0 2,816 Sep-06-2017, 09:09 PM
Last Post: SquareRoot
  socket server with SSL accepting unencrypted data kopite 0 3,302 Apr-20-2017, 08:31 AM
Last Post: kopite

Forum Jump:

User Panel Messages

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