#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3

    Stream Sockets using Makefile


    Hello,

    I am attempting to write a simple Hello World client server program using Stream Sockets. I have no problem writing it using send and recv. However, I want to get it to work using makefile. I've done some research online and haven't turned up any examples that I understand. I was hoping someone here would be able to help me out.

    Code:
    Server
    import socket;
    
    host = 'localhost';
    port = 15003;
    address = (host,port);
    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
    serverSocket.bind(address);
    serverSocket.listen(1);
    
    while 1:
        clientSocket,clientAddress = serverSocket.accept();
        clientFile = clientSocket.makefile();
        data = 'empty';
        while data != '':
            data = clientFile.read();
            print data;
        clientFile.write('Goodbye');
        clientFile.flush();
        clientFile.close();
        clientSocket.close();
    Code:
    Client
    import socket;
    
    host = 'localhost';
    port = 15003;
    address = (host,port);
    clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
    clientSocket.connect(address);
    
    clientFile = clientSocket.makefile();
    clientFile.write('Hello');
    clientFile.flush();
    clientFile.close();
    data = 'blank';
    clientFile = clientSocket.makefile();
    while data != '':
        data = clientFile.read();
        print data;
    
    clientFile.flush();
    clientFile.close();
    clientSocket.close();
    Cheers, Cam.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    To me, makefile means to use the program make which updates targets from newer dependencies. What do you mean?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Originally Posted by b49P23TIvg
    To me, makefile means to use the program make which updates targets from newer dependencies. What do you mean?
    I was told you can use socketVar.makefile() to treat a socket like a file. So instead of using recv and send you can use write and read methods that you would normally use for files. Supposedly this is a better way to do it since when the file is closed it will be treated as an EOF. So you can read until EOF is reached.

    There is an example of it here, but I don't see any differences from what I am doing.
    http://stackoverflow.com/questions/6946629/can-i-get-a-socket-makefile-to-have-the-same-read-semantics-as-a-regular-file

    Comments on this post

    • b49P23TIvg agrees : Clearly, I'm spinning wheels in a different realm.
    Last edited by Cameron0960; July 14th, 2013 at 05:15 PM.

IMN logo majestic logo threadwatch logo seochat tools logo