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 {
public static void main(String[] args){
// ChatRoomGUI chat1 = new ChatRoomGUI();
UsernameFrame chat2 = new UsernameFrame();
}
}
......@@ -10,15 +10,17 @@ public class Network {
private static final int port = 4321;
private static final String host = "127.0.0.1" ;
Socket client;
Scanner socketin;
Formatter socketOut;
public Network() throws IOException{
try(Socket client = new Socket( host, port);
Scanner socketin = new Scanner(client.getInputStream());
Formatter socketOut = new Formatter(client.getOutputStream());)
{
try {
client = new Socket( host , port );
socketin = new Scanner(client.getInputStream());
socketOut = new Formatter(client.getOutputStream());
socketOut.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
......
package chatRoom.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Formatter;
import java.util.Scanner;
public class ClientHandler {
private Socket socket;
// private DataOutputStream dataOutputStream;
// private DataInputStream dataInputStream;
Scanner in;
Formatter out;
public ClientHandler(Socket client) throws Exception {
if (client == null)
throw new Exception("client can't be null");
this.socket = client;
// dataOutputStream = new DataOutputStream(client.getOutputStream());
// dataInputStream = new DataInputStream(client.getInputStream());
try(Scanner in = new Scanner(client.getInputStream());
Formatter out = new Formatter(client.getOutputStream());){
run();
}
}
private void handleIncomingMessages(String message){
System.out.println(message);
}
public void run() {
while(true){
String input = in.next() ;
handleIncomingMessages(input);
}
}
}
\ No newline at end of file
//package chatRoom.server;
//import java.io.DataInputStream;
//import java.io.DataOutputStream;
//import java.io.IOException;
//import java.net.Socket;
//import java.util.Formatter;
//import java.util.Scanner;
//
//public class ClientHandler {
// private Socket socket;
//// private DataOutputStream dataOutputStream;
//// private DataInputStream dataInputStream;
// Scanner in;
// Formatter out;
//
//
// public ClientHandler(Socket client) throws Exception {
// if (client == null)
// throw new Exception("client can't be null");
// this.socket = client;
//// dataOutputStream = new DataOutputStream(client.getOutputStream());
//// dataInputStream = new DataInputStream(client.getInputStream());
// try(Scanner in = new Scanner(client.getInputStream());
// Formatter out = new Formatter(client.getOutputStream());){
// run();
// }
//
// }
//
// private void handleIncomingMessages(String message){
// System.out.println(message);
//
// }
//
// public void run() {
// while(true){
// String input = in.next() ;
// handleIncomingMessages(input);
// }
// }
// }
\ 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;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
private static final int PORT = 4321;
private ServerSocket serverSocket;
ArrayList<Socket> users = new ArrayList<>();
public Server() throws IOException {
serverSocket = new ServerSocket(PORT);
while (true) {
Socket client = serverSocket.accept();
try {
ClientHandler clientHandler = new ClientHandler(client);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//package chatRoom.server;
//
//import java.io.IOException;
//import java.net.ServerSocket;
//import java.net.Socket;
//import java.util.ArrayList;
//
//public class Server {
//
// private static final int PORT = 4321;
// private ServerSocket serverSocket;
// ArrayList<Socket> users = new ArrayList<>();
//
// public Server() throws IOException {
// serverSocket = new ServerSocket(PORT);
// while (true) {
// Socket client = serverSocket.accept();
// try {
// ClientHandler clientHandler = new ClientHandler(client);
// } catch (Exception e) {
// 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