Vinod K
Vinod K

Reputation: 2123

Server Client Communication Python

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

Upvotes: 4

Views: 13283

Answers (4)

Johanna
Johanna

Reputation: 660

try to change the client socket to:

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

Upvotes: -1

Senthil Kumaran
Senthil Kumaran

Reputation: 56813

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.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409136

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()

Upvotes: 8

Marcin
Marcin

Reputation: 49816

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

Upvotes: 3

Related Questions