Commit d1c335be authored by Amirhosein Rajabpour's avatar Amirhosein Rajabpour

project is completed!

with huge modification but simpler!!
message is sent and shown in server but there is no thread
parent 7f22e073
...@@ -6,7 +6,6 @@ public class Main { ...@@ -6,7 +6,6 @@ public class Main {
public static void main(String[] args){ public static void main(String[] args){
// ChatRoomGUI chat1 = new ChatRoomGUI();
UsernameFrame chat2 = new UsernameFrame(); UsernameFrame chat2 = new UsernameFrame();
} }
} }
...@@ -10,15 +10,17 @@ public class Network { ...@@ -10,15 +10,17 @@ public class Network {
private static final int port = 4321; private static final int port = 4321;
private static final String host = "127.0.0.1" ; private static final String host = "127.0.0.1" ;
Socket client;
Scanner socketin; Scanner socketin;
Formatter socketOut; Formatter socketOut;
public Network() throws IOException{ public Network() throws IOException{
try(Socket client = new Socket( host, port); try {
Scanner socketin = new Scanner(client.getInputStream()); client = new Socket( host , port );
Formatter socketOut = new Formatter(client.getOutputStream());) socketin = new Scanner(client.getInputStream());
{ socketOut = new Formatter(client.getOutputStream());
socketOut.flush(); socketOut.flush();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
e.printStackTrace(); e.printStackTrace();
......
package chatRoom.server; //package chatRoom.server;
import java.io.DataInputStream; //import java.io.DataInputStream;
import java.io.DataOutputStream; //import java.io.DataOutputStream;
import java.io.IOException; //import java.io.IOException;
import java.net.Socket; //import java.net.Socket;
import java.util.Formatter; //import java.util.Formatter;
import java.util.Scanner; //import java.util.Scanner;
//
public class ClientHandler { //public class ClientHandler {
private Socket socket; // private Socket socket;
// private DataOutputStream dataOutputStream; //// private DataOutputStream dataOutputStream;
// private DataInputStream dataInputStream; //// private DataInputStream dataInputStream;
Scanner in; // Scanner in;
Formatter out; // Formatter out;
//
//
public ClientHandler(Socket client) throws Exception { // public ClientHandler(Socket client) throws Exception {
if (client == null) // if (client == null)
throw new Exception("client can't be null"); // throw new Exception("client can't be null");
this.socket = client; // this.socket = client;
// dataOutputStream = new DataOutputStream(client.getOutputStream()); //// dataOutputStream = new DataOutputStream(client.getOutputStream());
// dataInputStream = new DataInputStream(client.getInputStream()); //// dataInputStream = new DataInputStream(client.getInputStream());
try(Scanner in = new Scanner(client.getInputStream()); // try(Scanner in = new Scanner(client.getInputStream());
Formatter out = new Formatter(client.getOutputStream());){ // Formatter out = new Formatter(client.getOutputStream());){
run(); // run();
} // }
//
} // }
//
private void handleIncomingMessages(String message){ // private void handleIncomingMessages(String message){
System.out.println(message); // System.out.println(message);
//
} // }
//
public void run() { // public void run() {
while(true){ // while(true){
String input = in.next() ; // String input = in.next() ;
handleIncomingMessages(input); // handleIncomingMessages(input);
} // }
} // }
} // }
\ No newline at end of file \ No newline at end of file
package chatRoom.server;
import java.io.IOException;
import java.net.ServerSocket;
public class Main {
public static void main(String[] args){
try {
SecondServer secondServer = new SecondServer();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package chatRoom.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.Scanner;
public class SecondServer {
private static final int PORT = 4321;
private ServerSocket serverSocket;
ArrayList<Socket> users = new ArrayList<>();
Scanner in;
Formatter out;
public SecondServer() throws IOException {
serverSocket = new ServerSocket(PORT);
while (true) {
Socket client = serverSocket.accept();
try{
in = new Scanner(client.getInputStream());
out = new Formatter(client.getOutputStream());
String input = in.next() ;
System.out.println(input);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package chatRoom.server; //package chatRoom.server;
//
import java.io.IOException; //import java.io.IOException;
import java.net.ServerSocket; //import java.net.ServerSocket;
import java.net.Socket; //import java.net.Socket;
import java.util.ArrayList; //import java.util.ArrayList;
//
public class Server { //public class Server {
//
private static final int PORT = 4321; // private static final int PORT = 4321;
private ServerSocket serverSocket; // private ServerSocket serverSocket;
ArrayList<Socket> users = new ArrayList<>(); // ArrayList<Socket> users = new ArrayList<>();
//
public Server() throws IOException { // public Server() throws IOException {
serverSocket = new ServerSocket(PORT); // serverSocket = new ServerSocket(PORT);
while (true) { // while (true) {
Socket client = serverSocket.accept(); // Socket client = serverSocket.accept();
try { // try {
ClientHandler clientHandler = new ClientHandler(client); // ClientHandler clientHandler = new ClientHandler(client);
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
} // }
//
} // }
} // }
//
} //}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment