This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
java:sockets [2015/03/24 09:46] gthanos |
java:sockets [2016/02/26 11:15] (current) |
||
|---|---|---|---|
| Line 66: | Line 66: | ||
| try ( | try ( | ||
| Socket echoSocket = new Socket(hostName, portNumber); | Socket echoSocket = new Socket(hostName, portNumber); | ||
| - | PrintWriter out = | + | PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true); |
| - | new PrintWriter(echoSocket.getOutputStream(), true); | + | BufferedReader in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream() )); |
| - | BufferedReader in = | + | BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); |
| - | new BufferedReader( | + | |
| - | new InputStreamReader(echoSocket.getInputStream())); | + | |
| - | BufferedReader stdIn = | + | |
| - | new BufferedReader( | + | |
| - | new InputStreamReader(System.in)) | + | |
| ) { | ) { | ||
| String userInput; | String userInput; | ||
| while ((userInput = stdIn.readLine()) != null) { | while ((userInput = stdIn.readLine()) != null) { | ||
| out.println(userInput); | out.println(userInput); | ||
| - | while(in.ready()) { | + | System.out.println("echoed : "+in.readLine()); |
| - | System.out.println(in.readLine()); | + | |
| - | } | + | |
| } | } | ||
| } catch (UnknownHostException e) { | } catch (UnknownHostException e) { | ||
| Line 94: | Line 87: | ||
| </code> | </code> | ||
| + | Για να εκτελέσετε τα δύο προγράμματα θα πρέπει να εκκινήσετε πρώτα το //EchoServer// δηλώνοντας το port στο οποίο θα ακούει (π.χ. ''java EchoServer 8888''). Στην συνέχεια εκκινείτε το //EchoClient// ως εξής ''java EchoClient localhost 8888''. Η σύνδεση μέσω sockets δεν υλοποιεί κάποιο πρωτόκολλο επικοινωνίας σε επίπεδο application μεταξύ των δύο άκρων (π.χ. http, ftp). | ||
| + | |||
| + | Δύο παραλλαγές των παρακάτω προγραμμάτων είναι η παρακάτω. Στο πρόγραμμα //EchoClient.java// πρώτα δημιουργείται το socket και μετά γίνεται connect σε συγκεκριμένη διεύθυνση (hostname, portnumber). Αντίστοιχα στο πρόγραμμα //EchoServer// πρώτα δημιουργείται το socket και στη συνέχεια γίνεται bind το server socket σε συγκεκριμένη διεύθυνση ("localhost", portnumber). | ||
| + | |||
| + | <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("Usage: java EchoServer <port number>"); | ||
| + | System.exit(1); | ||
| + | } | ||
| + | | ||
| + | int portNumber = Integer.parseInt(args[0]); | ||
| + | | ||
| + | try ( | ||
| + | ServerSocket serverSocket = new ServerSocket(); | ||
| + | ) { | ||
| + | serverSocket.bind(new InetSocketAddress("localhost", Integer.parseInt(args[0]) ) ); | ||
| + | try ( | ||
| + | Socket clientSocket = serverSocket.accept(); | ||
| + | PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); | ||
| + | BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); | ||
| + | ) { | ||
| + | // no catch here | ||
| + | System.out.println("Connected to "+clientSocket.getInetAddress().toString()+":"+clientSocket.getPort()); | ||
| + | String inputLine; | ||
| + | while ((inputLine = in.readLine()) != null) { | ||
| + | out.println(inputLine); | ||
| + | System.out.println("echoing: "+inputLine); | ||
| + | } | ||
| + | } catch (IOException e) { | ||
| + | System.out.println("Exception caught when trying to listen on port " | ||
| + | + portNumber + " or listening for a connection"); | ||
| + | System.out.println(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | <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( | ||
| + | "Usage: java EchoClient <host name> <port number>"); | ||
| + | System.exit(1); | ||
| + | } | ||
| + | |||
| + | String hostName = args[0]; | ||
| + | int portNumber = Integer.parseInt(args[1]); | ||
| + | InetSocketAddress socketAddress = new InetSocketAddress(hostName, portNumber); | ||
| + | |||
| + | try ( | ||
| + | Socket echoSocket = new Socket(); | ||
| + | BufferedReader stdIn = | ||
| + | new BufferedReader( | ||
| + | new InputStreamReader(System.in)); | ||
| + | ) { | ||
| + | echoSocket.connect(socketAddress); | ||
| + | System.out.println("Connected to "+echoSocket.getInetAddress().toString()+":"+echoSocket.getPort()); | ||
| + | try ( | ||
| + | PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true); | ||
| + | BufferedReader in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream() )); | ||
| + | ) { | ||
| + | String userInput; | ||
| + | while ((userInput = stdIn.readLine()) != null) { | ||
| + | out.println(userInput); | ||
| + | System.out.println("echoed: "+in.readLine()); | ||
| + | } | ||
| + | } | ||
| + | // no catch | ||
| + | } catch (UnknownHostException e) { | ||
| + | System.err.println("Don't know about host " + hostName); | ||
| + | System.exit(1); | ||
| + | } catch (IOException e) { | ||
| + | System.err.println("Couldn't get I/O for the connection to " + | ||
| + | hostName); | ||
| + | System.exit(1); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </code> | ||