سلام. دوستان یه برنامه چت ساده نوشتم اما کار نمیکنه. نمیدونم مشکل از کجاست. ممنون میشم کمک کنید. اول کلاس سرور:
package chatS;
import java.net.*;
import java.util.*;
import java.io.*;
public class Server {
public static void main(String[] args) {
try (ServerSocket server = new ServerSocket(3000);
Socket socket = server.accept();
Scanner systemIn = new Scanner(System.in);
Scanner socketIn = new Scanner(socket.getInputStream());
Formatter socketOut = new Formatter(socket.getOutputStream());){
String send;
String received;
System.out.println("Server is ready to chat...");
do {
received = socketIn.nextLine();
if (received != null)
System.out.println("Client: " + received);
send = systemIn.nextLine();
socketOut.format(send);
socketOut.flush();
} while ((!received.contains("exit")) || (!send.contains("exit")));
} catch (IOException e) {
e.printStackTrace();
}
}
}
کلاس کلاینت:
package chatC;
import java.util.*;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try (Socket socket = new Socket("localhost" , 3000);
Scanner systemIn = new Scanner(System.in);
Scanner socketIn = new Scanner(socket.getInputStream());
Formatter socketOut = new Formatter(socket.getOutputStream());) {
System.out.println("Client is ready to chat...");
String send;
String received;
do {
received = socketIn.nextLine();
if (received != null)
System.out.println(received);
send = systemIn.nextLine();
socketOut.format(send);
socketOut.flush();
} while ((!systemIn.equals("exit")) || (!socketIn.equals("exit")));
} catch (IOException e) {
e.printStackTrace();
}
}
}