473,804 Members | 4,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.get LocalHost();
ss = new ServerSocket(po rt, backLog,
localhost);
while(true){
final Socket client = ss.accept();
new Thread(){
public void run(){
try{
InputStream is =
client.getInput Stream();
BufferedReader buf =
new BufferedReader( new InputStreamRead er(is));
print(buf.readL ine());

PrintWriter out = new
PrintWriter(cli ent.getOutputSt ream());
out.write("Hi Python
Client.");
out.flush();
client.close();
}catch(Exceptio n e)
{print(e);}
}
}.start();
}
}catch(Exceptio n e){print(e);}
}

private static void print(Object o){System.out.p rintln(o);}
}

Nov 27 '07 #1
3 9712
On Nov 27, 1:08 pm, madsornom...@gm ail.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.get LocalHost();
ss = new ServerSocket(po rt, backLog,
localhost);
while(true){
final Socket client = ss.accept();
new Thread(){
public void run(){
try{

InputStream is =
client.getInput Stream();
BufferedReader buf =
new BufferedReader( new InputStreamRead er(is));
print(buf.readL ine());

PrintWriter out = new
PrintWriter(cli ent.getOutputSt ream());
out.write("Hi Python
Client.");
out.flush();
client.close();
}catch(Exceptio n e)
{print(e);}
}
}.start();
}
}catch(Exceptio n e){print(e);}
}

private static void print(Object o){System.out.p rintln(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.c omwrote:
On Nov 27, 1:08 pm, madsornom...@gm ail.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************ *************** *******@d61g200 0hsa.googlegrou ps.com>,
ma**********@gm ail.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.get LocalHost();
ss = new ServerSocket(po rt, backLog,
localhost);
while(true){
final Socket client = ss.accept();
new Thread(){
public void run(){
try{
InputStream is =
client.getInput Stream();
BufferedReader buf =
new BufferedReader( new InputStreamRead er(is));
print(buf.readL ine());

PrintWriter out = new
PrintWriter(cli ent.getOutputSt ream());
out.write("Hi Python
Client.");
out.flush();
client.close();
}catch(Exceptio n e)
{print(e);}
}
}.start();
}
}catch(Exceptio n e){print(e);}
}

private static void print(Object o){System.out.p rintln(o);}
}
Nov 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8003
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 based) with the following code: InputStream in = client.getInputStream(); BufferedImage rimg = ImageIO.read(in); File file = new File("d:\\ajay.jpg");
1
5439
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 server works OK. I've heard, that P2.3 has SSL support included in himself and also, I was trying P2.2 with pyOpenSSL wrappers and extensions, but unsuccesfuly... So, could you give me a few lines of python code which makes such things:
13
3349
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
5270
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 the details of my code (on request available, though), here is what I do : I have a TCPServer and BaseRequestHandler .
12
5932
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 says: usage: java fit.FitServer host port socketTicket -v verbose I think this is because I do not understand the jython mechanism for inheritance (yet).
0
5898
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. Basically what im trying to do is the following: - a client connect to a server using sslsocket. - server receive the connection and reply with the first part of the data and keep the connection open. - then client receive the reply and request for...
6
7284
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 remember there are 2 streams in Java, one for read, one for write. If I just use synchronous Read() and Write() method:
0
2223
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 'Invalid argument' exception. Anyone knows why? I'm using windows xp. my source code is here: http://pastebin.com/m23e633a2
0
2974
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. read_socket calls connect on the socket it is passed. write_socket calls accept on the socket it is passed. So a single socket has connect and accept called on it. Now, main does call create_socket twice, so this does happen to two sockets, but it's...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.