Server Client Communication Python

Go To StackoverFlow.com

4

Server

import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

host= 'VAC01.VACLab.com'
port=int(2000)
s.bind((host,port))
s.listen(1)

conn,addr =s.accept()

data=s.recv(100000)

s.close

CLIENT

import socket
import sys

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

host="VAC01.VACLab.com"
port=int(2000)
s.connect((host,port))
s.send(str.encode(sys.argv[1]))

s.close()

I want the server to receive the data that client sends.

I get the following error when i try this

CLIENT Side

Traceback (most recent call last): File "Client.py", line 21, in s.send(sys.argv[1]) TypeError: 'str' does not support the buffer interface

Server Side

File "Listener.py", line 23, in data=s.recv(100000) socket.error: [Errno 10057] A request to send or receive data was disallowed bec ause the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

2012-04-04 06:17
by Vinod K
I think you may have to convert the str to bytes - Words Like Jared 2012-04-04 06:19
Do i do this str.encode(sys.argv[1]) ?? i did that ,the errors have stopped but the data is not transferring - Vinod K 2012-04-04 06:45
Are you still having trouble - Words Like Jared 2012-04-04 14:35


8

In the server, you use the listening socket to receive data. It is only used to accept new connections.

change to this:

conn,addr =s.accept()

data=conn.recv(100000)  # Read from newly accepted socket

conn.close()
s.close()
2012-04-04 06:22
by Some programmer dude
Still giving the same error at the client side. The server error got resolved - Vinod K 2012-04-04 06:35
but their are <code>recv()</code> and <code>send()</code> methods on socket object - Mahesha999 2018-06-14 13:53


3

Your line s.send is expecting to receive a stream object. You are giving it a string. Wrap your string with BytesIO.

2012-04-04 06:21
by Marcin
Do i do this str.encode(sys.argv[1]) ?? i did that ,the errors have stopped but the data is not transferring - Vinod K 2012-04-04 06:42
@VinodK NO use BytesIO. Go look that up in the docs - Marcin 2012-04-04 07:05


0

Which version of Python are you using? From the error message, I guess you are unintentionally using Python3. You could try your program with Python2 and it should be fine.

2012-04-04 06:25
by Senthil Kumaran
Ya i am using Python - Vinod K 2012-04-04 06:29
Vinod - Try it with Python2 then to know the difference - Senthil Kumaran 2012-04-04 06:46


0

try to change the client socket to:

s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
2018-06-21 17:09
by Johanna
Ads