wood burning stoves 2.0*
The moose likes Applets and the fly likes Amit Agarwal jee!! please see this. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Amit Agarwal jee!! please see this." Watch "Amit Agarwal jee!! please see this." New topic
Author

Amit Agarwal jee!! please see this.

nima lama
Greenhorn

Joined: Jun 18, 2001
Posts: 19
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

Joined: Jan 16, 2001
Posts: 421
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

Joined: May 09, 2001
Posts: 92
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Amit Agarwal jee!! please see this.
 
Similar Threads
simple chat Applet, please see the code inside.
4th time i am asking.Please see the code.
chat programe, kindly see the code and help me.
Help wanted Urgently
simple chat Applet. kindly see the code.