This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Hi everybody Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hi everybody" Watch "Hi everybody" New topic
Author

Hi everybody

dfdf dfsdf
Greenhorn

Joined: Oct 21, 2003
Posts: 1
I'm programming a simple chat programm. But I' m in trouble with the Output and Inputstream. The Sockets work as my Server recognizes new connections if new Users connect. But both are not able to transfer data between each other. I think it has something to do with the Thread and the input reader. Can you help me on that please? - here's the code:
//Here is the part where I'd like to start the Thread from the class which implements runnable
if(ae.getActionCommand().equals("-><-"))
{
System.out.println("jbConnect");
try
{
Socket s = new Socket(jtf.getText(),5000);
System.out.println("ServerEntry");
jta.setText("verbunden zu: "+jtf.getText());
TextClient tc = new TextClient(s, 0, jta, jtf, jtfinput);
Thread t = new Thread(tc);
t.start();

}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
______________________
package connections;
import java.net.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;

//I think the that the OutputStream works. I expect that the the problem has something to do with the InputStream

public class TextClient implements Runnable, KeyListener{

boolean bit;
Socket socket;
int counter;
JTextArea jta;
JTextField jtf;
BufferedWriter bw;
BufferedReader br;
JTextField jtfinput;

public TextClient(Socket socket, int counter, JTextArea jta, JTextField jtf, JTextField jtfinput)
{

bit = true;

this. socket = socket;
this.counter = counter;
this.jta = jta;
this.jtf = jtf;
this.jtfinput = jtfinput;
jtf.addKeyListener(this);
jtfinput.addKeyListener(this);

try
{

bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch(Exception ex)
{
System.out.println("Exception buffered");
System.out.println(ex.getMessage());
}
}

public void run()
{
String brline;
try
{
System.out.println(br.toString());

while(true)
{

if((brline=br.readLine())!=null)
{
System.out.println("noch in run");

System.out.println("noch in try von run");


System.out.println("kommt in read line");
jta.setText(brline);
}
}



}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}



}






public void keyTyped(KeyEvent e)
{


}


public void keyPressed(KeyEvent e)
{


}


public void keyReleased(KeyEvent e)
{
System.out.println("keyevent");
System.out.println(e.getID());

System.out.println(e.getKeyCode());
if(e.getKeyCode()==10)
{

System.out.println("kommt in enter");

try
{

bw.write(jtfinput.getText());
System.out.println(jtfinput.getText());
System.out.println(bw.toString());
bw.flush();
System.out.println(bw.toString());
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}



}
}
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
dfdf dfsdf
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however, your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
You might want to edit your post and put the code snippet in between the UBB code tags, it makes it much easier to read.


Dave
 
wood burning stoves
 
subject: Hi everybody
 
Similar Threads
FileName to write to a .txt File
Problem with networking IO stream
Passing data between JFrame & JDialog
How do you formally disconect a client from a server?
JTextArea and saving to a file