Solved

Client - server chat room

Posted on 2000-05-13
9
798 Views
Last Modified: 2011-10-03
okay... this is the problem... I have coded 1 simple application for the server, and 1 simple applet for the client...

the code for the server:

import java.net.*;
import java.io.*;
import java.lang.*;

public class KadomonServer
{
      private static final int portNum = 1234;
      private DatagramSocket kadomonSocket;
      private String msg = new String();
            
      public KadomonServer()
      {
            try {
                  kadomonSocket = new DatagramSocket(portNum);
                  System.out.println("Kadomon server is started...");
            }
            catch (SocketException e) {
                  System.err.println("Couldn't create Datagram socket");
                  System.exit(1);
            }
      }
      
      public void start()
      {
            if (kadomonSocket == null) return;
            
            while (true) {
                  try {
                        InetAddress address;
                        int port;
                        DatagramPacket packet;
                        byte[] data = new byte[256];
                                                      
                        packet = new DatagramPacket(data, data.length);
                        kadomonSocket.receive(packet);
                        System.out.println("Data Received...");
                        
                        msg = new String(packet.getData(), 0);
                        address = packet.getAddress();
                        port = packet.getPort();
                        
                        msg.getBytes(0, msg.length(), data, 0);
                        packet = new DatagramPacket(data, data.length, address, port);
                        kadomonSocket.send(packet);
                        System.out.println("Data Sent...");
                  }
                  catch (SocketException e) {
                        System.err.println("Socket error : " + e);
                        e.printStackTrace();
                  }
                  catch (Exception e) {
                        System.err.println("Error: " + e);
                        System.exit(1);
                  }
            }
      }
      
      public static void main(String args[])
      {
            KadomonServer server = new KadomonServer();
            server.start();
      }
}


the code for the client:


import java.awt.*;
import java.net.*;
import java.applet.*;
import java.awt.Event;
import java.util.*;

public class KadomonChatApplet extends Applet
{
      private static final int PortNum = 1234;
      private String msg, name;
      private boolean isExit = false;
      private TextArea chatArea = new TextArea(15,60);
      private TextField msgField = new TextField(50);
      char[] pressed = new char[200];
      int wordCount = 0;
      String message;
      
      public void init()
      {            
            Button button = new Button("Submit");
            chatArea.setText("K.A.D.O.M.O.N   C.H.A.T\n\n");
            chatArea.setEditable(false);
            chatArea.setBackground(new Color(0xffffff));
            setLayout(new GridLayout(2,1,10,10));
            Panel top = new Panel();
            Panel bottom = new Panel();
            add(top);
            add(bottom);
            
            top.add(chatArea);
            bottom.add(msgField);
            bottom.add(button);
            msgField.requestFocus();
      }

      void updateChatArea(int key)
      {
            if (key == Event.ENTER) {
                  try {
                        DatagramSocket socket;
                        DatagramPacket packet;
                        byte[] data = new byte[256];
            
                        msg = msgField.getText();
                        msg.getBytes(0, msg.length(), data, 0);
                        socket = new DatagramSocket();
                        packet = new DatagramPacket(data, data.length,
                              InetAddress.getByName("copet"), PortNum);
                        socket.send(packet);
                  
                        packet = new DatagramPacket(data, data.length);
                        socket.receive(packet);
                        msg = new String(packet.getData(), 0);
                  
                        chatArea.appendText("<Your Name> " + msg + '\n');
                        socket.close();         
                  }
                  catch (UnknownHostException e) {
                        chatArea.appendText("Host couldn't be reached\n");
                  }
                  catch (Exception e) {
                        chatArea.appendText("Error : " + e + '\n');
                  }
                  msgField.setText("");
            }
      }
      
      public boolean keyDown(Event event, int key)
      {
            if (event.target == msgField)
                  updateChatArea(key);
            return false;
      }
}


I started the server, the server starts okay in MS-DOS. but when I opened my client applet using internet explorer 4.0, this comes out:

Error : com.ms.security.SecurityExceptionEx[KadomonChatApplet.updateChatArea]: cannot access "copet"

so.... can anyone let me know what is the problem ??

ps: i'm using PWS on Windows 95

thx
0
Comment
Question by:techpage
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2806587

replace  copet

with

getCodeBase().getHost()
0
 
LVL 3

Expert Comment

by:rjackman
ID: 2806633
i think this is because u are using the applet to connect the socket
instead of that try using servlet which when called will throw ur applet
to client machine
0
 

Expert Comment

by:anoop_pankaj
ID: 2806657
Applets can communicate with only the servers from which it is downloaded.So what you should do is put your applet(ie the HTML) in a server.It can be IIS,PeerWebServer,PersonalWebserver etc.Then download the applet in your browser .
0
 
LVL 3

Expert Comment

by:rjackman
ID: 2806678
hey anoop_pankaj
even if u place the applet on ur server it will give the same error
RJackman
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:techpage
ID: 2807736
anoop_pankaj, thanks for the answer but that still doesn't solve my problem...
could you guys please tell me how to solve... because i'm currently using PWS but it still can't work...

ravindra76, now i'm changing the code just like you suggested, but it still remain the same...

Error : com.ms.security.SecurityExceptionEx[KadomonChatApplet.updateChatArea]: cannot access "127.0.0.1":1234

so... how do i cure this ??

rjackman, yes i know a bit about servlet... do you mean I must learn servlet programming ?? i'm not sure if i had enough time... do you have another solution ??

please... guyss...

0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2808110
If You are running java 1.2 version or above

repalce Your java.polic file with


// Standard extensions get all permissions by default

grant {
      permission java.security.AllPermission;
};



Note: the file is present in

c:\jdkhome\jre\lib\security

:)
0
 
LVL 1

Expert Comment

by:yogeshr
ID: 2828848
yes , this application is running fine on WiNNT. I f you are running the client and the server on two different pcs you just pass the ipaddress of the pc where you are running the server. Esle if youare running the client and server from two different command prompts use InetAddress.getLocalHost() . It should work.
0
 

Accepted Solution

by:
bhajanpreets earned 50 total points
ID: 2830047
i very well agree with yogeshr.i have practically seen this and it works.as you are using pws just edit the properties and give the path of your virtual directory to your chat folder.
make default file as your html for applet.and browse...
if your client is on diffrent m/c give it the ip address of your server.if it is in same m/c use inetAddress.getLocalHost().
   its the same answer but i just repeated to make the pt. stronger
bhajan

0
 
LVL 1

Expert Comment

by:yogeshr
ID: 2832012
then in that case who should get the points???
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
basic hardware to learn oop advanced design patterns 3 88
maven project error 5 48
github account with ecipse 1 42
map related example 6 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now