Commit 06ffa244 authored by 9731087's avatar 9731087

First edition: client connects to the server. client sends a message and server…

First edition: client connects to the server. client sends a message and server catches. server uses Thread for managing clients
parent c96f69bf
import java.io.DataInputStream; import java.io.*;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket; import java.net.Socket;
public class ClientHandler { public class ClientHandler implements Runnable {
private Socket socket; private Socket client;
private DataOutputStream dataOutputStream; private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream; private DataInputStream dataInputStream;
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; }
dataOutputStream = new DataOutputStream(client.getOutputStream()); this.client = client;
dataInputStream = new DataInputStream(client.getInputStream()); // this.dataOutputStream = new DataOutputStream(client.getOutputStream());
run();
} }
private void handleIncomingMessages(String message) { private void handleIncomingMessages(String message) {
//TODO: implement this method //TODO: implement this method
// System.out.println("This is incoming message from Client: ");
System.out.println(message); System.out.println(message);
} }
@Override
public void run() { public void run() {
while (true) { try {
try { BufferedReader bufferedReader = null;
String input = dataInputStream.readUTF();
handleIncomingMessages(input); bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (IOException e) { String input = bufferedReader.readLine();
e.printStackTrace(); handleIncomingMessages(input);
} } catch (IOException e) {
e.printStackTrace();
} }
} }
} }
import java.io.IOException;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
System.out.println("Hello World!"); System.out.println(" -ServerOn\n");
Server server = new Server();
new Thread(server).start();
} }
} }
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;
public class Server implements Runnable {
public class Server {
private static final int PORT = 4321; private static final int PORT = 4321;
private ServerSocket serverSocket; private ServerSocket serverSocket;
private ArrayList<ClientHandler> clientHandlers;
private ArrayList<Socket> sockets;
boolean isRun = true;
public Server() throws IOException { public Server() throws IOException {
serverSocket = new ServerSocket(PORT); serverSocket = new ServerSocket(PORT, 2);
while (true) { }
Socket client = serverSocket.accept();
public void stop() throws IOException {
this.serverSocket.close();
isRun = false;
}
@Override
public void run() {
while (isRun) {
Socket client = null;
try {
// System.out.println("Waiting for Clients...");
client = this.serverSocket.accept();
System.out.println("CLIENT: " + client.getRemoteSocketAddress() + " CONNECTED");
} catch (IOException e) {
e.printStackTrace();
}
// this.sockets.add(client);
try { try {
ClientHandler clientHandler = new ClientHandler(client); ClientHandler clientHandler = new ClientHandler(client);
// this.clientHandlers.add(clientHandler);
Thread td = new Thread(clientHandler);
td.start();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
}
} }
package networking;
import java.net.*;
import java.io.*;
public class Network {
public static void main(String[] args) {
String ip = "localhost";
int port = 4321;
try {
System.out.println("Connecting to " + ip + " on port " + port);
Socket socket = new Socket(ip, port);
System.out.println("Just connected to " + socket.getRemoteSocketAddress());
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
printWriter.println("Hello from " + socket.getLocalSocketAddress());
printWriter.flush();
//socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
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