Note that there are some explanatory texts on larger screens.

plurals
  1. POClient-Server connection
    text
    copied!<p>I have a java program that will connect the client to the server. This includes making a file directory once the client had triggered the server through sending a message. For example: Once the server is running already, the client will then connect and will send a msg i.e "Your message: Lady", the server will receive a message like "Request to create a Directory named: Lady", after this a directory will be created named Lady.</p> <p>But the problem is this connection is only for one-to-one. Like only one client can connect to the server...</p> <p>This is the sample code:</p> <pre><code>/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package today._; import java.io.*; import java.net.*; import java.text.*; import java.util.*; public class myServer { protected static final int PORT_NUMBER = 55555; public static void main(String args[]) { try { ServerSocket servsock = new ServerSocket(PORT_NUMBER); System.out.println("Server running..."); while (true) { Socket sock = servsock.accept(); System.out.println("Connection from: " + sock.getInetAddress()); Scanner in = new Scanner(sock.getInputStream()); PrintWriter out = new PrintWriter(sock.getOutputStream()); String request = ""; while (in.hasNext()) { request = in.next(); System.out.println("Request to Create Directory named: " + request); if(request.toUpperCase().equals("TIME")) { try { File file = new File("C:\\" + request); if (!file.exists()) { if (file.mkdir()) { System.out.println("Directory is created!"); } else { System.out.println("Failed to create directory!"); } } } catch (Exception e) { System.out.println(e); } out.println(getTime()); out.flush(); } else { out.println("Invalid Request..."); out.flush(); } } } } catch (Exception e) { System.out.println(e.toString()); } } protected static String getTime() { DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); Date date = new Date(); return (dateFormat.format(date)); } } </code></pre> <hr> <pre><code>package today._; import java.io.*; import java.net.*; import java.util.*; public class myClient { protected static final String HOST = "localhost"; protected static final int PORT = 55555; protected static Socket sock; public static void main(String args[]) { try { sock = new Socket(HOST,PORT); System.out.println("Connected to " + HOST + " on port " + PORT); Scanner response = new Scanner(sock.getInputStream()); PrintWriter request = new PrintWriter(sock.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String txt = ""; while(!txt.toUpperCase().equals("EXIT")) { System.out.print("Your message:"); txt = in.readLine(); request.println(txt); request.flush(); System.out.println(response.next()); } request.close(); response.close(); in.close(); sock.close(); } catch(IOException e) { System.out.println(e.toString()); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload