Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hi everybody

 
dfdf dfsdf
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic