Last active
April 14, 2024 21:58
-
-
Save Anass-ABEA/26104e1a721cc2be793912abdb51ec63 to your computer and use it in GitHub Desktop.
Java Sockets #2 : Simplified Client/Server Application - Multiple connections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.sockets.simplified; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.Socket; | |
import java.util.Scanner; | |
public class Client { | |
private Socket socket; | |
private DataOutputStream out; | |
private Scanner in; | |
public Client(){ | |
try{ | |
socket = new Socket("127.0.0.1", Server.PORT); | |
out = new DataOutputStream(socket.getOutputStream()); | |
in = new Scanner(System.in); | |
writeMessages(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void writeMessages() throws IOException { | |
String line = ""; | |
while(!line.equals(Server.STOP_STRING)){ | |
line = in.nextLine(); | |
out.writeUTF(line); | |
} | |
close(); | |
} | |
private void close() throws IOException { | |
socket.close(); | |
out.close(); | |
in.close(); | |
} | |
public static void main(String[] args) { | |
new Client(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.sockets.simplified; | |
import java.io.BufferedInputStream; | |
import java.io.DataInputStream; | |
import java.io.IOException; | |
import java.net.Socket; | |
public class ConnectedClient { | |
private Socket clientSocket; | |
private DataInputStream in; | |
private int id; | |
public ConnectedClient(Socket clientSocket, int id) { | |
this.clientSocket = clientSocket; | |
this.id = id; | |
try { | |
System.out.println("Client "+id+ ": Client Connected"); | |
this.in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream())); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void readMessages(){ | |
String line = ""; | |
while(!line.equals(Server.STOP_STRING)){ | |
try { | |
line = in.readUTF(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Client "+id+ ": "+ line); | |
} | |
System.out.println("Client "+id+ ": Client Disconnected"); | |
} | |
public void close(){ | |
try{ | |
clientSocket.close(); | |
in.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.sockets.simplified; | |
import java.io.BufferedInputStream; | |
import java.io.DataInputStream; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class Server { | |
private ServerSocket server; | |
public static final int PORT = 3030; | |
public static final String STOP_STRING = "##"; | |
private int index = 0; | |
public Server(){ | |
try{ | |
server = new ServerSocket(PORT); | |
while(true) iniConnections(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void iniConnections() throws IOException { | |
Socket clientSocket = server.accept(); | |
if(clientSocket.isConnected()) | |
new Thread(()->{ | |
index++; | |
ConnectedClient client = new ConnectedClient(clientSocket,index); | |
client.readMessages(); | |
client.close(); | |
}).start(); | |
} | |
public static void main(String[] args) { | |
new Server(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if I want to Close the server manually
There STOP_STRING should be added to close ther server OR Stop the Running Server