Hanging Communication between Sockets

Hiya folks, I've got a situation here that is stumping me. Now, I've found a way around it, but it seems 'hacky' to me, and I kindof would like to understand the issue instead of just dancing around it.
So, I've got a server class listening on port 4444. Talking to it is a client class. The client will send something to the server which will answer. The client then displays the reply on the System.out. Now.. if the server replies with a one line answer, all is well, the client displays the reply and exits. But if the server replies with, say, 3 seperate out.println("message") statements, the client will receive all three replies, then it will hang as if waiting for a fourth... My solution was to have the server send a fourth message that indicates to the client that it is done with transmission, ie: out.println("10-4") and when the client sees "10-4" as a reply, it breaks and moves on in life. I will include the relevant code snippets below so you can see how things are working... out is the socket's PrintWriter for both the server and client. What happens (see client code) is that if I don't send the "10-4" message, it won't make it to the System.out.println("after inner while loop"); line..
Server code:
while((inputLine = in.readLine()) != null) {
    out.println("Hello World!  Received "+ inputLine);
    out.println("Second Message.");
    out.println("Third/Last Message!");
    out.println("10-4");
}Client code:
while ((userInput = stdIn.readLine()) != null) {
   out.println(userInput);
   //ok, its something having to do with in.readLine not completing
   //or something.  it isn't finishing the evaluation so the
   //while condition never evaluates as true or false
   while((received = in.readLine()) != null) {
      System.out.println("received: "+received);          
         if(received.equals("10-4"))
          break;
         System.out.println("bottom of inner while");
   System.out.println("after inner while loop");
}

Your solution is fine, not a hack at all. You can only expect to get null from the readLine() call when the server closes the socket.

Similar Messages

  • Socket communication between client and server

    Hi all,
    I am doing an assignment for communication between java client and java server using sockets. This communication is in the form of XML documents. I am facing a problem in this communication.
    Actually at Server side I'm creating an XML document(Document type object) using DocumentBuilderFactory in javax.xml.parsers package and transforming this Document into a stream using StreamResult.
    My code is :
    Transformer xmlTransformer = TransformerFactory.newInstance().newTransformer();
    StreamResult xmlString = new StreamResult(currentClientHandler.getSocketOutputStream());
    DOMSource xmlDocSource = new DOMSource(xmlDocument); // xmlDocument is Document type reference
    xmlTransformer.transform(xmlDocSource, xmlString);
    so, this xmlString(i.e. StreamResult) is passed directly into the output stream. Here I need to close() output stream after transform() call to help SAX parser to know about end of stream.
    Now at Client side, I am parsing this stream using SAX parser. It parses this correctly. But when sending some another data back to Server when client opens output stream, it given Socket closed exception. I know that closing input or output stream closes socket. But in my problem, I have to send data in streams and not by using files or byte[] etc.
    So what is nearest solution to problem ??
    Plz help me. Any kind of help will be greatly appreciated.

    hi
    thanks for ur reply.
    I didnt get any error msg while getting the back the datas.
    Actually i divided my application into two parts.
    My application will act as both server and client.
    server ll get the browser request and send to the client and the client will send that data to the c++ server.
    Im able to do that.and unable to get the data from server.
    Didnt get any error.
    can u tell me how to make an application to act as both client and server.
    I think im wrong in that part.
    thanks a lot

  • Communication between applets using socket

    Hi,
    we have an application in which based on certain criteria a video is bieng played in the applet on same machine.Now we have to separate the video component from the application part so that video can be played separately. they will be communicated using socket.Can any body tell me how to go about for the solution.

    To do this you must have a server application listening to a port on a server box that has a fixed IP. In your Java app, you need to create a Socket connection back to the server (may need to sign your jar - but maybe not)
    voila you're there.
    You may want to search google + these forums for my InfoFetcher class which is built to grab data from an inputstream and report updates to listeners

  • Communication between different JVMs

    Hallo,
    is there a simple way, to exchange data between different java sessions? At the moment I store the needed data to a table and load it in my java procedures when ever I need them.
    But because this data is temporary, it wouldn't really be necessary to store the data. It's just for communication between the different JVMs. Is there an other way to do this, too?
    Thanks, Christian

    Christian, I may have misunderstood your question, but, until java version 1.4, the only way for different JVMs to communicate was via sockets, or secondary storage (the file system -- which includes databases).
    In java version 5.0 (previously version 1.5), there is a new feature known as class data sharing.
    But since you posted to the "Database >> JVM" forum, I can't help thinking that you are referring to different database JVMs. Is that correct? In any case, sockets and secondary storage are your only options there as well.
    Good Luck,
    Avi.

  • Communication between classes ???

    Hi
    I am facing problems in establishing communication between java classes.
    My application consists of 4 java classes in following sequence.
    Sockets main() of java UI called JavaUI calls execute class;s func Execute()
    Client ---------------------� server---------------------------------------- -� java UI appln ------------------------------------------------------------------� execute button.
    When execute button is clicked ANT tools are called and executed. I want some mechanism by which my execute class notifies my java UI application about the successful completion of execution, which in turn tells my server and then server should acknowledge client about the same.
    How can I achieve this back ward communication in java.
    Message was edited by:
    javaUser10

    i have written a swing application in which does program does the following.
    1. Server Application (can be deploy on the server)
    a. checks an engine if it is still working by checking the database every minute
    b. if not working sends a string of message to a dedicated port
    2. Client Application()
    a. retrieves messages from the dedicated port
    b. engine has been stopped, client application can start the engine
    maybe you can do the same for your application

  • Communication between client and server

    I am using sockets for communication between the client and the server. is there any other way that i can use for communication between the client and server???

    Plenty of ways: JMS, SOAP, RMI, a RESTful API, writing-files-to-a-shared-directory-on-the-disk, Sneakernet, ...
    Some of them use sockets (or better TCP/IP) as the underlying protocol.
    But to give you a good answer, you would have to tell us why you want a different way. I hope you're not searching for a different way just for the sake of being different.

  • Aynchronous and Synchronous communication TCP socket

    Hi All,
    I have written a client/server test application to test the performance of the java socket library. I tested the application in two scenarios,
    1) Asynchronous communication between client and server, client sends data(packet size is 500bytes) without waiting for the server response. I tried to upload 1mb file. The time took was 1.5mb/sec which is reasonable.
    2) Synchronous communication between client and server, client sends data (again, packet size is 500bytes) and waits for the server response (all server responds is OK (1 byte data)). The amazing thing in this test was the performance went down drastically!!! it came down to 5-10kb/sec, which is unbelievably nonsense or I am doing something nonsense.
    code snippet (server side)
    //reading from the socket (tried both buffered and non-buffered )
    inputstream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    //writing to the outputstream of the socket (server side)
    outstream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream))
    //then flush the data, in case
    client side code is very simple,
    while(not EOF)
    outputstream.write(buffer); //buffer size is 500bytes, as I mentioned above
    if(synchronous)
    wait for server response, otherwise don't
    I don't know why in case of synchronous communication it should bring down the speed so much, may be I am using wrong java classes or java sucks!
    I would greatly appreciate any help on this.
    Platform used: JDK 1.3.1, WIN2K.

    I have to break it up because that's how it's done in the real application. The test application that I have written is just part of the product that I am working on.
    To give you the background, we have C++ product where we are porting server side of the whole product to java.
    The client(which is written in c++) sends data(which is basically attributes), of size 500bytes in every request. The no. of request depends on how big the model(3D Model) is. In case of very huge models (will have MBs of attributes) the client takes long time to send all the attributes, which is the case with java server (client still in c++). But the in case c++ server it doesn't make any difference, the data transfer rate is same no matter how big the model is.
    I know if I increase the size of the packet I will get higher performance, but unfortunately I can't do that. That's how the client is designed to do it.
    thanks,
    Ganesh

  • Error 2032 in communication between Flex Client and WCF

    Hi All,
    I'm trying to establish communication between Flex Client
    and WCF service.
    WCF service accepts gZip compressed data and returns gZip
    compressed results.
    So I used Flex ByteArray.compress() and
    ByteArray.uncompress() for this purpose. However, it throws error
    2032.
    The gZip compression/decompression uses MemoryStream class in
    C#. Based on my previous experience, memory stream communication
    between Flex and C# gives erro 2032.
    Is there a work around for this?
    Thanks,
    Vishal

    I read some thread in the forum, and found somebody had the similar problem with me. Just want to know how to settle this problem.
    In the client/server program. Client is a JAVA program and Server a
    VC++ program. The connection works, and the problem appears after some time. The Client sends a lots of requests to Serverm, the server seems receive nothing. But at the same time, the server is able to send messages to Client. The Client also can get the messages and handle them. Don't understand why there this problem and why it appears when it wants.
    The client is a Win2k platorm with JDK1.3.1 and the server is also a Win2K platform with VC++ 6.0.
    In the Client, using:
    inputFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    outputToServer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
    Hope can get your help.

  • Jndi communication between app servers?

    Hi all,
    I'm quite familiar with J2EE and its communication architecture. In the past, I haven't had the need to communicate from one app server to another (just all web/applet clients to the central app server). But now I need to :)
    Is there a way to communicate (synchronously) between app servers on different machines that aren't clustered? Clustering is not a possibility.
    I know JMS is an option, but JMS is asyncrhonous. I also know you can't create server sockets in an app server because the J2EE spec disallows this approach, so forget about client/server communication via sockets.
    This would be my ideal solution:
    Server A does a jndi lookup for a bean on server B. Server A gets this reference and works with it just like any other J2EE client to server B.
    Does JNDI allow you to do lookups to other namespaces outside of the current namespace on server A?
    Can this even be done?
    Thanks,
    Les Hazlewood

    Yes. It's possible. For one of my applications, I have three HP-UX servers that run i2 software on them, but for different business units. I have a J2EE application that does a little bit of everything for the i2 applications. Since the business logic is the same, with only the business dept differing, I deployed 3 instances of JBoss, each running on their own server. My user's log into one of the applications, doesn't matter which, and if they request a service (EJB) that needs to run on a different server, my webapp makes a connection to that server and uses the remote EJB to do the work. It's very possible and an excellent solution.
    The solution will depend on which J2EE server you're using.
    Here's a class I use to retrieve the InitialContext I need for my application:
    package com.cibavision;
    import javax.naming.InitialContext;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import java.rmi.RemoteException;
    import java.util.Properties;
    public class CibaJNDI{
       public static InitialContext getInitialContext ( String server ) throws javax.naming.NamingException {
          Properties p = new Properties () ;
          p.put(Context.INITIAL_CONTEXT_FACTORY,
                 "org.jnp.interfaces.NamingContextFactory");
          p.put(Context.PROVIDER_URL,
                 "jnp://" + server + ":1099");
          p.put(Context.URL_PKG_PREFIXES,
                 "org.jboss.naming:org.jnp.interfaces");
          return new InitialContext (p) ;
    }Using this class, I have logic in my application that calls for the appropriate server, something like this:
    if businessA then use server1
    if businessB then use server2
    if businessC then use server3
    I then call for my InitialContext, request my EJB, and I'm off and running. No clustering, it matters which machine I'm on (I have to read extract files, etc.)
    Works great. We are also a weblogic shop, but I haven't made any calls to weblogic from outside my J2EE app yet, but it should only differ by the hash attributes you pass to new InitialContext( props ).
    Hope this helps,
    Perry Tew

  • Communicating between Air apps on local network?

    I am trying to build an AIR application that allows communication on a local network only.  Can I use the Local Connection to achieve this or is that only to communicate between apps on a single machine.  The functionality I am trying to achieve is simple check in/out app to let users know when someone is already working in a folder.   I already have the functionality built for this, I just need to be able to broadcast events to another AIR app on a local network.  Can anyone help or shot this down?  Thanks in advance.

    You are right: you can only use LocalConnection for communication between AIR applications (and SWF content in browsers) on a single machine.
    You may want to build a server application that the other AIR applications communicate with via a socket connection.

  • Is in PI7.1 possible asynchronous communication between SOAP and ABAPProxy?

    Hi,
    when method execute_asynchronous has disapeared since XI/PI 7.1, is
    there still way how to use ABAP proxy in asynchronous way?
    We need to build asynchronous connection SOAP->PI->ABAP_Proxy.
    In PI, both interfaces are defined as asynchronous (outbound for SOAP and
    inbound for ABAP Proxy).
    Despite of this fact, when message is sent, it is processed
    synchronous way.
    I have set breakpoint in my implementation of method for ABAP Proxy
    message processing. When message is sent and breakpoint is reached,
    whole connection stays open (between SOAP and PI and between PI and
    ABAP Proxy) and waits for processing method (the breakpointed one) to
    return. Only when processing method returns, is connection finelly
    closed.
    If i understand it correctly, this is synchronous behavior. In
    asynchronous behavior, as i understand it, should be connection
    between PI and ABAP Proxy of application server closed immediately
    after message has been delivered. This mean before my processing
    method is even called.
    The same could be said about SOAP and PI communication. Connection
    should be closed immediately after PI has received message. From
    definition of asynchronous communication of PI is obvious, that PI
    should receive message correctly and close connection to sender system
    even when receiver is unreachable. It should deliver message later
    when, receiver system is back on line. So why it keeps connection to
    sender system open while it waits for receiver?
    Why is this happening, when both interfaces are defined as
    asynchronous? Could be the reason for this, if APPLICATION
    ACKNOWLEDGEMENT is set on by default? If so, how can i change it
    to SYSTEM ACKNOWLEDGEMENT, or disable it at all?
    Or is this kind of asynchronous communication even possible since
    XI/PI 7.1 ?
    Processing of message we are sending can take some time, so we dont
    want connection pending open while waiting for finish of
    processing. Thats the reason why we have chose asynchronous model to
    use.

    Quote from How to Use the J2EE SOAP Adapter:
    "If you select Best Effort, the Web service client will receive a response
    message in the SOAP body. Otherwise, the Web service client will not receive a
    response message if no error occurs."
    "if no error occurs" - that is the problem. In either case he still
    waits if some error occure or not. I dont want it. Once PI has
    received message, I want the connection with sender to be closed. If
    there will be error in communication between PI and reciever, I want
    to see it only in PI log. That mean no notification to sender to be
    send about that error.
    Is that possible?

  • Communication between Best Buy and Apple

    Where is the communication between these two companies? It's frustrating to have NO CLUE if a phone will come in a day or in 5 weeks. Who at Apple is deciding what they end to what stores? Obviously, Best Buy has to pay for the phones. There has to be some sort of communication. 
    Would it really be that hard for someone high up in Best Buy to communicate with Apple? Maybe a list from each store of their orders and what they are waiting on. Give that to Apple. Apple can give some sort of answer. Exepect this much in this time frame... expect this much later. I mean, something. Instead of leaving everyone in the dark, completely. 
    Someone at Best Buy corporate has to have SOME clue how Apple is shipping and to what stores. To say no one has no clue at all, is nuts. And if it's true... Best Buy needs to get a clue. DO SOME WORK FOR YOUR CUSTOMERS. At least get some truth, so if we need to go elsewhere, we can. It would be better for both sides.

    Apparently since they come on UPS, they don't know when the shipments are coming in or how much. Which I think is bull.
    If that's the case, what I want to know is HOW do they know they received the amount they were expecting? If they don't know how much they're getting or when they're getting a shipment, then what's stopping the delivery man from stealing a box, and they wouldn't know any better? Maybe the higher up manager's know, and they aren't allowed to reveal that info, but there has to be a checks system in there somewhere verifying the stock received.

  • Communication between iViews

    How to realize communication between two iViews of the same window but not the same portal page? I tried with using firing portal events but this doesn’t seem to work with Mozilla Firefox. Do you have any other ideas?
    The sending iView is a portal application the receiving iView a Web Dynpro for ABAP application (so I cant use JavaScript, I guess).
    Thanks!
    René
    Edited by: Rene Guenther on Jan 10, 2008 3:29 PM

    Hi Mrkvicka,
    To enable communication between iviews you need to use the EPCF .
    You can raise an event in the component of the first iview and define an OnClick event.
    this event you can subscribe from the other iview
    Refer to this link to get a more clear picture
    DropDown Selection and EPCF
    also refer to
    EPCF
    hope this helps,
    Regards,
    Uma.

  • Communication between jsp and abstractportalcomponent

    Hello All
    Communication between jsp and abstractPortalComponent.
    jsp contains one input text field and one submit button.
    when the user clicks on submit button it will call the component and that input value will
    display in same jsp page.
    how this communication will happen?
    Rgrds
    Sri

    Hi Srikanth,
    In the JAVA File, 
    OnSubmit Event,
    String inputvalue ;
    InputField myInputField = (InputField) getComponentByName("Input_Field_ID");
    if (myInputField != null) {
                   inputvalue = myInputField.getValueAsDataType().toString();
    request.putValue("textvalue", inputvalue);
    request is IPORTALCOMPONENTREQUEST Object.
    In JSP File,   to retreive the value,
    <%
    String  textstring = (String) ComponentRequest.getValue("textvalue");
    %>
    In PORTALAPP.XML File,
    <component name="component name">
          <component-config>
            <property name="ClassName" value="classname"/>
            <property name="SafetyLevel" value="no_safety"/>
          </component-config>
          <component-profile>
            <property name="tagLib" value="/SERVICE/htmlb/taglib/htmlb.tld"/>
          </component-profile>
        </component>
    Using the code above, You can pass and read values between abstract portal component and Jsp Page.
    Instead of this, I suggest you to use JSPDYNPAGE Component for Data Exchange.
    Check the [Link|http://help.sap.com/saphelp_nw2004s/helpdata/de/ce/e0a341354ca309e10000000a155106/frameset.htm].
    Hope this helps you.
    Regards,
    Eben Joyson

  • Communication between : AP and WLAN controller

    Hi,
    The communication between AP and WLAN Controller is ( Data and Control ) UDP.
    Source port 1024 and destination port 12222 and 12223. Actually which device listen to which port or both should listen as control and data can be generated from both the devices.
    How does the user ( wireless client) traffic is switched - if user traffic is a TCP traffic. It will be sent to WLANC and then WLANC forwards it to respective VLAN or default gateway ( depending upon the destination in the packet ).
    Please explain / share the experience.
    any link on cisco.com
    Thanka in advance
    Subodh

    "the LWAPP Control and Data messages are encapsulated in UDP packets that are carried over the IP network. The only requirement is established IP connectivity between the access points and the WLC. The LWAPP tunnel uses the access point's IP address and the WLC's AP Manager interface IP address as endpoints. The AP Manager interface is explained in further detail in the
    implementation section. On the access point side, both LWAPP Control and Data messages use an ephemeral port that is derived from a hash of the access point MAC address as the UDP port. On the WLC side, LWAPP Data messages always use UDP port 12222. On the WLC side, LWAPP Control messages always use UDP port 12223.
    The mechanics and sequencing of Layer 3 LWAPP are similar to Layer 2 LWAPP except that the packets are carried in UDP packets instead of being encapsulated in Ethernet frames."
    Taken from "Cisco 440X Series Wireless LAN Controllers Deployment Guide"

Maybe you are looking for

  • Help me to send an email throught jsf....

    Hi guys, i've developed a jsf application using jsf 1.1.0.1 , servlet 2.3 and tomcat 5.5 embedded with eclipse. I've a question for you and i'm not able to solve it,excuse my inexperience. I've in my menu a password request commandlink,user select it

  • Header in flat file destination (ssis)

    Hi I am creating a header in the flat file destination which has some hard coded values and record count which will get its value from a variable assisgned to a row count transformation in the package. There is no problem in hard coded value but when

  • ITunes Behaving Strangely When I Sync

    Ok, I have some serious issues going on with iTunes, and I am hopeful that you all can help me diagnose the problem. The symptoms: 1. If I download an app on my iPhone or iPad which already exist in my iTunes library, iTunes will delete that app from

  • Java Mail header info displayed in email results

    I am having problems with the output generated by the send() method in javamail. The out contains unwanted email header info within the email. Please review code and email output. The Cc: thru Content-Transfer-Encoding should not display -Thanks....

  • Can I use Linux next to mountain lion on my MBP? if so, how to install...

    I have a MBP 2.9 GHz Intel Core i7 OSX 10.8.3 and like to add Linux to my machine. Is that possible, and if so, what is the best way to go about installing this? DJSMID