الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

JApplet chat application

مغلق
بدأه SelvaDoR في 19 أغسطس 2006 · 8 رد · 938 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

مرحبا ...

انا عضو جديد هنا .. و عندي مشكله و أتمنه المساعدة السريعة

i wrote a JApplet chat application

and this is the sourse code for the server

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.net.*;


public class Server extends JApplet implements ActionListener

{


JButton btn;

JTextArea main;

JTextField msg;

String maintxt ="";

JLabel labl;

String message = "";



Socket socket = null;

ServerSocket srv = null;



public void init()

{



Container cont = getContentPane();


cont.setBackground(Color.gray);

cont.setLayout(new FlowLayout());

main= new JTextArea("",10,20);

labl = new JLabel("Server");

btn = new JButton("Send");

btn.addActionListener(this);

msg = new JTextField(25);


cont.add(labl);

cont.add(new JScrollPane(main));

cont.add(msg);

cont.add(btn);

Recive r = new Recive();

r.start();



}




public void actionPerformed(ActionEvent ev)

{

if (ev.getSource() == btn)

{

maintxt +=msg.getText()+"\n";

main.setText(maintxt);


try{


OutputStream out = socket.getOutputStream();

message = msg.getText();

out.write(message.getBytes());

msg.setText("");



msg.setText("");

}

catch(Exception ex)

{

System.out.println(ex);

}

}

}



class Recive extends Thread

{


public void run()

{



try {

int port = 5555;

srv = new ServerSocket(port);

socket = srv.accept();


while (true)

{

InputStream in = socket.getInputStream();

byte[]b = new byte[1024];

in.read(b);

String s = new String(b);

maintxt +=s.trim()+"\n";

main.setText(maintxt);

}

}

catch (Exception e)

{

System.out.println(e);

}

}

}

}

and this the client

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.net.*;

import javax.swing.JTextField;


public class Client extends JApplet implements ActionListener

{


JButton btn;

JTextArea main;

JTextField msg;

JLabel labl;

String maintxt ="";


Socket socket = null;

OutputStream out = null;

public Client()

{

try

{

InetAddress addr = InetAddress.getByName("my ip");

int port = 5555;

socket = new Socket(addr, port);

}

catch(Exception ex)

{

System.out.println(ex);

}

}


public void init()

{

Container cont = getContentPane();


cont.setBackground(Color.gray);

cont.setLayout(new FlowLayout());


main= new JTextArea("",10,20);

//txt.setColumns(20);

//txt.setRows(10);

btn = new JButton("Send");

btn.addActionListener(this);

msg = new JTextField(25);

labl = new JLabel("Client");

cont.add(labl);

cont.add(new JScrollPane(main));

cont.add(msg);

cont.add(btn);


Recive r = new Recive();

r.start();

}


public void actionPerformed(ActionEvent ev)

{

if (ev.getSource() == btn)

{

maintxt +=msg.getText()+"\n";

main.setText(maintxt);




BufferedReader input = null;

String message = "";


try {

message = msg.getText();

out = socket.getOutputStream();

out.write(message.getBytes());

msg.setText("");


}

catch (Exception e)

{

System.out.println(e);

}

}

}



class Recive extends Thread

{

public void run()

{


try {

while (true)

{


InputStream in = socket.getInputStream();

System.out.println("in client while");

byte[]b = new byte[1024];

in.read(b);

String s = new String(b);

maintxt +=s.trim()+"\n";

main.setText(maintxt);


}


}

catch (Exception e)

{

System.out.println(e+"in thread");

}


}

}


}

and this is the client html page thats contain the applet(Client.html)

<html>

<head>

</head>

<body>

<applet code=Client.class width=400 height=400 codebase = "http://myip:8080/Chat1">

</applet>

</body>

</html>

so

this application works fine on my computer, that means the server and the client can chat toguther on my computer

but

when another computer on the LAN used the application by (Client.html)

the server gives me this
EXCeption

Why , and what is the solution

java.security.AccessControlException: access denied (java.net.SocketPermission

myip:1210 accept,resolve)

thanx alot

تم تعديل هذه المشاركة بواسطة SelvaDoR في 19 أغسطس 2006 في 19:46

#2

أختي المشكلة تكمن هنا

			InetAddress addr = InetAddress.getByName("my ip");
			int port = 5555;
			socket = new Socket(addr, port);

يجب أن تستبدليها بالip للسيرفر

أرجو أن تكوني فهمتي عليه

سلام

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3

when i wrote "my ip" i mean ...

in my code i wrote my ip

i know that

Plz replace it and put your ip and try it

#4
SelvaDoR كتب:
مرحبا ...

انا عضو جديد هنا .. و عندي مشكله و أتمنه المساعدة السريعة

i wrote a JApplet chat application

and this is the sourse code for the server

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.net.*;


public class Server extends JApplet implements ActionListener

{


JButton btn;

JTextArea main;

JTextField msg;

String maintxt ="";

JLabel labl;

String message = "";

Socket socket = null;

ServerSocket srv = null;

public void init()

{

Container cont = getContentPane();


cont.setBackground(Color.gray);

cont.setLayout(new FlowLayout());

main= new JTextArea("",10,20);

labl = new JLabel("Server");

btn = new JButton("Send");

btn.addActionListener(this);

msg = new JTextField(25);


cont.add(labl);

cont.add(new JScrollPane(main));

cont.add(msg);

cont.add(btn);

Recive r = new Recive();

r.start();

}

public void actionPerformed(ActionEvent ev)

{

if (ev.getSource() == btn)

{

maintxt +=msg.getText()+"\n";

main.setText(maintxt);


try{


OutputStream out = socket.getOutputStream();

message = msg.getText();

out.write(message.getBytes());

msg.setText("");

msg.setText("");

}

catch(Exception ex)

{

System.out.println(ex);

}

}

}

class Recive extends Thread

{


public void run()

{

try {

int port = 5555;

srv = new ServerSocket(port);

socket = srv.accept();


while (true)

{

InputStream in = socket.getInputStream();

byte[]b = new byte[1024];

in.read(b);

String s = new String(b);

maintxt +=s.trim()+"\n";

main.setText(maintxt);

}

}

catch (Exception e)

{

System.out.println(e);

}

}

}

}

and this the client

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.net.*;

import javax.swing.JTextField;


public class Client extends JApplet implements ActionListener

{


JButton btn;

JTextArea main;

JTextField msg;

JLabel labl;

String maintxt ="";


Socket socket = null;

OutputStream out = null;

public Client()

{

try

{

InetAddress addr = InetAddress.getByName("my ip");

int port = 5555;

socket = new Socket(addr, port);

}

catch(Exception ex)

{

System.out.println(ex);

}

}


public void init()

{

Container cont = getContentPane();


cont.setBackground(Color.gray);

cont.setLayout(new FlowLayout());


main= new JTextArea("",10,20);

//txt.setColumns(20);

//txt.setRows(10);

btn = new JButton("Send");

btn.addActionListener(this);

msg = new JTextField(25);

labl = new JLabel("Client");

cont.add(labl);

cont.add(new JScrollPane(main));

cont.add(msg);

cont.add(btn);


Recive r = new Recive();

r.start();

}


public void actionPerformed(ActionEvent ev)

{

if (ev.getSource() == btn)

{

maintxt +=msg.getText()+"\n";

main.setText(maintxt);

BufferedReader input = null;

String message = "";


try {

message = msg.getText();

out = socket.getOutputStream();

out.write(message.getBytes());

msg.setText("");


}

catch (Exception e)

{

System.out.println(e);

}

}

}

class Recive extends Thread

{

public void run()

{


try {

while (true)

{


InputStream in = socket.getInputStream();

System.out.println("in client while");

byte[]b = new byte[1024];

in.read(b);

String s = new String(b);

maintxt +=s.trim()+"\n";

main.setText(maintxt);


}


}

catch (Exception e)

{

System.out.println(e+"in thread");

}


}

}


}

and this is the client html page thats contain the applet(Client.html)

<html>

<head>

</head>

<body>

<applet code=Client.class width=400 height=400 codebase = "http://myip:8080/Chat1">

</applet>

</body>

</html>

so

this application works fine on my computer, that means the server and the client can chat toguther on my computer

but

when another computer on the LAN used the application by (Client.html)

the server gives me this
EXCeption

Why , and what is the solution

java.security.AccessControlException: access denied (java.net.SocketPermission

client ip:1210 accept,resolve)

thanx alot
#5

أختي أعتذر عن التأخير كل ما عليك فعله هو تغيير my ip إلى اسم الجهاز المشترك معك على الشبكة

أنا لم أقم بتجريبها لأني لا أعمل على شبكة

أما إن لم تضبط معك فقومي بجعل الجهاز الخادم بإخبار الجهاز العميل برقم الip كحل مؤقت أنا لست في البيت لذا لا أستطيع المساعدة بشكل جيد

صحيح لاحظت أن برنامجك لا يعمل كتشات بمعنى تشات فلم يستطع السيرفر التعامل سوى مع عميل واحد فقط

أرجو أن أكون ساعدتك وأعتذر على التأخير

الله معكم

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#6

sorrry

you dount understand please try it and reolace "myip" and put your ip and tell me ...

thanx

#7

يرجى مراجعة قواعد المشاركة...

حيث ان العنوان مخالف ..

سوف اقوم بتعديل العنوان

و شكرا للتفهم

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why

#8

the problem is still there

Can any one solve it

#9

Al salamo 3alykom,

i hope that i can help...

according to my tiny knowledge about java in general and applets and networking in particular, i think that : the applicatoin and the applet must be in the same machine, the user invoke the applet from his machine and then communicate with the server running on the server machine as follows:

1- install apache or IIS on your machine (the server)

2- run the application -- the server program-- and then put the applet at the following dir in your machine : localhost/applet.html -- where localhost is your webserver dir

3- go to the other computer -- the client -- and invoke http://thehostmachineIP/applet.html

good luck, Al salamo 3alykom wa ra7mat ALLAH wa brakato.

هذا الموضوع مغلق.

مواضيع مشابهة