Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

I fail at python sockets, hard

Name: Anonymous 2008-03-28 9:30

I'm trying to make something that once it is disconnected will automatically try to reconnect.  I'll add some more features in later so it doesn't hammer the server but right now I just want to keep it simple and get that part working.  The problem is that when I use sock.close I get an error message of
Bad File Descriptor
and if I either use shutdown or just go straight to reconnecting I get:
Transport endpoint is already connected

This is what I've got right now:

#! /usr/bin/env python
import socket, string
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def doconn():
    sock.connect(("localhost", 1234))
def dodiscon():
    sock.close()
    doconn()

doconn()

while (1):
    buffer = sock.recv(1024)
    if not buffer:
        dodiscon()


What am I doing wrong?

Name: Anonymous 2008-03-28 10:23

Read SICP

Name: Anonymous 2008-03-28 11:53

[code] tags are there for a reason, for fuck's sake.

Name: Anonymous 2008-03-28 12:11

>>3
EXPERT BBCODE PROGRAMMER

Name: Anonymous 2008-03-28 12:30

>>3
one word, THE FORCED INDENTATION OF CODE, thread over.

Name: Anonymous 2008-03-28 12:33

strange

Name: Anonymous 2008-03-28 12:36

you clearly don't understand what the connect() actually does. Do some reading and try again. (Pro-tip: it's a shortcut to bind on a non-specified port)

Name: Anonymous 2008-03-28 12:46

>>5
FIOC won't help if you use a proportional font.

Name: Anonymous 2008-03-28 14:54

Just recreate the socket

Name: Anonymous 2008-03-28 15:03

Create a new socket for each connection instead of calling connect() again on the old one.
And while (1): ? Those parentheses are superfluous and misleading.
And dodiscon is convoluted, and inconsistent with doconn (note the n's). Put in the extra typing effort and give them full names, like do_disconnect. But it's not really a disconnect, it's a reconnect, so change the name to reflect that.

Name: Anonymous 2008-03-28 23:14

#! /usr/bin/env python
import socket, string
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def doconn():
    sock.connect(("localhost", 1234))
def dodiscon():
    sock.close()
    doconn()

doconn()

while (1):
    buffer = sock.recv(1024)
    if not buffer:
        dodiscon()

Name: Anonymous 2008-03-28 23:24

Sockets in general are just fucking retarded and impossible.

I really don't think I want to be a programmer anymore.

Name: Anonymous 2008-03-28 23:24

Good choice

Name: Anonymous 2008-03-28 23:32

While we're on the topic of sockets, I have a question for [spoiler]/prog/[spoiler]:

Say your UNIX application connects to some server, and is doing normal read and write stuff, and then suddenly the socket dies. What do you do? Do you try to reconnect, or do you abort the whole program with an error? Maybe you do some hybrid approach that tries to reconnect a couple times at increasing intervals of time, and then abort after a while.

Well [spoiler]/prog/[spoiler], tell me.

Name: Anonymous 2008-03-28 23:34

>>14
It depends on what the program is doing.

Name: Anonymous 2008-03-28 23:50

>>15
Wrong answer.

Name: Anonymous 2008-03-29 0:04

>>14
Totally DDOS the host with the string "DON'T FUCK WITH MY SOCKET\0"

Name: Anonymous 2008-03-29 0:06

>>17
That's not a string, it's an array of bytes.

Name: Anonymous 2008-03-29 0:07

>>16
Wrong reply.

Name: Anonymous 2008-03-29 0:13

Another question for the programmers of /prog/:

Say you have a data structure in C, and you have a function to malloc a new structure, and return it et cetera.

Do you create a function and get passed in a pointer to a pointer to a struct, or return a pointer to a struct:


int
ds_init(struct ds **ptr)
{
  *ptr = malloc(sizeof(**ptr));

  // Of course you can return more interesting integers for errors et cetera.
  return 1;
}

Name: Anonymous 2008-03-29 0:21

>>20
I use macros and call abort() if malloc fails, of course!

Name: Anonymous 2008-03-29 0:25

>>21
EXPERT PROGRAMMERs use exit()!

Name: Anonymous 2008-03-29 0:30

>>22
Did you mean ExitProcess(NULL, NULL, NULL, NULL, NULL)?

Name: Anonymous 2008-03-29 0:43

>>23
I'm sorry, I use Ubanto, the one true operating system!

Name: Anonymous 2008-03-29 1:25

I crossposted this to 10 different forums and eventually got an answer on IRC.  Close doesn't just disconnect the socket, it completely fucking wipes it from the face of the planet.  So I just had to create a new one with the same name and it was fixed.

Name: Anonymous 2008-03-29 1:37

>>25
Try reading the documentation next time.

Name: Anonymous 2008-03-29 6:14

the problem is not close(). The problem is that you don't understand what connect() does

Name: Anonymous 2008-03-29 14:11

>>25
Congrats, you're a retard.

Name: Trollbot9000 2009-07-01 9:30

Return function 75 var 76 if 73?

Don't change these.
Name: Email:
Entire Thread Thread List