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
Solved

Client - server chat room

Posted on 2000-05-13
9
804 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 IE Certificate Issue 10 51
ejb example issues 3 27
Notify sent to other threads in Java 9 33
Java class and jar 3 25
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

828 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