-
getting input from a socket
Hi there,
I have written a little IM client. Nothing really exciting however I do have a problem. If I run the following code from main there is no problem.
while ((inputRecieved = in.readLine()) != null)
{
writeToChatArea();
System.out.println("echo: " + in.readLine());
}
however... I need to have that little bit of code called elsewhere inside another class. The problem is that if I do that the program hangs. and I can't click the send button. I am confused. There is no problem in main... That code is called and then everything is fine however if I call it from the ConnectToServer class it screws everything up.
Maybe there is something I don't know? I have looked at ORielly's book learning java and haven't found anything that might help.
Just pointing me in the right direction would be great.
Thanks alot
Ken
-
Use Threads. I'm in the middle of writing an AIM client myself, trying to get webcam support at this point in development. Make a class called IMConnection, or something, and implement the Runnable interface.
Code:
public class IMConnection implements Runnable
{
private boolean isConnected = false;
private Thread chatConnection = null;
public void run()
{
String inputRecieved;
//keep thread alive while connected to server
while(isConnected)
{
inputRecieved = in.readLine();
if (inputRecieved != null)
{
writeToChatArea();
System.out.println("echo: " + in.readLine());
}
}
}
//connects you with your IM server
public void connect()
{
//do stuff to connect to chat server
isConnected = true;
chatConnection = new Thread(this);
chatConnection.start();
}
}
Basically you would just make a new IMConnection object then call connect(). If connection to chat server was successful, start a new thread which listens to the input stream.
If your application is to be much larger and the connection information has to be a seperate class, make a listener to send events to your other classes as needed.
-
sweet! That did it...
I didn't understand that a thread had access to the variables from where it was created. What I need to do is re-read that chapter and I will, however once you pointed that out (via that fact that you where using that sort of code) I understood. I of course modified the class but it works spiffy!
Thank you very much.
Below is that class you gave me and I modified. OH! You notice I added the actionListener so we could click it!
Ken
class IMConnection implements Runnable, ActionListener
{
private boolean isConnected = false;
private Thread chatConnection = null;
//GetNetInput getNetInput = new GetNetInput();
Socket serverSocketConnection = null;
String serverName = "localhost";
JTextArea chatArea = null;
JTextArea inputArea = null;
BufferedReader in = null;
PrintWriter pOut = null;
OutputStream out = null;
SendTextButtonHandler sendTextButtonHandler = null;
public void actionPerformed( ActionEvent e )
{
this.connect();
System.out.println("the connect button was pressed...");
}
public void run()
{
String inputRecieved = null;
//keep thread alive while connected to server
while(isConnected)
{ try {
inputRecieved = in.readLine();
System.out.println(inputRecieved);
chatArea.append(inputRecieved);
chatArea.append("\n");
} catch( IOException ioe) {
System.out.println(ioe);
}
}
}
//connects you with your IM server
public void connect()
{
//do stuff to connect to chat server
System.out.println("The connect function was called...");
try {
serverSocketConnection = new Socket(serverName, 33442);
} catch(IOException e) {
System.out.println("could not connect");
}
//After we connect create the buffers...
try {
in = new BufferedReader(new InputStreamReader(serverSocketConnection.getInputStream()));
out = serverSocketConnection.getOutputStream();
pOut = new PrintWriter(new OutputStreamWriter(out), true);
} catch( IOException ioe) {
System.out.println(ioe);
}
isConnected = true;
chatConnection = new Thread(this);
chatConnection.start();
// Pass the sendTextButtonHandler a refrence to the printWriter and the
// inputArea.. We could clean this up but for now it works!
sendTextButtonHandler.pOut = pOut;
sendTextButtonHandler.inputArea = inputArea;
sendTextButtonHandler.chatArea = chatArea;
}
}
Similar Threads
-
By AM003295 in forum VB Classic
Replies: 4
Last Post: 06-16-2005, 12:47 AM
-
Replies: 5
Last Post: 06-04-2004, 01:08 PM
-
Replies: 2
Last Post: 09-28-2002, 07:02 AM
-
By Aliasgar in forum Java
Replies: 0
Last Post: 04-17-2001, 07:25 AM
-
By Ruchi Dhar in forum Java
Replies: 2
Last Post: 10-24-2000, 02:00 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks