read/write to java socket in python

James Matthews nytrokiss at gmail.com
Tue Nov 27 13:40:38 EST 2007


It's a socket interface it should be universal!

On Nov 27, 2007 4:08 PM, <madsornomads at gmail.com> wrote:

> Hi all,
>
> I have a problem with reading from a Java server after I have written
> to it - it just hangs. It works fine if I just write to the server and
> not try to write. I have read the HOWTO on sockets - and it states
> that there is a problem (something about flushing), but not what the
> solutions is. Nor do google. Can somebody please help?
>
> A few lines down you can see the example code that sums up the
> problem. Just change the name of the Python HOST-variable.
>
> Thanks
> Mads
>
>
> This is the client in Python:
> #! /usr/bin/env python
>
> import sys
> from socket import *
>
> PORT = 3122
> HOST = 'app-5'
> SUCCESS = 'Success'
> FAILURE = 'Failure'
>
> s = socket(AF_INET, SOCK_STREAM)
> s.connect((HOST, PORT))
> s.send("Hi Java Server");
> print "Have written, waiting to recieve.."
> print s.recv(1014)
> s.close()
>
> And this the server in Java:
> import java.io.*;
> import java.net.*;
>
> public class Server{
>        public static void main(String args[]){
>
>                int port = 3122;
>                int backLog = 50;
>
>                ServerSocket ss = null;
>                try{
>
>                        InetAddress localhost =
> InetAddress.getLocalHost();
>                        ss = new ServerSocket(port, backLog,
> localhost);
>                        while(true){
>                                final Socket client = ss.accept();
>                                new Thread(){
>                                        public void run(){
>                                                try{
>
>
>                                                InputStream is =
> client.getInputStream();
>                                                BufferedReader buf =
> new BufferedReader(new InputStreamReader(is));
>                                                print(buf.readLine());
>
>                                                PrintWriter out = new
> PrintWriter(client.getOutputStream());
>                                                out.write("Hi Python
> Client.");
>                                                out.flush();
>                                                client.close();
>                                                }catch(Exception e)
> {print(e);}
>                                        }
>                                }.start();
>                        }
>                }catch(Exception e){print(e);}
>        }
>
>        private static void print(Object o){System.out.println(o);}
> }
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://search.goldwatches.com/?Search=Movado+Watches
http://www.goldwatches.com/coupons
http://www.jewelerslounge.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071127/240f8014/attachment.html>


More information about the Python-list mailing list