Commit 1510f37d authored by nargessalehi98's avatar nargessalehi98

Add servr

parents
Pipeline #5122 canceled with stages
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
import java.io.*;
import java.net.Socket;
public class Handler implements Runnable {
public Socket socket;
public DataInputStream streamIn = null;
public DataOutputStream streamOut=null;
public Handler(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
try {
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
boolean done = false;
String line = "";
while (!done) {
try {
line += streamIn.readUTF();
System.out.println(line);
if (line.contains("bye")) {
done = true;
break;
}
} catch (IOException ioe) {
done = true;
}
try {
streamOut=new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
streamOut.writeUTF(line);
streamOut.flush();
} catch (IOException ioe) {
System.out.println("Sending error: " + ioe.getMessage());
}
}
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static ServerSocket server;
public static void main(String[] args) {
System.out.println("Binding to port " + 5001 + ", please wait ...");
try {
server = new ServerSocket(5001);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Server started: " + server);
while (true) {
System.out.println("Waiting for a client ...");
Socket socket = null;
try {
socket = server.accept();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Client accepted: " + socket);
Thread thread = new Thread(new Handler(socket), "thread");
thread.start();
}
}
}
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