Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Amit Agarwal jee!! please see this.

 
nima lama
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Amit jee,
i tried to make some changes in the chat program(into frame). but it show Strange result.this program it's the same as the last time i send you through e-mail along with six other related program.
So, please try to run this program and see the result.
and make changes please.
*********************
RunTCPClient program
*********************
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class RunTCPClient extends Frame implements ChatClient,ActionListener
{
TextArea txta;
TextField txt;
Button b;

Socket clientSocket;
DataInputStream chatInputStream;
DataOutputStream chatOutputStream;
StringReader userInputStream;
public RunTCPClient()
{
super();
setLayout(new FlowLayout());
txta=new TextArea(20,20);
txt=new TextField(40);
b=new Button("send");
b.addActionListener(this);
add(txta);
add(txt);
add(b);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{


int port=8080;

String portStr=System.getProperty("port");
if(portStr!=null)
{
try
{
port=Integer.parseInt(portStr);
}
catch(Exception ignore)
{}
}
String hostName=System.getProperty("host");
if(hostName==null)hostName="localhost";

try
{
clientSocket=new Socket(hostName,port);
chatOutputStream=new DataOutputStream(clientSocket.getOutputStream());

chatInputStream=new DataInputStream(clientSocket.getInputStream());

//userInputStream=new DataInputStream(System.in());
//i change DataInputStream into StringReader since StringReader can accept String from the TextField.
userInputStream=new StringReader(txt.getText());
System.out.println("connected to the chat server");

//System.out.println("what name do you like to keep");
//System.out.flush();
String myName=txt.getText();

//out.println(txt.getText());
//out.flush();
chatOutputStream.writeUTF(myName);
txta.setText(myName);


RunTCPClient thisClient=new RunTCPClient();

TCPChatReader reader=new TCPChatReader(thisClient,chatInputStream);

reader.start();

while(true)
{
String chatLine=txt.getText();
sendChat(chatOutputStream,chatLine);

}
}
catch(Exception ex)
{
System.out.println("got exception");
ex.printStackTrace();
System.exit(1);
}
}
}
public void userHasEntered(String who)
{
txta.setText("---"+who+" has entered--");
}
public void userHasLeft(String who)
{
txta.setText("---"+who+" has left---");
}
public void incomingChat(String who,String chat)
{
txta.setText("<"+who+">"+chat);
}
public void disconnect()
{
System.out.println("chat server connection closed");
System.exit(0);
}

public static void sendChat(DataOutputStream outStream,String line)throws IOException
{
outStream.writeInt(TCPChatMessageTypes.CHAT);
outStream.writeInt(line.length());
outStream.writeBytes(line);
}
public static void main(String args[])
{
RunTCPClient f=new RunTCPClient();
f.setSize(400,400);
f.show();
}
}

 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make your code more readable by
<font size=+0>[CODE]
and &lbrack should be [
and naturally the closing tags.
Example done:
<font size=+0></font>
Nearly, don't know, why the font shows up but does not work

[This message has been edited by Peter Gragert (edited June 28, 2001).]
 
Amit Agarwal
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi nima,
one problem that might be soved is in the display of the messages. use TextArea.append() instead of setText().
other than that please give some concrete / specific info of the "strange results".
Amit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic