473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 9685
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: IndianOZ | last post by:
Hi , I'm trying to send an Image over the Socket Connection (from the Client using winsock, which is a VB based client) and at the other end I'm reading the Stream (at the Server which is java...
1
by: Krzysztof Pa¼ | last post by:
Hi, I want to make simple client in phyton, which would be able to communicate with Java server using SSL sockets. There is the Java clients, which is doing this - so I'm pretty sure, that Java...
13
by: Ajay | last post by:
hi! can you call a Python application from a Java program? does this require any additional package to be installed? thanks cheers
4
by: google | last post by:
Dear newsgroup, I give up, I must be overseeing something terribly trivial, but I can't get a simple (Java) applet to react to incoming (python) SocketServer messages. Without boring you with...
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
0
by: kuguy | last post by:
Hi all, I'm new to the forums, so I hope this isn't in the wrong place... I have that "Software caused connection abort: socket write error" exception error that i've never meet before. ...
6
by: Ryan Liu | last post by:
Hi, I have some basic question about NetworkStream, can someone explain to me? Thanks a lot in advance! TcpClient has a GetStream() method return a NetworkStream for read and write. I...
0
by: Leo Jay | last post by:
I'd like to read and write the same socket in different threads. one thread is only used to read from the socket, and the other is only used to write to the socket. But I always get a 10022...
0
by: Jean-Paul Calderone | last post by:
On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <python.leojay@gmail.comwrote: No - it's just what I said. create_socket creates one socket and passes it to read_socket and write_socket. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.