Buscar

Socket em java progama exemplo

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

Socket/build/classes/.netbeans_automatic_build
Socket/build/classes/.netbeans_update_resources
Socket/build/classes/javaapplication6/ClienteSocket.class
package javaapplication6;
public synchronized class ClienteSocket extends Thread {
 private static Object jTextField1;
 private java.net.Socket conexao;
 public void ClienteSocket(java.net.Socket);
 public static void main(String[]);
 public void run();
}
Socket/build/classes/javaapplication6/ServidorSocket.class
package javaapplication6;
public synchronized class ServidorSocket extends Thread {
 private static java.util.Map MAP_CLIENTES;
 private java.net.Socket conexao;
 private String nomeCliente;
 private static java.util.List LISTA_DE_NOMES;
 public void ServidorSocket(java.net.Socket);
 public boolean armazena(String);
 public void remove(String);
 public static void main(String[]);
 public void run();
 public void send(java.io.PrintStream, String, String[]);
 static void <clinit>();
}
Socket/build.xml
 
 Builds, tests, and runs the project JavaApplication6.
 
 
Socket/manifest.mf
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Socket/nbproject/build-impl.xml
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set src.dir
 Must set test.src.dir
 Must set build.dir
 Must set dist.dir
 Must set build.classes.dir
 Must set dist.javadoc.dir
 Must set build.test.classes.dir
 Must set build.test.results.dir
 Must set build.classes.excludes
 Must set dist.jar
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set javac.includes
No tests executed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set JVM to use for profiling in profiler.info.jvm
 Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set javac.includes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 To run this application from the command line without Ant, try:
 
 
 
 
 
 
 java -cp "${run.classpath.with.dist.jar}" ${main.class}
To run this application from the command line without Ant, try:
 
 java -jar "${dist.jar.resolved}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set debug.class
 
 
 
 
 Must select one file in the IDE or set debug.class
 
 
 
 
 Must set fix.includes
 
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set profile.class
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 
 Must select some files in the IDE or set test.includes
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set javac.includes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Some tests failed; see details above.
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set test.includes
 
 
 
 Some tests failed; see details above.
 
 
 
 Must select some files in the IDE or set test.class
 Must select some method in the IDE or set test.method
 
 
 
 Some tests failed; see details above.
 
 
 
 
 Must select one file in the IDE or set test.class
 
 
 
 Must select one file in the IDE or set test.class
 Must select some method in the IDE or set test.method
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Socket/nbproject/genfiles.properties
build.xml.data.CRC32=0d1309e4
build.xml.script.CRC32=92f86e22
build.xml.stylesheet.CRC32=28e38971@1.53.1.46
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=0d1309e4
nbproject/build-impl.xml.script.CRC32=6abe1915
nbproject/build-impl.xml.stylesheet.CRC32=6ddba6b6@1.53.1.46
Socket/nbproject/private/private.properties
compile.on.save=true
user.properties.file=C:\\Users\\comp1\\AppData\\Roaming\\NetBeans\\7.2.1\\build.properties
Socket/nbproject/project.properties
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
 ${run.classpath}
debug.test.classpath=\
 ${run.test.classpath}
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/JavaApplication6.jar
dist.javadoc.dir=${dist.dir}/javadoc
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.processorpath=\
 ${javac.classpath}
javac.source=1.7
javac.target=1.7
javac.test.classpath=\
 ${javac.classpath}:\
 ${build.classes.dir}
javac.test.processorpath=\
 ${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=javaapplication6.ServidorSocket
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
 ${javac.classpath}:\
 ${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.test.classpath=\
 ${javac.test.classpath}:\
 ${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test
Socket/nbproject/project.xml
 
 org.netbeans.modules.java.j2seproject
 
 
 JavaApplication6
 
 
 
 
 
 
 
 
Socket/src/javaapplication6/ClienteSocket.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * alairton, fernando, charlan
 */ 
package javaapplication6;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.JTextField;
public class ClienteSocket extends Thread {
 private static Object jTextField1;
 private Socket conexao;
 public ClienteSocket(Socket socket) {
 this.conexao = socket;
 }
 public static void main(String args[])
 {
 try {
 Socket socket = new Socket("127.0.0.1", 5555);
 PrintStream saida = new PrintStream(socket.getOutputStream());
 BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("PARA ENCERRAR NÃO DIGITE NADA E TECLE ENTER\n");
 System.out.print("Digite seu nome: ");
 String meuNome = teclado.readLine();
 saida.println(meuNome.toUpperCase());
 Thread thread = new ClienteSocket(socket);
 thread.start();
 String msg;
 while (true)
 {
 System.out.print("Mensagem > ");
 msg = teclado.readLine();
 saida.println(msg);
 }
 } catch (IOException e) {
 System.out.println("Falha na Conexao... .. ." + " IOException: " + e);
 }
 }
 public void run()
 {
 try {
 BufferedReader entrada = new BufferedReader(new InputStreamReader(this.conexao.getInputStream()));
 String msg;
 while (true)
 {
 msg = entrada.readLine();
 if (msg == null) {
 System.out.println("Conexão encerrada!");
 System.exit(0);
 }
 System.out.println();
 System.out.println(msg);
 System.out.print("Responder > ");
 }
 } catch (IOException e) {
 System.out.println("Ocorreu uma Falha na conexão... .. ." + " IOException: " + e);
 }
 }
}
Socket/src/javaapplication6/ServidorSocket.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * alairton, fernando, charlan
 */
package javaapplication6;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
public class ServidorSocket extends Thread {
 private static Map<String, PrintStream> MAP_CLIENTES;
 private Socket conexao;
 private String nomeCliente;
 private static List<String> LISTA_DE_NOMES = new ArrayList<String>();
 public ServidorSocket(Socket socket) {
 this.conexao = socket;
 }
 public boolean armazena(String newName) {
 for (int i = 0; i < LISTA_DE_NOMES.size(); i++) {
 if (LISTA_DE_NOMES.get(i).equals(newName))
 return true;
 }
 LISTA_DE_NOMES.add(newName);
 return false;
 }
 public void remove(String oldName) {
 for (int i = 0; i < LISTA_DE_NOMES.size(); i++) {
 if (LISTA_DE_NOMES.get(i).equals(oldName))
 LISTA_DE_NOMES.remove(oldName);
 }
 }
 public static void main(String args[]) {
 MAP_CLIENTES = new HashMap<String, PrintStream>();
 try {
 ServerSocket server = new ServerSocket(5555);
 System.out.println("ServidorSocket rodando na porta 5555");
 while (true) {
 Socket conexao = server.accept();
 Thread t = new ServidorSocket(conexao);
 t.start();
 }
 } catch (IOException e) {
 System.out.println("IOException: " + e);
 }
 }
 public void run() {
 try {
 BufferedReader entrada = new BufferedReader(new InputStreamReader(this.conexao.getInputStream()));
 PrintStream saida = new PrintStream(this.conexao.getOutputStream());
 this.nomeCliente = entrada.readLine();
 if (armazena(this.nomeCliente)) {
 saida.println("Este nome ja existe! Conecte novamente com outro Nome.");
 this.conexao.close();
 return;
 } else {
 System.out.println(this.nomeCliente + " : Conectado ao Servidor!");
 saida.println("Conectados: " + LISTA_DE_NOMES.toString());
 }
 if (this.nomeCliente == null) {
 return;
 }
 MAP_CLIENTES.put(this.nomeCliente, saida);
 String[] msg = entrada.readLine().split(":");
 while (msg != null && !(msg[0].trim().equals(""))) {
 send(saida, " escreveu: ", msg);
 msg = entrada.readLine().split(":");
 }
 System.out.println(this.nomeCliente + " saiu do bate-papo!");
 String[] out = {" do bate-papo!"};
 send(saida, " saiu", out);
 remove(this.nomeCliente);
 MAP_CLIENTES.remove(this.nomeCliente);
 this.conexao.close();
 } catch (IOException e) {
 System.out.println("Falha na Conexao... .. ." + " IOException: " + e);
 }
 }
 public void send(PrintStream saida, String acao, String[] msg) {
 out:
 for (Map.Entry<String, PrintStream> cliente : MAP_CLIENTES.entrySet()) {
 PrintStream chat = cliente.getValue();
 if (chat != saida) {
 if (msg.length == 1) {
 chat.println(this.nomeCliente + acao + msg[0]);
 } else {
 if (msg[1].equalsIgnoreCase(cliente.getKey())) {
 chat.println(this.nomeCliente + acao + msg[0]);
 break out;
 }
 }
 }
 }
 }
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Continue navegando