Compare commits
3 Commits
19f9cd84de
...
d4572677f5
Author | SHA1 | Date |
---|---|---|
|
d4572677f5 | |
|
53c846a386 | |
|
b0fcb7c79c |
Binary file not shown.
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
// Client class
|
||||||
|
class Client {
|
||||||
|
|
||||||
|
// driver code
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// establish a connection by providing host and port
|
||||||
|
// number
|
||||||
|
try (Socket socket = new Socket("localhost", 8080)) {
|
||||||
|
|
||||||
|
// writing to server
|
||||||
|
PrintWriter out = new PrintWriter(
|
||||||
|
socket.getOutputStream(), true);
|
||||||
|
|
||||||
|
// reading from server
|
||||||
|
BufferedReader in = new BufferedReader(new InputStreamReader(
|
||||||
|
socket.getInputStream()));
|
||||||
|
|
||||||
|
// object of scanner class
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String line = null;
|
||||||
|
|
||||||
|
while (!"exit".equalsIgnoreCase(line)) {
|
||||||
|
|
||||||
|
// reading from user
|
||||||
|
line = sc.nextLine();
|
||||||
|
|
||||||
|
// sending the user input to server
|
||||||
|
out.println(line);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
// displaying server reply
|
||||||
|
System.out.println("Server replied "
|
||||||
|
+ in.readLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
// closing the scanner object
|
||||||
|
sc.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,72 @@
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
class Server {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ServerSocket server = null;
|
||||||
|
try {
|
||||||
|
// start server
|
||||||
|
server = new ServerSocket(8080);
|
||||||
|
server.setReuseAddress(true);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
Socket client = server.accept();
|
||||||
|
System.out.println("New client: " + client.getInetAddress().getHostAddress());
|
||||||
|
|
||||||
|
ClientHandler clientSock = new ClientHandler(client);
|
||||||
|
new Thread(clientSock).start();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (server != null) {
|
||||||
|
try {
|
||||||
|
server.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ClientHandler implements Runnable {
|
||||||
|
|
||||||
|
private Socket clientSocket;
|
||||||
|
|
||||||
|
public ClientHandler(Socket socket) {
|
||||||
|
this.clientSocket = socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
PrintWriter out = null;
|
||||||
|
BufferedReader in = null;
|
||||||
|
try {
|
||||||
|
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
||||||
|
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||||
|
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null) {
|
||||||
|
System.out.printf("From the client: %s\n", line);
|
||||||
|
out.println(line);
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (out != null) {
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
if (in != null) {
|
||||||
|
in.close();
|
||||||
|
System.out.println("closing socket " + clientSocket.getInetAddress().getHostAddress());
|
||||||
|
clientSocket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue