Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Amit Agarwal jee!! please see this.

 
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();
}
}

 
pioneer
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).]
 
pioneer
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
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!