0

i have found this amazing tutorial on raywenderlich.com:

http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server

it create a Twisted Python server, and talk with app iphone chat, i would let the server talk also with a Java chat, and now i write some code, this is the python server:

from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor


class IphoneChat(Protocol):
def connectionMade(self):
    #self.transport.write("""connected""")
    self.factory.clients.append(self)
    print "clients are ", self.factory.clients

def connectionLost(self, reason):
    self.factory.clients.remove(self)

def dataReceived(self, data):
    #print "data is ", data
    a = data.split(':')
    if len(a) > 1:
        command = a[0]
        content = a[1]

        msg = ""
        if command == "iam":
            self.name = content
            msg = self.name + " has joined"

        elif command == "msg":
            msg = self.name + ": " + content

        print msg

        for c in self.factory.clients:
            c.message(msg)

def message(self, message):
    self.transport.write(message + '\n')


factory = Factory()
factory.protocol = IphoneChat
factory.clients = []

reactor.listenTCP(1025, factory)
print "Chat server started"
reactor.run()

then i create a simple java example to connect to the server:

import java.net.*;
import java.io.*;
import java.util.*;

public class MyClassSocket  {

private ObjectInputStream sInput;       // to read from the socket
private ObjectOutputStream sOutput;     // to write on the socket
private Socket socket;

private String server, username;
private int port;

MyClassSocket(String server, int port, String username) {
    this.server = server;
    this.port = port;
    this.username = username;
}

private void display(String msg) {

    System.out.println(msg);      // println in console mode

}

public boolean start() {

    try {
        socket = new Socket(server, port);
    }
    // if it failed not much I can so
    catch(Exception ec) {
        display("Error connectiong to server:" + ec);
        return false;
    }

    String msg = "Connection accepted " + socket.getInetAddress() + ":" + socket.getPort();
    display(msg);

    try
    {

        sInput  = new ObjectInputStream(socket.getInputStream()); //<-------- block here
        sOutput = new ObjectOutputStream(socket.getOutputStream());
    }
    catch (IOException eIO) {
        display("Exception creating new Input/output Streams: " + eIO);
        return false;
    }

    return true;

}

public static void main(String[] args) {

    MyClassSocket client = new MyClassSocket("localhost",1025,"PieroJava");
    client.start();
}

}

The code freeze in the point i comment in the code above, and i can see in the server terminal that there is a new connection, and i can see in the client terminal that i connect but don't go over here:

sInput  = new ObjectInputStream(socket.getInputStream());

how i can do?

Piero
  • 9,173
  • 18
  • 90
  • 160
  • 1
    How do you know that's the line the client blocks on? – Jean-Paul Calderone Nov 07 '12 at 13:36
  • i have put some System.out.println first and after that line, and print only the first... – Piero Nov 07 '12 at 13:37
  • 1
    What about the exception case? Are you sure exceptions will get reported? From http://docs.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html#getInputStream() it doesn't sound like getInputStream will ever block for any reason, so it's strange that you observe it to be blocking. – Jean-Paul Calderone Nov 07 '12 at 13:38
  • the problem is that i get no exceptions, remain stuck there and don't go over, i have found this similar question: http://stackoverflow.com/questions/8377291/objectinputstreamsocket-getinputstream-does-not-work that seem my same problem, and in the Comment answer, the user talk that have found his problem, but he have a java server... – Piero Nov 07 '12 at 13:41
  • 1
    Ah. That question talks about the ObjectInputStream constructor reading from the input stream. But your server does not write anything to the connection on setup. So there's nothing for the client to read, so the read blocks forever. – Jean-Paul Calderone Nov 07 '12 at 14:28
  • I think you might want to update the topic of your question; "object-C" (by which I believe you mean "Objective C") does not seem to be involved in this question; just Python and Java. – Glyph Nov 07 '12 at 17:41

1 Answers1

2

I suggest not using ObjectInputStream and ObjectOutputStream. It will probably be challenging to get these to interact nicely with a Python server anyway, and they don't implement a very good protocol anyway. I recommend a simpler protocol with better cross language support such as AMP.

In any case, new ObjectInputStream seems to be expected to block until it can read bytes from the connection. If you want to make it succeed, your Twisted-based server will need to write the beginning of a Java object stream to the connection.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122