java:sockets
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:sockets [2015/03/24 09:14] – gthanos | java:sockets [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 7: | Line 7: | ||
{{ : | {{ : | ||
+ | Το πακέτο java.net της Java παρέχει την κλάση, [[http:// | ||
+ | |||
+ | Δείτε τα παρακάτω προγράμματα πελάτη/ | ||
+ | |||
+ | <code java EchoServer.java> | ||
+ | import java.net.*; | ||
+ | import java.io.*; | ||
+ | |||
+ | public class EchoServer { | ||
+ | public static void main(String[] args) throws IOException { | ||
+ | | ||
+ | if (args.length != 1) { | ||
+ | System.err.println(" | ||
+ | System.exit(1); | ||
+ | } | ||
+ | | ||
+ | int portNumber = Integer.parseInt(args[0]); | ||
+ | | ||
+ | try ( | ||
+ | ServerSocket serverSocket = | ||
+ | new ServerSocket(Integer.parseInt(args[0])); | ||
+ | Socket clientSocket = serverSocket.accept(); | ||
+ | PrintWriter out = | ||
+ | new PrintWriter(clientSocket.getOutputStream(), | ||
+ | BufferedReader in = new BufferedReader( | ||
+ | new InputStreamReader(clientSocket.getInputStream())); | ||
+ | ) { | ||
+ | String inputLine; | ||
+ | while ((inputLine = in.readLine()) != null) { | ||
+ | out.println(inputLine); | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } catch (IOException e) { | ||
+ | System.out.println(" | ||
+ | + portNumber + " or listening for a connection" | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java EchoClient.java> | ||
+ | import java.io.*; | ||
+ | import java.net.*; | ||
+ | |||
+ | public class EchoClient { | ||
+ | public static void main(String[] args) throws IOException { | ||
+ | | ||
+ | if (args.length != 2) { | ||
+ | System.err.println( | ||
+ | " | ||
+ | System.exit(1); | ||
+ | } | ||
+ | |||
+ | String hostName = args[0]; | ||
+ | int portNumber = Integer.parseInt(args[1]); | ||
+ | |||
+ | try ( | ||
+ | Socket echoSocket = new Socket(hostName, | ||
+ | PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), | ||
+ | BufferedReader in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream() )); | ||
+ | BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); | ||
+ | ) { | ||
+ | String userInput; | ||
+ | while ((userInput = stdIn.readLine()) != null) { | ||
+ | out.println(userInput); | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } catch (UnknownHostException e) { | ||
+ | System.err.println(" | ||
+ | System.exit(1); | ||
+ | } catch (IOException e) { | ||
+ | System.err.println(" | ||
+ | hostName); | ||
+ | System.exit(1); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Για να εκτελέσετε τα δύο προγράμματα θα πρέπει να εκκινήσετε πρώτα το // | ||
+ | |||
+ | Δύο παραλλαγές των παρακάτω προγραμμάτων είναι η παρακάτω. Στο πρόγραμμα // | ||
+ | |||
+ | <code java EchoServer.java> | ||
+ | import java.net.*; | ||
+ | import java.io.*; | ||
+ | |||
+ | public class EchoServer { | ||
+ | public static void main(String[] args) throws IOException { | ||
+ | | ||
+ | if (args.length != 1) { | ||
+ | System.err.println(" | ||
+ | System.exit(1); | ||
+ | } | ||
+ | | ||
+ | int portNumber = Integer.parseInt(args[0]); | ||
+ | | ||
+ | try ( | ||
+ | ServerSocket serverSocket = new ServerSocket(); | ||
+ | ) { | ||
+ | serverSocket.bind(new InetSocketAddress(" | ||
+ | try ( | ||
+ | Socket clientSocket = serverSocket.accept(); | ||
+ | PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), | ||
+ | BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); | ||
+ | ) { | ||
+ | // no catch here | ||
+ | System.out.println(" | ||
+ | String inputLine; | ||
+ | while ((inputLine = in.readLine()) != null) { | ||
+ | out.println(inputLine); | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } catch (IOException e) { | ||
+ | System.out.println(" | ||
+ | + portNumber + " or listening for a connection" | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java EchoClient.java> | ||
+ | import java.io.*; | ||
+ | import java.net.*; | ||
+ | |||
+ | public class EchoClient { | ||
+ | public static void main(String[] args) throws IOException { | ||
+ | | ||
+ | if (args.length != 2) { | ||
+ | System.err.println( | ||
+ | " | ||
+ | System.exit(1); | ||
+ | } | ||
+ | |||
+ | String hostName = args[0]; | ||
+ | int portNumber = Integer.parseInt(args[1]); | ||
+ | InetSocketAddress socketAddress = new InetSocketAddress(hostName, | ||
+ | |||
+ | try ( | ||
+ | Socket echoSocket = new Socket(); | ||
+ | BufferedReader stdIn = | ||
+ | new BufferedReader( | ||
+ | new InputStreamReader(System.in)); | ||
+ | ) { | ||
+ | echoSocket.connect(socketAddress); | ||
+ | System.out.println(" | ||
+ | try ( | ||
+ | PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), | ||
+ | BufferedReader in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream() )); | ||
+ | ) { | ||
+ | String userInput; | ||
+ | while ((userInput = stdIn.readLine()) != null) { | ||
+ | out.println(userInput); | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | // no catch | ||
+ | } catch (UnknownHostException e) { | ||
+ | System.err.println(" | ||
+ | System.exit(1); | ||
+ | } catch (IOException e) { | ||
+ | System.err.println(" | ||
+ | hostName); | ||
+ | System.exit(1); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |Προηγούμενο: | ||
java/sockets.1427188442.txt.gz · Last modified: 2015/03/24 09:14 by gthanos