Solved

Client - server chat room

Posted on 2000-05-13
9
800 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

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