Skip to content

Commit 64f8b98

Browse files
committed
Sockets -- EchoSockets
1 parent 9319a39 commit 64f8b98

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

README.md

712 Bytes

ChromeCast-Multicast

Conexión por sockets

Útil para saber como es la programación por sockets en Java. Para compilar y ejecutar:

#compilar los .java
javac ./Sockets/*.java

#Init server side: host: localhost, port_number: 7
java ./Sockets/EchoServer 7

#Init client side: host: localhost, port_number: 7
java ./Sockets/EchoServer localhost 7

Sockets/EchoClient.class

2.67 KB
Binary file not shown.

Sockets/EchoClient.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
import java.io.*;
3+
import java.net.*;
4+
5+
public class EchoClient {
6+
public static void main(String[] args) throws IOException {
7+
8+
if (args.length != 2) {
9+
System.err.println(
10+
"Usage: java EchoClient <host_name> <port_number>");
11+
System.exit(1);
12+
}
13+
14+
String hostName = args[0];
15+
int portNumber = Integer.parseInt(args[1]);
16+
17+
try (
18+
Socket echoSocket = new Socket(hostName, portNumber);
19+
PrintWriter out =
20+
new PrintWriter(echoSocket.getOutputStream(), true);
21+
BufferedReader in =
22+
new BufferedReader(
23+
new InputStreamReader(echoSocket.getInputStream()));
24+
BufferedReader stdIn =
25+
new BufferedReader(
26+
new InputStreamReader(System.in))
27+
) {
28+
String userInput;
29+
while ((userInput = stdIn.readLine()) != null) {
30+
out.println(userInput);
31+
System.out.println("echo: " + in.readLine());
32+
}
33+
} catch (UnknownHostException e) {
34+
System.err.println("Don't know about host " + hostName);
35+
System.exit(1);
36+
} catch (IOException e) {
37+
System.err.println("Couldn't get I/O for the connection to " +
38+
hostName);
39+
System.exit(1);
40+
}
41+
}
42+
}

Sockets/EchoServer.class

2.68 KB
Binary file not shown.

Sockets/EchoServer.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import java.net.*;
3+
import java.io.*;
4+
5+
public class EchoServer {
6+
public static void main(String[] args) throws IOException {
7+
8+
if (args.length != 1) {
9+
System.err.println("Usage: java EchoServer <port_number>");
10+
System.exit(1);
11+
}
12+
13+
int portNumber = Integer.parseInt(args[0]);
14+
15+
try (
16+
ServerSocket serverSocket =
17+
new ServerSocket(Integer.parseInt(args[0]));
18+
Socket clientSocket = serverSocket.accept();
19+
PrintWriter out =
20+
new PrintWriter(clientSocket.getOutputStream(), true);
21+
BufferedReader in = new BufferedReader(
22+
new InputStreamReader(clientSocket.getInputStream()));
23+
) {
24+
String inputLine;
25+
while ((inputLine = in.readLine()) != null) {
26+
System.out.println(inputLine);
27+
if(inputLine == "Hola"){
28+
29+
inputLine.replace('o', 'a');
30+
}
31+
out.println(inputLine);
32+
}
33+
} catch (IOException e) {
34+
System.out.println("Exception caught when trying to listen on port "
35+
+ portNumber + " or listening for a connection");
36+
System.out.println(e.getMessage());
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)