This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-12-22 17:45:40

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Python Socket Problem

When I have successfully connected to a socket like this:

Code:

import socket
network = socket.socket()
network.connect_ex(("my ip", 61111))

I am trying to send something to the port like this:

Code:

network.send("Hi")

But I get an error!

Python Shell wrote:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    network.send("hi")
error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

HELP!  yikes


Fork Clamor on GitHub!

Offline

 

#2 2011-12-22 17:48:54

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Python Socket Problem

ohaiderstudios wrote:

When I have successfully connected to a socket like this:

Code:

import socket
network = socket.socket()
network.connect_ex(("my ip", 61111))

I am trying to send something to the port like this:

Code:

network.send("Hi")

But I get an error!

Python Shell wrote:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    network.send("hi")
error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

HELP!  yikes

Out of curiosity, what is connect_ex()?

Offline

 

#3 2011-12-22 17:50:35

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: Python Socket Problem

Magnie wrote:

ohaiderstudios wrote:

When I have successfully connected to a socket like this:

Code:

import socket
network = socket.socket()
network.connect_ex(("my ip", 61111))

I am trying to send something to the port like this:

Code:

network.send("Hi")

But I get an error!

Python Shell wrote:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    network.send("hi")
error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

HELP!  yikes

Out of curiosity, what is connect_ex()?

It's like connect, but returns a warning code instead of an exception when it fails!

Last edited by ohaiderstudios (2011-12-22 17:52:40)


Fork Clamor on GitHub!

Offline

 

#4 2011-12-22 17:51:58

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: Python Socket Problem

Hang on, I think I may know the problem now...

This ruins everything!

Last edited by ohaiderstudios (2011-12-22 17:52:14)


Fork Clamor on GitHub!

Offline

 

#5 2011-12-22 18:16:15

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: Python Socket Problem

Okay, now I replaced "connect_ex" with "bind", and I connect fine, but when I try to send, I get the same error?

Does 'bind()' not keep the connection going or something?

Last edited by ohaiderstudios (2011-12-22 18:16:43)


Fork Clamor on GitHub!

Offline

 

#6 2011-12-22 18:17:27

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Python Socket Problem

ohaiderstudios wrote:

Okay, now I replaced "connect_ex" with "bind", and I connect fine, but when I try to send, I get the same error?

Does 'bind()' not keep the connection going or something?

Do you have a server running? Bind is for servers.

Offline

 

#7 2011-12-22 18:42:18

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: Python Socket Problem

Magnie wrote:

ohaiderstudios wrote:

Okay, now I replaced "connect_ex" with "bind", and I connect fine, but when I try to send, I get the same error?

Does 'bind()' not keep the connection going or something?

Do you have a server running? Bind is for servers.

Now I'm seriously confused...

Okay, let me try to understand:
The server binds to a port so that the client can connect to the server?


Fork Clamor on GitHub!

Offline

 

#8 2011-12-22 18:44:27

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Offline

 

#9 2011-12-22 18:46:55

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: Python Socket Problem

I DON'T NEED THAT!!!!  big_smile   big_smile   big_smile   big_smile
I FIGURED IT OUT!!!!  big_smile   big_smile   big_smile   big_smile
IT WORKS!!!!!!  big_smile   big_smile   big_smile   big_smile
YAAAAAAAAAAAAAAAAAAAAAAAAAY!!!!!!!!!!!!!!!!!!!!!!!!!!!

big_smile


I think...


Fork Clamor on GitHub!

Offline

 

#10 2011-12-22 20:08:30

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: Python Socket Problem

ohaiderstudios wrote:

I DON'T NEED THAT!!!!  big_smile   big_smile   big_smile   big_smile
I FIGURED IT OUT!!!!  big_smile   big_smile   big_smile   big_smile
IT WORKS!!!!!!  big_smile   big_smile   big_smile   big_smile
YAAAAAAAAAAAAAAAAAAAAAAAAAY!!!!!!!!!!!!!!!!!!!!!!!!!!!

big_smile


I think...

How? Please post so other people can see how you did it for future reference.


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#11 2011-12-22 23:39:24

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: Python Socket Problem

midnightleopard wrote:

ohaiderstudios wrote:

I DON'T NEED THAT!!!!  big_smile   big_smile   big_smile   big_smile
I FIGURED IT OUT!!!!  big_smile   big_smile   big_smile   big_smile
IT WORKS!!!!!!  big_smile   big_smile   big_smile   big_smile
YAAAAAAAAAAAAAAAAAAAAAAAAAY!!!!!!!!!!!!!!!!!!!!!!!!!!!

big_smile


I think...

How? Please post so other people can see how you did it for future reference.

Basically, in order to open a port for clients to connect to, you need to use

Code:

network = socket.socket()
network.bind((ip, port))
network.listen(backlog)

As Magnie pointed out, "bind()" is only used for the server side, so the client uses

Code:

sock = socket.socket()
sock.connect((ip, port))

It was just a little bit of confusion on my part. Magnie could probably explain it better.  smile


Fork Clamor on GitHub!

Offline

 

Board footer