Solved

Client - server chat room

Posted on 2000-05-13
9
795 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 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

17 Experts available now in Live!

Get 1:1 Help Now