مساعدة Java) إضافة Bottun يظهر المستخدمين المتصلين بالسيرفر
السلام عليكم ورحمة الله وبركاته
انا عندي برنامج جاهز بلغة الجافا وابغى اعرف كيف اضيف له Bottun يظهر لي المستخدمين المتصلين بالسيرفر
البرنامج TCP Chat
هنا السيرفر
كود PHP:
//: TCPChatServer.java
// SYNOPSIS: TCPChatServer
// DESCRIPTION: The program creates a TCP socket in the inet
// listen for connections from TCPClients, accept
// clients into private sockets.
////////////////////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
class EchoServe extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public EchoServe (Socket s)
throws IOException {
socket = s;
System.out.println("Serving: "+socket);
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Enable auto-flush:
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
// add the connected client out stream to the vector
TCPChatServer.outToClients.add(out);
// If any of the above calls throw an
// exception, the caller is responsible for
// closing the socket. Otherwise the thread
// will close it.
start(); // Calls run()
}
public void run() {
try {
while (true)
{
System.out.println("....Server is waiting...");
String str = in.readLine();
if (str== null ) break;
TCPChatServer.boradcast(str);
}
System.out.println("Disconnected with.."+socket);
// Remove client from the vector
} catch (IOException e) {
} finally {
try {
socket.close();
} catch(IOException e) {}
}
}
}
public class TCPChatServer {
Vector users = new Vector();
static Vector outToClients = new Vector();
static int PORT=12345; // assign to next avalible Port.
public static void boradcast(String message) {
// send message to all connected users
for ( PrintWriter c : outToClients )
{
c.println(message);
c.flush();
}
}
public static void main(String[] args)
throws IOException {
if (args.length == 1) {
PORT=Integer.parseInt(args[0]); // assign to a given Port.
}
// Create a Server Socket.
ServerSocket s = new ServerSocket(PORT);
InetAddress addrs= InetAddress.getLocalHost();
// Or InetAddress addrs= InetAddress.getByName("localhost");
// Or InetAddress addrs= InetAddress.getByName("127.0.0.1");
System.out.println("TCP/Server running on : "+ addrs +" ,Port "+s.getLocalPort());
try {
while(true) {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
new EchoServe(socket); // Handle the incoming Client.
} catch(IOException e) {
// If it fails, close the socket,
// otherwise the thread will close it:
socket.close();
}
}
} finally {
s.close();
}
}
} ///:~
وهناا الكلاينت
كود PHP:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.JOptionPane;
import static java.lang.System.out;
public class TCPChatClient extends JFrame implements ActionListener {
private final String screenName;
// GUI stuff
private JTextArea enteredText = new JTextArea(10, 32);
private JTextField typedText = new JTextField(32);
// socket for connection to chat server
private Socket socket;
// for writing to and reading from the server
private BufferedReader in;
private PrintWriter out;
السلام عليكم ورحمة الله وبركاته
انا عندي برنامج جاهز بلغة الجافا وابغى اعرف كيف اضيف له Bottun يظهر لي المستخدمين المتصلين بالسيرفر
البرنامج TCP Chat
هنا السيرفر
كود PHP:
//: TCPChatServer.java
// SYNOPSIS: TCPChatServer
// DESCRIPTION: The program creates a TCP socket in the inet
// listen for connections from TCPClients, accept
// clients into private sockets.
////////////////////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
class EchoServe extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public EchoServe (Socket s)
throws IOException {
socket = s;
System.out.println("Serving: "+socket);
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Enable auto-flush:
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
// add the connected client out stream to the vector
TCPChatServer.outToClients.add(out);
// If any of the above calls throw an
// exception, the caller is responsible for
// closing the socket. Otherwise the thread
// will close it.
start(); // Calls run()
}
public void run() {
try {
while (true)
{
System.out.println("....Server is waiting...");
String str = in.readLine();
if (str== null ) break;
TCPChatServer.boradcast(str);
}
System.out.println("Disconnected with.."+socket);
// Remove client from the vector
} catch (IOException e) {
} finally {
try {
socket.close();
} catch(IOException e) {}
}
}
}
public class TCPChatServer {
Vector
static Vector
static int PORT=12345; // assign to next avalible Port.
public static void boradcast(String message) {
// send message to all connected users
for ( PrintWriter c : outToClients )
{
c.println(message);
c.flush();
}
}
public static void main(String[] args)
throws IOException {
if (args.length == 1) {
PORT=Integer.parseInt(args[0]); // assign to a given Port.
}
// Create a Server Socket.
ServerSocket s = new ServerSocket(PORT);
InetAddress addrs= InetAddress.getLocalHost();
// Or InetAddress addrs= InetAddress.getByName("localhost");
// Or InetAddress addrs= InetAddress.getByName("127.0.0.1");
System.out.println("TCP/Server running on : "+ addrs +" ,Port "+s.getLocalPort());
try {
while(true) {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
new EchoServe(socket); // Handle the incoming Client.
} catch(IOException e) {
// If it fails, close the socket,
// otherwise the thread will close it:
socket.close();
}
}
} finally {
s.close();
}
}
} ///:~
وهناا الكلاينت
كود PHP:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.JOptionPane;
import static java.lang.System.out;
public class TCPChatClient extends JFrame implements ActionListener {
private final String screenName;
// GUI stuff
private JTextArea enteredText = new JTextArea(10, 32);
private JTextField typedText = new JTextField(32);
// socket for connection to chat server
private Socket socket;
// for writing to and reading from the server
private BufferedReader in;
private PrintWriter out;