Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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
[923 byte] By [r0k3t] at [2007-11-11 7:36:48]
# 1 Re: getting input from a socket
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.

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.
Phaelax at 2007-11-11 22:37:51 >
# 2 Re: getting input from a socket
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;
}
}
r0k3t at 2007-11-11 22:38:57 >