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.DataOutputStream;
import java.io.IOException;
import java.io.*;
import java.net.Socket;
public class ClientHandler {
private Socket socket;
public class ClientHandler implements Runnable {
private Socket client;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
public ClientHandler(Socket client) throws Exception {
if (client == null)
if (client == null) {
throw new Exception("client can't be null");
this.socket = client;
dataOutputStream = new DataOutputStream(client.getOutputStream());
dataInputStream = new DataInputStream(client.getInputStream());
run();
}
this.client = client;
// this.dataOutputStream = new DataOutputStream(client.getOutputStream());
}
private void handleIncomingMessages(String message) {
//TODO: implement this method
// System.out.println("This is incoming message from Client: ");
System.out.println(message);
}
@Override
public void run() {
while (true) {
try {
String input = dataInputStream.readUTF();
handleIncomingMessages(input);
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader bufferedReader = null;
bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String input = bufferedReader.readLine();
handleIncomingMessages(input);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
public static void main(String[] args) throws IOException {
System.out.println(" -ServerOn\n");
Server server = new Server();
new Thread(server).start();
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server implements Runnable {
public class Server {
private static final int PORT = 4321;
private ServerSocket serverSocket;
private ArrayList<ClientHandler> clientHandlers;
private ArrayList<Socket> sockets;
boolean isRun = true;
public Server() throws IOException {
serverSocket = new ServerSocket(PORT);
while (true) {
Socket client = serverSocket.accept();
serverSocket = new ServerSocket(PORT, 2);
}
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 {
ClientHandler clientHandler = new ClientHandler(client);
// this.clientHandlers.add(clientHandler);
Thread td = new Thread(clientHandler);
td.start();
} catch (Exception e) {
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