albertHankock 0 Newbie Poster

So I need to write an ping client and server for my networking class. Server code is running no problem but I'm a bit lost on my client code. I copy and pasted the server code because I figured it would be easier to modify.. Please check this out.....THE PROBLEM IS LINES 43 AND 44 IN THE CLIENT CODE. IDK WHAT IT IS THO

Server:

import socket
import random # to create random numbers
import time # for putting the process to sleep (suspend)

PING_REQUEST_SIZE = 1024

class PingServer:

   def __init__(self, port):
      self.port = port
      self.__LOSS_RATE = 0.5
      self.__AVERAGE_DELAY = 100  # @@@milliseconds

#  
#  Print ping data to the standard output stream.
# 
   def printData(self, request, address):
      print("Printing ping request...")

      # request is a byte array. Convert it to an str before printing
      strRequest = request.decode("utf-8")

      # Print host address and data received from it.
      print("Received from host: " + str(address[0])
            + " port: " +  str(address[1])
            + " data: " + strRequest)


   def run(self):
      try:
         # Create a datagram socket for receiving and sending UDP packets
         serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         # bind the socket to a specific port of the local machine
         serverSocket.bind(("localhost", self.port))

         print("Started Ping Server on host: "
               + str(serverSocket.getsockname()[0])
               + " port: " + str(serverSocket.getsockname()[1]) + ".")

         while True:
            print("------------------------\nWaiting for ping request...")

            request, clientAddress = serverSocket.recvfrom(PING_REQUEST_SIZE)
            print("Received a ping request.")

            # Print the recieved data.
            self.printData(request, clientAddress)

            # Decide whether to reply, or simulate packet loss.
            if (random.random() < self.__LOSS_RATE):
               print("Dropped (lost) ping request. No reply will be sent.")
               continue 


            # Simulate network delay.
            time.sleep(random.random() * 2 * self.__AVERAGE_DELAY)

            # Send reply: echo with same data
            serverSocket.sendto(request, clientAddress)
            print("Sent the ping reply.")

      except Exception as exception:
         print(exception)
         serverSocket.close()
         print("...Quiting the program.")


def main():
   port = eval(input("Enter the port number to start the Ping server: "))

   pingServer = PingServer(port)
   pingServer.run()

main()

Now the client:

import socket
import random # to create random numbers
import time # for putting the process to sleep (suspend)

PING_REQUEST_SIZE = 1024

class PingClient:

   def __init__(self, port):
      self.port = port
  #    self.__LOSS_RATE = 0.5
  #    self.__AVERAGE_DELAY = 100  # @@@milliseconds

#  
#  Print ping data to the standard output stream.
# 
   def printData(self, request, address):
      print("Printing ping request...")

      # request is a byte array. Convert it to an str before printing
      strRequest = request.decode("utf-8")

      # Print host address and data received from it.
      print("Received from host: " + str(address[0])
            + " port: " +  str(address[1])
            + " data: " + strRequest)


   def run(self):
      try:
         # Create a datagram socket for receiving and sending UDP packets
         clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         # bind the socket to a specific port of the local machine
         clientSocket.bind(("localhost", self.port))

         print("Started Ping Server on host: "
               + str(clientSocket.getsockname()[0])
               + " port: " + str(clientSocket.getsockname()[1]) + ".")

         while True:
            print("sending...")

            request, serverAddress = clientSocket.sendto("text", clientSocket.getsockname())
            print("sending ping...")

            # Receive reply
            clientSocket.recvfrom(request, serverAddress)
            print("Ping returned.")

            # Print the recieved data.
    #        self.printData(request, clientAddress)

            # Decide whether to reply, or simulate packet loss.
   ##         if (random.random() < self.__LOSS_RATE):
   ##            print("Dropped (lost) ping request. No reply will be sent.")
   ##            continue 


            # Simulate network delay.
    ##        time.sleep(random.random() * 2 * self.__AVERAGE_DELAY)

            # Send reply: echo with same data
    ##        serverSocket.sendto(request, clientAddress)
    ##        print("Sent the ping reply.")

      except Exception as exception:
         print(exception)
         clientSocket.close()
         print("...Quiting the program.")


def main():
   port = eval(input("Enter the port number: "))

   pingClient = PingClient(port)
   pingClient.run()

main()

When I run the client, I get:

Enter the port number: 2222
Started Ping Server on host: 127.0.0.1 port: 2222.
sending...
'str' does not support the buffer interface
...Quiting the program.

Any help is greatly appreciated, or providing links to where I can find more information on this is nice too!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.