471,326 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

read/write to java socket in python

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);}
}

Nov 27 '07 #1
3 9493
On Nov 27, 1:08 pm, madsornom...@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);}

}
I don't know, but it's amazing to compare the python client with the
java server.
Nov 27 '07 #2
On Nov 27, 4:29 pm, hdante <hda...@gmail.comwrote:
On Nov 27, 1:08 pm, madsornom...@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 *

[Snip - a little Python]
And this the server in Java:
import java.io.*;
import java.net.*;

[Snip - a lot of Java]
}

I don't know, but it's amazing to compare the python client with the
java server.

Yes, Python is really handy :)

/Mads
Nov 27 '07 #3
Your server program is using readLine(), which will block until a
newline is received. The server code does not write a newline, so it is
waiting at recv() for data from the server, and the server is still
waiting for a newline. If you change the client to do the following, it
should work:

s.send("Hi Java Server\n");
Dave
In article
<d1**********************************@d61g2000hsa. googlegroups.com>,
ma**********@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);}
}
Nov 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by IndianOZ | last post: by
13 posts views Thread by Ajay | last post: by
4 posts views Thread by google | last post: by
12 posts views Thread by Mark Fink | last post: by
reply views Thread by Jean-Paul Calderone | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.