0

I am new to python program. And I written a client/server program for adding two numbers. Client give two numbers and server adds up. While running, I am hitting with the above error on the client side.

Client.py

!/usr/bin/python

import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('127.0.0.1', 58817)
sock.connect(server_address)

a=raw_input("Enter a number: ")
b=raw_input("Enter a number: ")

sock.sendall(a,b)
data = sock.recv(1024)

print data

sock.close()

Server.py

!/usr/bin/python

import socket
import sys

def sum(a, b):
  data=a + b
  return data

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('127.0.01', 58817)
sock.bind(server_address)
sock.listen(1)
connection, client_address = sock.accept()

print ("connection from", client_address)

while True:
 data=connection.recv(1024)
 if not data: break
 print "server received : ", repr(data)
 sum(data)

connection.sendall(data)
connection.close()

while running the program, i am hitting with below error in client side

Enter a number: 4
Enter a number: 3

Traceback (most recent call last):



File "clientadd.py", line 15, in <module>
    sock.sendall(a,b)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
TypeError: an integer is required

There is some mistake with client side assigning int (a,b). How to change that one and put in some order as read by server ? Can anyone help me on this ? and can check if the program is good and in working condition to get output ?

Thanks!

2
  • sock.sendall(a,b)...this is the problem. You can't send any data type other than bytes over network. Commented Feb 16, 2017 at 5:35
  • How to resolve this one? Any tips to rephrase ? I need to pass two datas to server and get one back as addition. so how it is possible way to pass sendall (a,b) in another way ? Commented Feb 16, 2017 at 5:38

2 Answers 2

1

Remember, you can only send or receive bytes over socket. So, both your client and socket needs to reflect that.

On client side, instead of sendall(a,b), you can:

b=bytes("{},{}".format(a,b),"ascii")
sock.sendall(b)

On server side, parse the string to get integers.

data=connection.recv(1024)
a,b = data.decode().split(',')
a,b = int(a),int(b)

You should be able to send back the sum as bytes now.

Sign up to request clarification or add additional context in comments.

2 Comments

While trying the code, it saying me as Traceback (most recent call last): File "clientadd.py", line 15, in <module> c=bytes("{},{}".format(a,b),"ascii") TypeError: str() takes at most 1 argument (2 given)
works fine with me.I have thought hard. But can't find the problem. Maybe python2 issue I am not aware of.maybe.
0

You're server_address on the server side has a typo. It should be ('127.0.0.1', 58817) the client will give an error, because the server it is trying to connect to doesn't exist.

1 Comment

Hi, I have made the changes to server_address = ('127.0.0.1', 58817). But still i can see the error hitting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.