maateen

Greenhorn
+ Follow
since Dec 18, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by maateen

hello
Rasmeet Devji
its nice to listen that ur applet is running fine in IE
....
i m also developing the same chat software using URL
but my software doesn't need security policy....
i would be giving u proper solution if either send ur code or make fquestion clear to ...
thanX
would u plz send ur code
it may help me to figure out ur prob...
I want to establish a socket on client side through applet so i can write on this socket
i also have a server program running on remote machine...
but Browser is not letting this applet to make a socket at local port in client machine
thanx in advance..
thanx Angela Jessi
i downloaded the html converter & created the html file
also changed the properties of java plugin in control panel
but still no output...
24 years ago
i have used an object of serverconnection class
this is class which constructs a URL with that server....
i m sending the code for that class
it may help U..
//_________________________________
import java.io.*;
import java.net.*;
import java.util.*;
public class ServerConnection {
private static int uniqNum = 0;
private static final String SERVER_MSG_PREFIX = "SERVER";
private static final String CLIENT_MSG_PREFIX = "GETNEXTMSG";
private static final String ECHO = "ECHO";
private URL baseUrl;
public ServerConnection(String host, int port){
try {
System.out.println("stating to get connected");
baseUrl = new URL("http://" + host + ":" + port + "/" );
System.out.println(baseUrl);
} catch (MalformedURLException e) {
System.out.println ("Error forming baseURL");
}
}
public String getNextMessage(){
//Runtime.getRuntime().exec("");
String tempString=null;
try{
URL u=new URL(this.baseUrl,ServerConnection.encode(ServerConnection.CLIENT_MSG_PREFIX+ServerConnection.uniqNum++));
//System.out.println("GETNEXTMESSAGE"+u);
BufferedReader read=new BufferedReader(new InputStreamReader(u.openConnection().getInputStream()));
tempString = read.readLine();
read.close();
}
catch(java.io.IOException ioexp){
System.out.println(ioexp.toString());
}
return tempString;
}
public void broadcastMessage(String message){
String encodedMessage=ServerConnection.encode(ServerConnection.ECHO+message);
try{
URL u=new URL(baseUrl,encodedMessage);
u.openConnection().getInputStream().close();
System.out.println(u);
}
catch(java.net.MalformedURLException murlexp){
System.out.println(murlexp.toString());
}
catch(java.io.IOException ioexp){
System.out.println(ioexp.toString());
}
}
static String encode (String str) {
StringBuffer buf = new StringBuffer();
for (int i = 0, len = str.length() ; i < len ; i++) {<br /> char c = str.charAt(i);<br /> if (((c >= 'a') && (c <= 'z')) | | ((c >= 'A') && (c <= 'Z')) | |<br /> ((c >= '0') && (c <= '9'))) {<br /> buf.append(c);<br /> } else {<br /> buf.append('%');<br /> buf.append(Character.forDigit((c >> 4) & 0xF, 16));
buf.append(Character.forDigit((c >> 0) & 0xF, 16));
}
}
return buf.toString ();
}
}
Application Server is such program which provides services for applications for example
WEBLOGICS is application server also called container which provides a specific envirnment to Enterprose Java Beans
there are other many application servers...
but a web server is such which provieds responses to clients conneted through any specific protocol for example http or ftp
any how i hope this may help U
write if still there is any confusion...
u can read write any file from sockets
just using proper class from IO package...
cuz sockets
have inputstreams
& outputstreams
web server is required for URL.
this web server should have to provide a service on a perticuler
port
&
our client would get connected by typing a url in address
the formate would be
http:\\"<<service provider name>>":"port"\"<<client program>>"
here is some client program
//________________
import java.net.*;
import java.io.*;
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ClientApplet extends Applet implements Runnable{
private TextArea theTextArea=null;
private TextField theTextField=null;
private Thread thread=null;
private ServerConnection connection=null;
public void init(){
try{
//System.out.println(connection.getNextMessage());
theTextArea =new TextArea("Main Chat Room....",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY);
theTextField=new TextField();
theTextField.setSize(100,25);
theTextField.requestFocus();
theTextArea.setEditable(false);
this.setLayout(new BorderLayout(2,3));
this.add(theTextField,BorderLayout.SOUTH);
this.add(theTextArea,BorderLayout.NORTH);
this.setVisible(true);
connection=new ServerConnection("192.168.0.249",6006);
theTextField.addKeyListener(new KeyListener(){
public void keyPressed(java.awt.event.KeyEvent A) {
if (A.getKeyCode()==A.VK_ENTER) {
if (!theTextField.getText().equals("")) {
connection.broadcastMessage(theTextField.getText());
//theTextArea.append(connection.getNextMessage());
theTextField.setText("");
}//innet IF
}//outer IF
if (A.getKeyCode()==A.VK_0){
theTextArea.append(connection.getNextMessage());
}
}
public void keyReleased(java.awt.event.KeyEvent A) {
}
public void keyTyped(java.awt.event.KeyEvent A) {
}
});
}
catch(Exception exp){
}
}
public void start(){
//connection.getNextMessage();
// if(thread==null){
thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
// }
// else
// thread.start();
}
public void run() {/*
while ( thread==Thread.currentThread() ) {
String line = connection.getNextMessage();
if (!line.equals(null)) {
//line=line.substring(this.SERVER_MSG_PREFIX.length());
theTextArea.append(line+"\n");
//System.out.println(line);
}
}//END-WHILE
*/}
}
web server is required for URL.
this web server should have to provide a service on a perticuler
port
&
our client would get connected by typing a url in address
the formate would be
http:\\<<service provider name>> ort\<<client program>>
here is some client program
//________________
import java.net.*;
import java.io.*;
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ClientApplet extends Applet implements Runnable{
private TextArea theTextArea=null;
private TextField theTextField=null;
private Thread thread=null;
private ServerConnection connection=null;
public void init(){
try{
//System.out.println(connection.getNextMessage());
theTextArea =new TextArea("Main Chat Room....",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY);
theTextField=new TextField();
theTextField.setSize(100,25);
theTextField.requestFocus();
theTextArea.setEditable(false);
this.setLayout(new BorderLayout(2,3));
this.add(theTextField,BorderLayout.SOUTH);
this.add(theTextArea,BorderLayout.NORTH);
this.setVisible(true);
connection=new ServerConnection("192.168.0.249",6006);
theTextField.addKeyListener(new KeyListener(){
public void keyPressed(java.awt.event.KeyEvent A) {
if (A.getKeyCode()==A.VK_ENTER) {
if (!theTextField.getText().equals("")) {
connection.broadcastMessage(theTextField.getText());
//theTextArea.append(connection.getNextMessage());
theTextField.setText("");
}//innet IF
}//outer IF
if (A.getKeyCode()==A.VK_0){
theTextArea.append(connection.getNextMessage());
}
}
public void keyReleased(java.awt.event.KeyEvent A) {
}
public void keyTyped(java.awt.event.KeyEvent A) {
}
});
}
catch(Exception exp){
}
}
public void start(){
//connection.getNextMessage();
// if(thread==null){
thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
// }
// else
// thread.start();
}
public void run() {/*
while ( thread==Thread.currentThread() ) {
String line = connection.getNextMessage();
if (!line.equals(null)) {
//line=line.substring(this.SERVER_MSG_PREFIX.length());
theTextArea.append(line+"\n");
//System.out.println(line);
}
}//END-WHILE
*/}
}
hello,
U have to run ur server program first in one java console then run client program in new console
some points to remember...
ur server program should have to listen on a specific port in this program ur server should have to listen on port 80
but the more important thing is that ports above 1024 are for for system & are allocated automatically by Operating system.
so try to use port above 1024
here is some code which may help U
Sever Code
U may run it by changing the IP & port of ur desired machine
//________________________
import java.util.*;
import java.io.*;
import java.net.*;
public class SimpleChatServer extends Thread {
private static final String SERVER_MSG_PREFIX = "SERVER";
private static final String CLIENT_MSG_PREFIX = "GETNEXTMSG";
private static final String ECHO = "ECHO";
protected java.util.Vector connections=null;
private String str=null;
public static void main(String argv[]) {
new SimpleChatServer();
}
public SimpleChatServer(){
int port = 6006;
ServerSocket ss = null;
try {
ss = new ServerSocket(port);
System.out.println("Server Socket Created Waiting for Clients...");
} catch (IOException e) {
System.out.println("Could not listen on port: " + port + ", " + e);
System.exit(1);
}
connections = new java.util.Vector();
System.out.println("Ready...");
while (true) {
Socket s = null;
try {
s = ss.accept();
System.out.println("Socket Created\n"+s);
}
catch (IOException e) {
System.out.println("Accept failed: " + port + ", " + e);
System.exit(1);
}
try {
BufferedReader read=new BufferedReader(new InputStreamReader(s.getInputStream()));
str = read.readLine();
System.out.println(str);
PrintWriter print=new PrintWriter(s.getOutputStream(),true);
print.println("Java Chat Service at port: 6006");
str = str.substring("GET /".length(),str.length()-" HTTP/1.1".length());
System.out.println(str);
if(str.startsWith(SimpleChatServer.CLIENT_MSG_PREFIX)){
synchronized(connections){
connections.addElement(new HttpConnection(s,s.getOutputStream(),s.getInputStream()));
}
System.out.println(connections);
continue;
}
if(str.startsWith(SimpleChatServer.ECHO)){
str = str.substring(SimpleChatServer.ECHO.length());
read.close();
print.close();
s.close();
for(int i = 0;i<connections.size();i++){>
HttpConnection connection = (HttpConnection)connections.elementAt(i);
synchronized(connections){
synchronized(connection){
connection.writeToConnection(str);
connection.writeClose();
connection.socketClose();
}
}
}
connections.removeAllElements();
System.out.println(connections);
continue;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}

U just add new object in array then it automatically update the table & draw it itself....
but still if there is any ambiguity then check out the following code it may help U....
//___________________________
//package swingchat;
import java.util.Vector;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Color;
import javax.swing.JCheckBox;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.table.JTableHeader;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
public class NoticeDisplay extends JDialog{
private javax.swing.JTable DBTable;
private javax.swing.JScrollPane tablePanel;
private JTableHeader tableHeader;
private JLabel label1;
private boolean diaplayableRecords=true;
private Vector columNames=null;
private Vector rowVector(final Vector data){
Vector tableVector=new Vector();
int j=0;
for(int i=0 ;i<data.size();j++){
Vector rowVector=new Vector();
rowVector.add(data.elementAt(j));
rowVector.add(data.elementAt(++j));
tableVector.add(rowVector);
i=i+2;
}
return tableVector;
}
public NoticeDisplay(final Vector messageList){
super.setTitle("Saved Messages Form Data Base.");
this.setSize(550,500);
this.setLocation(100,20);
this.setResizable(false);
columNames=new Vector();
columNames.add("Serial #");
columNames.add("Messages");
label1=new JLabel();
label1.setBorder(BorderFactory.createEtchedBorder());
label1.setBackground(Color.darkGray);
DBTable=new JTable(this.rowVector(messageList),columNames);
DBTable.setBorder(BorderFactory.createEtchedBorder());
tablePanel=new JScrollPane(DBTable);
tablePanel.setBorder(BorderFactory.createEtchedBorder());
tableHeader=DBTable.getTableHeader();
tableHeader.resizeAndRepaint();
tableHeader.setBorder(BorderFactory.createCompoundBorder());
this.getContentPane().add(tablePanel,BorderLayout.SOUTH);
this.getContentPane().add(label1,BorderLayout.NORTH);
this.getContentPane().setVisible(true);
this.setVisible(true);
}
}
24 years ago
even after installing html convertor
my JApplet is not running in browser.
it throws ClassNotFoundException in java console.
with regards
24 years ago
can some one help me out to make an applet signed
& grand it some permissions like
to get URLConnection
to make a socket at local port at client side
thanks with regards..
24 years ago
can some one help me out to make an applet signed
& grand it some permissions like
to get URLConnection
to make a socket at local port at client side
thanks with regards..
24 years ago
there is a security file for java runtime envirnment placed in
C:\Program Files\JavaSoft\JRE\1.3\lib\security\java.security
this file defines all the rights for applets....
24 years ago