مساء الخير
في برنامج اخذت الأكود من كتاب.. عباره عن قسم server وقسم client
ال server يستقبل ال string من اي client يتم انشائه , ويرسل لكل ال clients المتصله معه بما فيهم ال client الي رسل
طبعاً البرنامج ما فيه إلا client واحد .. عشان كذا لما ارسل اي شيء منه .. المفروض اني استقبل الي رسلته
انا الآن ابي اجرب البرنامج .. حاولت اشغله ما اشتغل
في ملاحظه مكتوبه في الكتاب بس مافهمتها .. هذي الملاحظه :
to run chat client, you need two terminals
first, launch this server from one terminal,
then launch the client from another terminal
ماذا يقصد بـ terminal هنا ؟
هل البرامج التي تبنا على server & client لتشغيلها لازم نحتاج جهازين على الأقل او شبكه فعليه ؟
مع العلم ان الكتاب حاط ملاحظه على ال setUpNetworking method تقول:
sock = new Socket("127.0.0.1", 5000);
we're using localhost so you can test the client and server on one machine
هذه الملاحظه تقول انه يمكنني تشغيل البرنامج على جهاز واحد
إذاً اين هي المشكله هنا ؟ ام ان هناك خطأ في الكود لم انتبه له ؟
الصوره توضح لما سيبدو عليه البرنامج إذا اشتغل
اترككم مع الكود..
---------------------------
server
import java.io.*;
import java.net.*;
import java.util.*;
public class VerySimpleChatServer {
ArrayList clientOutputStreams;
public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;
public ClientHandler(Socket clientSocket) {
try {
sock = clientSocket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} catch(Exception ex) {ex.printStackTrace(); }
} // close constructor
public void run() {
String message;
try {
while((message = reader.readLine()) != null) {
System.out.println("read " + message);
tellEveryone(message);
} // close while
} catch(Exception ex) {ex.printStackTrace(); }
} // c run
} // c inner class
public static void main(String[] args) {
new VerySimpleChatServer().go();
}
public void go() {
clientOutputStreams = new ArrayList();
try {
ServerSocket serverSocket = new ServerSocket(5000);
while(true) {
Socket clientSocket = serverSocket.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);
Thread t = new Thread(new ClientHandler(clientSocket));
t.start();
System.out.println("got a connection");
}
} catch(Exception ex) { ex.printStackTrace(); }
} // c go
public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();
while(it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
} catch(Exception ex) { ex.printStackTrace(); }
} // end while
} // end tellEveryone
} // end class
------------------------
client
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleChatClient {
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
public static void main(String[] args) {
SimpleChatClient client = new SimpleChatClient();
client.go();
}
public void go() {
JFrame frame = new JFrame("Ludicrously Simle Chat Client");
JPanel mainPanel = new JPanel();
incoming = new JTextArea(15,50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(20);
JButton sendButton = new JButton("send");
sendButton.addActionListener(new sendButtonListener());
mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(400, 500);
frame.setVisible(true);
} // close go
private void setUpNetworking() {
try {
sock = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("netwotking established");
} catch(IOException ex) {
ex.printStackTrace();
}
} // close setup networking
public class sendButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
try {
writer.println(outgoing.getText());
writer.flush();
} catch(Exception ex) { ex.printStackTrace(); }
outgoing.setText("");
outgoing.requestFocus();
}
} // close inner class
public class IncomingReader implements Runnable {
public void ran() {
String message;
try {
while((message = reader.readLine()) != null) {
System.out.println("read" + message);
incoming.append(message + "\n");
}
} catch(Exception ex) {ex.printStackTrace(); }
}
}
}l




