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
#2
Line 77 connectSock.sendall(message) is indented one level too less.
You're never sending data.

Output:
deadeye@nexus ~/sserver_test $ python tt.py -t received 100 back from server ('127.0.0.1', 5001) it took 6.389617919921875e-05 deadeye@nexus ~/sserver_test $ deadeye@nexus ~/sserver_test $ python tt.py -t received 1 back from server ('127.0.0.1', 5001) it took 0.000110626220703125 deadeye@nexus ~/sserver_test $
Output:
deadeye@nexus ~/sserver_test $ python tt.py -r The server is ready to receive! Connection from ('127.0.0.1', 59644) Connection from ('127.0.0.1', 59708)
Try to improve your code. Put the different parts into functions.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
so that client side code is fine just server is not sending anything back?
Reply
#4
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: # <-- Eventloop of server
 
        connectSock, clientAddr= serverSock.accept()
        print( "Connection from", clientAddr)
 
        while True:   # keep receiving until client close
                      # the next call blocks
            message = connectSock.recv(1024)        
             
            if not message:  # <-- Exit the loop if no message
                break
                 
            connectSock.sendall(message) # <-- was not indented
            # so it never sent data 
 
    connectSock.close()
The code itself allows only one connections at the same time.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
you are a Gem sir!!

thanks a lot

the last question is that how can i run the client side to make the same length of data as defined by the user in buffer.
so like in command when user says -l 1000, i want the code to make 1000 random numbers and send to the server and then server can send it back.
I've tried the following but still doesn't work.

 if args.t:
                try:
                        clientSock.connect((hostAddr, port))
                        buffcounter = 0
                        count = 0
                        dataOutBytes = ''
                        while bufferLenght > 0:
                                number = randint(1,999)
                                dataOutStr= bin(number).replace("0b", "")
                                dataOutBytes+= dataOutStr
                                bufferLenght = bufferLenght - 1
                                count = count + 1 #just to make sure it runs as long as buffer
                                print (count)
                                
                        dataOutBytes = dataOutStr.encode()       
                        numBytes= len(dataOutBytes)
                        print ("number of bytes to be sent", numBytes)
                        start = time.time()
                        clientSock.sendall(dataOutBytes)
this just sends the last number that was produced by the loop. like is it not appending the string?

like i want to make a string of data so like each time this loop runs (1000 times) it adds the new number into existing string. so like the string have 1000 numbers init (because buffer length is 1000) and then it should be transmitted to server.

the loop is running as long as buffer size , but the string only have the last number that was produced by the
 randint() 
function.

any suggestions on that please ?

Output:
C:\Users\m\Desktop>clientserver.py -t -p 5050 -l 1000 -ip 127.0.0.1 1101010000100010011111010111111000110110110011100101010100111111011010100101100111110000101100101101100101011101001001011101010011111010111001111101110011101101111110011010000011010100011011001001010101111111101001010111010001011001101110101111111010111110001111111001011100001101011100011000011001010010010000001010100100111010010110101011101000101111101000111100110011000110111011011110101001111111001001101010101001000110111010010110110001110110010111011010100100011001011001100010111100101011100110011110111110110110100101111001100001110011001011111110000011010001100110111001110101011011010010111011111011111000000110001000111110101111111100100110010111110001100111100011110100111101100101101101101101011001010000010110010010011101010001010110011001001011000011001111000110011101110111110000001100011010110110111101011001100001000100000101110011001111000011011111101101100011101110010111010001001110111010100001001110101000001000111011101111111101110011110111111001101011011101110110000110101110111001001110011101011001101010011011011001111001101111100101110111010111001101100010011011101001001011101101111110010110011000101101010011100001100111101011110101110110001110110100110010010010010010101011010111100100110101110100010011111000000110100110101110001110100000101110001011000001101000010010001000110101110011000001110110101000101100101101100111001011111101110011100111110110101110100110110011001010110111111011101010111001000010100101111010010011111011111101100110010001010101000000101101000011110111010011010001000100010101010111101011000100110110101100111100111101001000110110100111101000010101110011110100111001000110110011110001110100101100010001111011101011111100000010111100110111101101110011101101101111101001010110011011000101111110011011010000111111001010011000110111001101000100111110010110110110010011001010010101100110111000101001111001110100000011010100011110111101000101000110001000111000111110000010000101111011110111101110010010101001001000111000110111011101011011110011011011001011011100110100000101000110100010101101110111111010110101110111011000101010110001110100011100010000011100011001111000011100110001100010010010001011000100101100011100111101000101000101000101011000110001101011010110010001000100101100010101011001011111010010100000000111010111111010101111100111011001011001110011010111110001011001011010101110101111111010110011011010010101100100010101010011010111101101001010100110011101100001110010100001001111000001110111010010110101111011000100101011001001100101011100001101011010101011011100011111110101100100111101111110100000011101011100110000110111100100111011111010000111111001000111111101111110100111001010011010000011001011000010110110110110000111011111101000011111011000100011011010001010101101010001001010100101100111011000100110101101000011010000111101111001011011101110111100110100110111000100110101001010010101011010101001100000110010011011101010101100100011101101001111101110110000100001001001110001110110110011110010010000101001100001011111011111110010001100001001001111010001111011100011000111101001001111000111011010001001001010101100010100011010000110010101111100010001001010010100101010100010110110101010001001101011010011100001101111111110011010001010100011001110110011101100011101111011101110101100110011001011000011101111110011100000110111000101101001111100000001111001111110111011100111000011011111001110010011110110011101101111010101101110011101001001000111010001010011110000101010110010101100000111101011101110110010000001111000100110001101010101111111111101110010110011110011010101111001011010001111111010110111111001000101111101011100001111110010110100000101010100111110000011101100111011110110000110101111110011001010000100110001000100110101100011011111011101010010000011000001110000001001001110110001001001000001101001110010111101010000011101001011101000111101111111100011001010101001011010001001001011110111100000101111100010111000111110011110000001001011010000100001011110110101101111100111101110110001001110011111111100000011010110111100100011100011110111110111011111110010011111100110101011011111100100000000100010101100100111100110101001111010101010010010100000110111010111011101001000100110001111010110110110101000011111100001011001011111110101101111111011111011001010001010110001111110100101110000101000111111011111101000010011101010000110001101110111010011011010111101101101001110101111110101001101110101101110101010011011111100110010100101101110000101010110011111010111110101110100100100001001010100111101101010011110011010111010000011001011101001111111001101111110101011100011001001011100110011110000010101011100110101011101000110111011100101100010010101011101010111110101111101011000010001000110010100011010011000110100001100101111011111010110101011100011011110111000010001111010111101100101110000100100001011000011110101110000100110111000011010010001110110110101010110110000110111011101111000000100011011111100111101111100100001011000110001111110001111100101010001110111100001011001001001011100010110110000110100000111111111111100110111010101000111001100111110001011000100111111000100111111011001101001001000101100101010001111010000100111100011101011010100110011010110000010110111001101101100110110110111100110011011111101000110110001101101111100101010001101111111111011001010110001010101100101010011110010000110101101100011010110010111110011101101110000110100101101101111111101010111011111011110011011101001010010111110111110100001101111101000101000101111111111101001110111000101010000001011110000101011100100111101010111100011111010110000011100110110110000011111110101101101111011001001011001001101010111101101010101010110110001100110101000110000001010000111011100000110011000011011001111111101110000101001100110101111001011001101101010101000001011110101100101000111010010111001011100100110111010101110100101001001001111101111100110111010001101000101111100001000111011100011010101111000101110100101100110111101101110010101101010110011110010011110010111111011111111000101011111101100110111011011000111010011111100001001111101110011001010110001011010011000100011110111101000011010001110100001111001001001010111000110000011000011011000001111000100010011100001000100001011111010101111011000001100110111010110111100100101110110100111011000110111000101011011110101111010001111111001011011110000100111000101111100111010100111001111010111100111100011101101111011111101110010011111111101100010011011101001100000110100010001110001100010110001110111101010001111011000111101000010011011011011101110110100111110011001100111110111000110101110101101111111110111011110110110010010010100011100111011110010010011010011011100001100110010110100011010000111001010100010011010011010011100011100001101000010111011011001110100100111111111100101110010110110001010100001110111001010011110100111101001000100110000010100011001110010111101111110100100101000111110010001110011100001100011011101011011111101010110010010101110001000101110111100110101010100011111001011111001110011101001011011101011111000111010101100101110111111111000011011100001000010001001010010010111100010101100001001001110011101100100110100010111001011010000101011010011001000001110100010100010111111011011011110111001111111001111110001110100011010100001110110101011000011101001001111110101111010101110101100001010110011110110010010111010100110000111100111110000100110010111011001100100001100100100110110101011001000110000101001010010111100001111011010101001011000110111000001011010010101100010101010001010010001011110011101000110111001101100001100010011011110001010110101110111011100001101011000011111100010011110000001010000011001001101010010001101001011010011101101010011011111101010110011111111011110111101100001100001111111100010110101010110001010001000000111111011110000111001110111010000011001101000111010010110010001000011110100111011100101101001011110101101001100011110001111100101110011001101111100001110001111001100010010010001111110101000010101010011101011111111000101111111010100001101100101000111011110011110001110100011001110011001111011111001011111110110101101101111011101101111101101011110101111010100111011111001111111000011111111000011101100010111101111001111011011010101001001101111110111011110110110110011101101011001101110110011011010111101011100001011001001000101000001110101100110110101111010110010111001001011110100011101010101111001000110110011110111111101100001011001100111100100111111010101111110101001010011100010001000100100111100111101101111101110111101101101111011101111101001110101110111101011101101111001110111100010101111111100101000101111111000011101101110101011111001101110101010010101111111101011001110000101101111111111110010110101011110010011111011011101011010111010010100010100010011010111110101100111111110000110101110110011111000011011100011111001111100010010101010111000110101000010111101001001101001011110100100010011000010011101111011101000011011001010111011010000101101100100011100110110100111000101110110010101110011010111110110101100101000001110100000100101100110000111001110100 number of bytes to be sent 10 received 1001110100 back from server ('127.0.0.1', 5050) it took 0.0 sec. C:\Users\m\Desktop>
this was the output, but as you can see it only sent last 10 bits and server send them back. any suggestions to this issue ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Socket to stream data and tranmit and revieve data kiyoshi7 0 2,340 Aug-11-2022, 10:52 PM
Last Post: kiyoshi7
  how to split socket data onto multiple clients for separate processing ConcealedFox70 0 1,905 Jan-11-2022, 08:26 PM
Last Post: ConcealedFox70
  socket without blocking loop and automatically retrieve data from the port RubenP 3 3,488 Jun-21-2020, 10:59 PM
Last Post: Gribouillis
  Soft Access Point & Socket Data Streaming groger57 1 2,477 Aug-01-2019, 02:53 PM
Last Post: groger57
  Send data BMP180 between client and server trought module socket smalhao 0 2,805 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Python socket : Error receive data quanglnh1993 1 12,952 Mar-14-2018, 11:59 AM
Last Post: avorane
  Socket won't receive data suddenly with multiple clients SquareRoot 0 2,768 Sep-06-2017, 09:09 PM
Last Post: SquareRoot
  socket server with SSL accepting unencrypted data kopite 0 3,240 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