?
Solved

Client - server chat room

Posted on 2000-05-13
9
Medium Priority
?
812 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 100 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

770 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