• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Java Sockets, Java Network Connections, Java Swing, Head First Java

 
Greenhorn
Posts: 5
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, first post here.

I have an issue wherein I cannot pass information from a server socket to a JTextArea - and cannot for the life of me figure out why.

The program sends user input to the server, the server receives (indicated via print for testing purposes), and then... nothing.

Sorry regarding the verbosity, but it's a GUI program and I think it helps elucidate the problem. See [1] for full code.



[1] https://github.com/nreid2946/BeatBox_WithClient
 
Marshal
Posts: 74042
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please use more specific thread subjects.

BeatBox? Is that out of Head First Java (Sierra and Bates)? Please start by confirming that you are reading from the socket by printing all the messages to System.out. Preferably without a GUI.
 
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you starting up IncomingReader ?
 
Nicholas Reid
Greenhorn
Posts: 5
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! Apologies, I will in the future.

Yes, that's right. I have slightly forked it. My goal is to take in the JTextField input, send that to the server, and then have the server put the retrieved text as a message in the JTextArea called 'incoming'. The issue is with the text not appearing within JTextArea, as the IncomingReader seems to not pass the try-catch statement.

I have several Sout statements both in the posted code, and the server. Which confirms the message is being sent to the server, but does not print 'read: 'message''. this would indicate there's something wrong with the IncomingReader code.
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicholas Reid wrote:Hello! Apologies, I will in the future.

You can go back and edit in now. That would be helpful.
 
Nicholas Reid
Greenhorn
Posts: 5
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IncomingReader is called before the Swing code, please see [1]. It's called after setUpNetworking(), before JScrollPane etc.

I couldn't post the entire code here, and I'm not super sure how to get it working without the GUI.
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
readLine() is expecting a "\n", are you sure the server is sending that? Is server flushing the output buffer?

The incoming.append() call is on a different thread than the EDT, you'll need an invokeLater().

I would put more println()'s in IncomingReader to let me know if readLine() returned null, for instance.
 
Rancher
Posts: 162
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'm not aware of "the original", but there several things that may throw you of in several spots.
It would properly took me quite a bit to write it all down before this thread moves along - so I try to keep it short for now:

1) GUI synchronization: one of basics of GUI development is: ANY gui related stuff HAS TO be done within the EDT - and anything else has to be de-coupled from it.
This is the simples code >I< can come up with - but there'Re better ways to do this - someone will help you:


2) a-sync processing of incomming data from remote: One can write synchronous protocols, like HTTP - or go the a-sync way: full duplex like the bits on the wire. It's pretty much the same but without the listener around it:

Note: This time I used invokeAndWait() - which waits until the gui update has completed before returning to the read loop. Shouldn't be an issue with the default buffer sizes.

As to write a complete example: There'Re several ways - but I would go for a MVC approach: First get the chat running as console based code - than add the GUI via controllers hooking into where the console code runs.
 
Nicholas Reid
Greenhorn
Posts: 5
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:readLine() is expecting a "\n", are you sure the server is sending that? Is server flushing the output buffer?

The incoming.append() call is on a different thread than the EDT, you'll need an invokeLater().

I would put more println()'s in IncomingReader to let me know if readLine() returned null, for instance.



Is readLine() expecting a "\n"? The functionality expressed there is meant to print the message to the JTextArea 'incoming', then make a new line, for another message.

The server seems to work fine, see here:



I've put print statements, and it seems to get the message. And writer.println(message) should send it to IncomingReader, no? That having been said, I really can't get it to run the try-catch statement. I don't know if there's something wrong with my conditions?

Regardless, I have tried giving the server input, and retrieving output via a program without the GUI, and there still seems to be an issue with retrieval of the message.
 
Nicholas Reid
Greenhorn
Posts: 5
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:readLine() is expecting a "\n", are you sure the server is sending that? Is server flushing the output buffer?

The incoming.append() call is on a different thread than the EDT, you'll need an invokeLater().

I would put more println()'s in IncomingReader to let me know if readLine() returned null, for instance.



Is readLine() expecting a "\n"? The functionality expressed there is meant to print the message to the JTextArea 'incoming', then make a new line, for another message.

The server seems to work fine, see here:



I've put print statements, and it seems to get the message. And writer.println(message) should send it to IncomingReader, no? That having been said, it does actually run the IncomingReader try-catch block but seems to have a problem actually appending the JTextArea. Is this because of me using a separate thread?

Regardless, I have tried giving the server input, and retrieving output via a program without the GUI, and the same problem occurs.
 
reply
    Bookmark Topic Watch Topic
  • New Topic