Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I love threads #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions Chat/source/echo/EchoClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,30 @@ public class EchoClient {
public static void main(String[] args) throws IOException {


String hostName = "localhost";
String serverName = "localhost";
//int portNumber = Integer.parseInt(args[1]);
int portNumber = 1234;

try (
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =
new BufferedReader(
new InputStreamReader(System.in))
) {
Socket echoSocket = new Socket(serverName, portNumber);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in))
)
{
System.out.println("server says: " + in.readLine());
String userInput;
while ((userInput = stdIn.readLine()) != "\n") {
while ((userInput = stdIn.readLine()) != "") {
out.println(userInput);
System.out.println("server says: " + in.readLine());
}

} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.err.println("Don't know about host " + serverName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
serverName);
System.exit(1);
}
}
Expand Down
34 changes: 30 additions & 4 deletions Chat/source/sinGUI/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

/*
*
*
* https://stackoverflow.com/questions/4981214/change-value-of-a-variable-x-in-main-method-through-running-thread
*
*
*/
public class Server extends Thread {
ArrayList<Socket> clientes = new ArrayList<Socket>(5);
public static void main(String [] args) throws IOException {
Expand All @@ -19,19 +25,39 @@ public static void main(String [] args) throws IOException {
public void run() {
//Socket nuevoCliente= server.accept();
}

public Server() throws IOException {

ArrayList<Socket> clientes = new ArrayList<Socket>(5);
int puertoEscuchaServer = 1234;

System.out.println("SERVER: server on, listening on " + puertoEscuchaServer);
ServerSocket server = new ServerSocket(puertoEscuchaServer);
Socket nuevoCliente;
System.out.println("SERVER: awaiting connection on " + server.getLocalPort());

Thread aceptarNuevoCliente = new Thread(){
@Override
public void run(){
try {
while (true){
System.out.println("esperando");
clientes.add( server.accept() );
System.out.println("aceptado!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
};

aceptarNuevoCliente.start();

/*
while (true) {
nuevoCliente = server.accept();
clientes.add( nuevoCliente );
System.out.println("se conect� " + nuevoCliente.getLocalSocketAddress() + " a trav�s de " + nuevoCliente.getPort());
}

*/
//server.close();

/*
Expand Down
43 changes: 36 additions & 7 deletions Chat/source/threaded/chat/Cliente.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
package threaded.chat;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Cliente {
Socket socket;

public Cliente(){
socket = new Socket();

public class Cliente extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cliente frame = new Cliente();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Cliente() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
}

}
42 changes: 29 additions & 13 deletions Chat/source/threaded/chat/Participante.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
package threaded.chat;

import java.net.InetAddress;
import java.net.Socket;

public class Participante {

private String name;
private String IP;
private InetAddress IP;
private Socket socket;

public String getName(){
return name;
/**
* @param socket the socket to set
*/
public void setSocket(Socket socket) {
this.socket = socket;
}

public String toString() {
return name;
/**
* @return the socket
*/
public Socket getSocket() {
return socket;
}

public String getName(){
return name;
}

public String showYourself() {
return "Participante [name=" + name + ", IP=" + IP + ", socket=" + socket + ", is closed:" + socket.isClosed() +
", isconnected: " + socket.isConnected() + "]";
}

public Participante(String n) {
name = n;
}





public void setIP(String ip) {
IP = ip;
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return name;
}

public String getIP() {
return IP;
public void setIP(InetAddress inetAddress) {
IP = inetAddress;
}

}
43 changes: 0 additions & 43 deletions Chat/source/threaded/chat/ServerListener.java

This file was deleted.

Loading