Commit 7534414b authored by Roham's avatar Roham

lab10 new

parent 2baf75d8
Pipeline #666 failed with stages
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ClientHandler {
private Socket socket;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
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());
run();
}
private void handleIncomingMessages(String message)
{
System.out.println(message);
}
public void run () {
try {
String input = dataInputStream.readUTF();
handleIncomingMessages(input);
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
import java.io.IOException;
public class Main {
public static void main(String [] args) {
try {
Thread t = new Server(1234);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.net.*;
import java.io.*;
public class Server extends Thread {
private ServerSocket serverSocket;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
}
public void run() {
while (true) {
try {
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket client = serverSocket.accept();
ClientHandler clientHandler = new ClientHandler(client);
client.close();
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
} catch (IOException e) {
e.printStackTrace();
break;
} 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