Problema com socket e java me

0 respostas
M
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.util.Scanner;

class TCPServer {
    public static PrintWriter envia;
    public static Socket socketsClientes[] = new Socket[100];
    public static int cont = 0;
  InputStream recebe = null;
    OutputStream envia2=null;
    DataOutputStream dataOutputStream = null; 
    private char escolha;
    int jcliente1=0, jcliente2=0;
public static void tratarRecebimento(String protocolo) {
    System.out.println("recebi protocolo: "+protocolo);
    // System.out.println("recebi protocolo: "+estado);
    }
    public static class AtendeCliente extends Thread {
        private Socket sock;
        private int id;
        private BufferedReader recebe = null;
        String entrada = null;
        public  AtendeCliente(Socket s, int c) {
            sock = s;
            id = c;
        }
        public void run() {
            do {
                try {
                    recebe = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                    entrada = recebe.readLine();
                    for(int i=0;i<cont;i++)
                    {
                      envia = new PrintWriter(socketsClientes[i].getOutputStream(), true);  
                     // envia.println("C " + id + ": " + entrada );
                    }
                } catch (IOException e) {
                    System.err.println("erro de comunicação.");
                }
            } while (!entrada.equals("fim"));
        }
    }

    public static void main(String argv[]) throws Exception 
    {
           java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TCPServer();
            }
        });
         envia = null;
         String entrada = null; 
        BufferedReader recebe = null;
             ServerSocket socketServidor = null;
        Socket socketCliente = null;
        try {
            socketServidor = new ServerSocket(6789);
            System.out.println("SERVIDOR TCP online!\n");
            System.out.println("Aguardando o cliente...\n");
            socketCliente = socketServidor.accept();
            envia = new PrintWriter(socketCliente.getOutputStream(), true);
            recebe = new BufferedReader(new InputStreamReader(socketCliente.getInputStream()));
        } catch (IOException e) {
            System.err.println("Erro ao aceitar conexcoes");
            System.exit(1);
        }
        do {
            try {
                entrada = recebe.readLine();
                String protocolo = entrada.substring(0);
                tratarRecebimento(protocolo);
            } catch (IOException e) {
                System.err.println("erro de comunicacao.");
            }
        } while (!entrada.equals("fim"));
        /*try {
            recebe.close();
            socketCliente.close();
            socketServidor.close();
        } catch (IOException e) {
            System.err.println("erro de comunicacao.");
        }*/}}
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.io.StreamConnection;
public class Teclas extends Canvas {
    String keyText = null;    // Key code text 
    int[] tabuleiro = new int[9];
    int[] resultado = new int[9];
    int jcliente1=0, jcliente2=0;
    char escolha=' ';
    boolean ocupado = false;
    boolean ganhou = false;
    boolean perdeu = false;
    boolean ninguem = false;
    OutputStream os=null;
    InputStream is =null;
    SocketConnection sc =null;
     Random rd = new Random();
     StreamConnection streamConnection = null; 
     OutputStream outputStream = null;
    int cont = 1;
    int jogadas = 0;
    InputStream recebe = null;
    DataInputStream dataInputStream = null;
    DataOutputStream envia2 = null; 
    public Teclas(){
        for (int i = 0; i <= 8; i++) {
            tabuleiro[i] = 0;
            resultado[i] = 0;
        }
    }
    public void conecta(String protocolo) throws IOException{
        System.out.println("#####"+protocolo);
              sc = (SocketConnection)  
                    Connector.open("socket://localhost:6789");
              sc.setSocketOption(SocketConnection.KEEPALIVE, 5);
            is = sc.openInputStream();  
    os = sc.openOutputStream(); 
    os.write("$$$$$$$".getBytes());
        }
    public void enviar(String protocolo) throws IOException {
        
        
        //is = sc.openInputStream();  
  //  os = sc.openOutputStream(); 
  //  os.write(protocolo.getBytes());   
    /*}  
    is.close();  
    os.close();  
    sc.close();   */
    }
    public void paint(Graphics g) {
        linhas(g);
        if (tabuleiro[Integer.parseInt(keyText)-1] == 0) {
            if (keyText.equals("1")) {                tabuleiro[0] = 1;              }
            if (keyText.equals("2")) {                tabuleiro[1] = 1;                System.out.println("*************");          }
            if (keyText.equals("3")) {                tabuleiro[2] = 1;            }
            if (keyText.equals("4")) {                tabuleiro[3] = 1;            }
            if (keyText.equals("5")) {                tabuleiro[4] = 1;            }          
            if (keyText.equals("6")) {                tabuleiro[5] = 1;            }
            if (keyText.equals("7")) {                tabuleiro[6] = 1;            }
            if (keyText.equals("8")) {                tabuleiro[7] = 1;            }
            if (keyText.equals("9")) {                tabuleiro[8] = 1;          }
            if (cont <= 4) {
                try {
                  conecta("enviar");    
              } catch (IOException ex) {
                    System.out.println("erro aki#########");
                }
                cont++; 
                System.out.println("chegou aki");
              }
        }
        for (int p = 0; p <= 8; p++) {
            g.setColor(211, 211, 0);
            if (tabuleiro[0] == 1) {                g.fillRect(5, 5, 50, 50);            }
            if (tabuleiro[1] == 1) {                g.fillRect(65, 5, 50, 50);            }
            if (tabuleiro[2] == 1) {                g.fillRect(125, 5, 50, 50);            }
            if (tabuleiro[3] == 1) {                g.fillRect(5, 65, 50, 50);            }
            if (tabuleiro[4] == 1) {                g.fillRect(65, 65, 50, 50);            }
            if (tabuleiro[5] == 1) {                g.fillRect(125, 65, 50, 50);            }
            if (tabuleiro[6] == 1) {                g.fillRect(5, 125, 50, 50);            }
            if (tabuleiro[7] == 1) {                g.fillRect(65, 125, 50, 50);            }
            if (tabuleiro[8] == 1) {                g.fillRect(125, 125, 50, 50);            }

            g.setColor(10, 10, 255);
            if (tabuleiro[0] == 2) {                g.fillArc(5, 5, 49, 49, 0, 360);            } //x,y,w,h, angulo inicio, circuferencia cheia}
            if (tabuleiro[1] == 2) {                g.fillArc(65, 5, 49, 49, 0, 360);            }
            if (tabuleiro[2] == 2) {                g.fillArc(125, 5, 49, 49, 0, 360);            }
            if (tabuleiro[3] == 2) {                g.fillArc(5, 65, 49, 49, 0, 360);            }
            if (tabuleiro[4] == 2) {                g.fillArc(65, 65, 49, 49, 0, 360);            }
            if (tabuleiro[5] == 2) {                g.fillArc(125, 65, 49, 49, 0, 360);            }
            if (tabuleiro[6] == 2) {                g.fillArc(5, 125, 49, 49, 0, 360);            }
            if (tabuleiro[7] == 2) {                g.fillArc(65, 125, 49, 49, 0, 360);            }
            if (tabuleiro[8] == 2) {                g.fillArc(125, 125, 50, 50, 0, 360);            }
            
            if ((tabuleiro[0] == 1) && (tabuleiro[1] == 1) && (tabuleiro[2] == 1)) {
                ganhou = true;                resultado[0] = 1;                resultado[1] = 1;                resultado[2] = 1;
            }
            if ((tabuleiro[3] == 1) && (tabuleiro[4] == 1) && (tabuleiro[5] == 1)) {
                ganhou = true;                resultado[3] = 1;                resultado[4] = 1;                resultado[5] = 1;
            }
            if ((tabuleiro[6] == 1) && (tabuleiro[7] == 1) && (tabuleiro[8] == 1)) {
                ganhou = true;                resultado[6] = 1;                resultado[7] = 1;                resultado[8] = 1;
            }
            if ((tabuleiro[0] == 1) && (tabuleiro[3] == 1) && (tabuleiro[6] == 1)) {
                ganhou = true;                resultado[0] = 1;                resultado[3] = 1;                resultado[6] = 1;
            }
            if ((tabuleiro[1] == 1) && (tabuleiro[4] == 1) && (tabuleiro[7] == 1)) {
                ganhou = true;                resultado[1] = 1;                resultado[4] = 1;                resultado[7] = 1;
            }
            if ((tabuleiro[2] == 1) && (tabuleiro[5] == 1) && (tabuleiro[8] == 1)) {
                ganhou = true;                resultado[2] = 1;                resultado[2] = 1;                resultado[2] = 1;
            }
            if ((tabuleiro[0] == 1) && (tabuleiro[4] == 1) && (tabuleiro[8] == 1)) {
                ganhou = true;                resultado[0] = 1;                resultado[4] = 1;                resultado[8] = 1;
            }
            if ((tabuleiro[2] == 1) && (tabuleiro[4] == 1) && (tabuleiro[6] == 1)) {
                ganhou = true;                resultado[2] = 1;                resultado[4] = 1;                resultado[6] = 1;
            }
            //regras perder
            if ((tabuleiro[0] == 2) && (tabuleiro[1] == 2) && (tabuleiro[2] == 2)) {
                perdeu = true;                resultado[0] = 2;                resultado[1] = 2;                resultado[2] = 2;
            }
            if ((tabuleiro[3] == 2) && (tabuleiro[4] == 2) && (tabuleiro[5] == 2)) {
                perdeu = true;                resultado[3] = 2;                resultado[4] = 2;                resultado[5] = 2;
            }
            if ((tabuleiro[6] == 2) && (tabuleiro[7] == 2) && (tabuleiro[8] == 2)) {
                perdeu = true;                resultado[6] = 2;                resultado[7] = 2;                resultado[8] = 2;
            }
            if ((tabuleiro[0] == 2) && (tabuleiro[3] == 2) && (tabuleiro[6] == 2)) {
                perdeu = true;                resultado[0] = 2;                resultado[3] = 2;                resultado[6] = 2;
            }
            if ((tabuleiro[1] == 2) && (tabuleiro[4] == 2) && (tabuleiro[7] == 2)) {
                perdeu = true;                resultado[1] = 2;                resultado[4] = 2;                resultado[7] = 2;
            }
            if ((tabuleiro[2] == 2) && (tabuleiro[5] == 2) && (tabuleiro[8] == 2)) {
                perdeu = true;                resultado[2] = 2;                resultado[5] = 2;                resultado[8] = 2;
            }
            if ((tabuleiro[0] == 2) && (tabuleiro[4] == 2) && (tabuleiro[8] == 2)) {
                perdeu = true;                resultado[0] = 2;                resultado[4] = 2;                resultado[8] = 2;
            }
            if ((tabuleiro[2] == 2) && (tabuleiro[4] == 2) && (tabuleiro[6] == 2)) {
                perdeu = true;                resultado[2] = 2;                resultado[4] = 2;                resultado[6] = 2;
            }
            if ((ganhou == true) || (perdeu == true)) {
                LimpaTela(g);
                ImpResultado(g);
                if (ganhou == true) {
                    g.setColor(0, 0, 0);
                    g.drawString("Você Ganhou Parabéns!", getWidth() / 2, getHeight() / 2, Graphics.TOP | Graphics.HCENTER);
                }
                if (perdeu == true) {
                    g.setColor(0, 0, 0);
                    g.drawString("Você Perdeu que Pena!", getWidth() / 2, getHeight() / 2, Graphics.TOP | Graphics.HCENTER);
                }
            }
            ninguem = false;
            jogadas = 0;
            for (int i = 0; i <= 8; i++) {
                if ((tabuleiro[i] == 1) || (tabuleiro[i] == 2)) {
                    jogadas++;
                }
            }
            if (jogadas == 8) {
                ninguem = true;
            }
            if ((ganhou == false) && (perdeu == false) && (ninguem == true)) {
                //resultado
                LimpaTela(g);
                g.setColor(0, 0, 0);
                g.drawString("Ninguém ganhou,deu empate!", getWidth() / 2, getHeight() / 2, Graphics.TOP | Graphics.HCENTER);
            }
        }
    }
    public void keyPressed(int keyCode) {
        keyText = getKeyName(keyCode);
        repaint();
    }
    public void jogacomputador() {
        ocupado = false;
        while (ocupado == false) {
             int num=rd.nextInt(9);
  
      if ((num >= 0) &&(num<=8)){
       if ( tabuleiro[num]==0){
        ocupado =true; 
        tabuleiro[num]=2;}}
                }
            }
    public void ImpResultado(Graphics g) {
        //mostra resultado
        g.setColor(211, 211, 211);
        if (resultado[0] == 1) {
            g.fillRect(5, 5, 50, 50);
        }
        if (resultado[1] == 1) {
            g.fillRect(65, 5, 50, 50);
        }
        if (resultado[2] == 1) {
            g.fillRect(125, 5, 50, 50);
        }
        if (resultado[3] == 1) {
            g.fillRect(5, 65, 50, 50);
        }
        if (resultado[4] == 1) {
            g.fillRect(65, 65, 50, 50);
        }
        if (resultado[5] == 1) {
            g.fillRect(125, 65, 50, 50);
        }
        if (resultado[6] == 1) {
            g.fillRect(5, 125, 50, 50);
        }
        if (resultado[7] == 1) {
            g.fillRect(65, 125, 50, 50);
        }
        if (resultado[8] == 1) {
            g.fillRect(125, 125, 50, 50);
        }


        if (resultado[0] == 2) {
            g.fillArc(5, 5, 49, 49, 0, 360);
        } //x,y,w,h, angulo inicio, circuferencia cheia}
        if (resultado[1] == 2) {
            g.fillArc(65, 5, 49, 49, 0, 360);
        }
        if (resultado[2] == 2) {
            g.fillArc(125, 5, 49, 49, 0, 360);
        }
        if (resultado[3] == 2) {
            g.fillArc(5, 65, 49, 49, 0, 360);
        }
        if (resultado[4] == 2) {
            g.fillArc(65, 65, 49, 49, 0, 360);
        }
        if (resultado[5] == 2) {
            g.fillArc(125, 65, 49, 49, 0, 360);
        }
        if (resultado[6] == 2) {
            g.fillArc(5, 125, 49, 49, 0, 360);
        }
        if (resultado[7] == 2) {
            g.fillArc(65, 125, 49, 49, 0, 360);
        }
        if (resultado[8] == 2) {
            g.fillArc(125, 125, 50, 50, 0, 360);
        }
    }
    public void LimpaTela(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
    public void linhas(Graphics g) {
        //linhas
        g.fillRect(0, 60, 180, 1);
        g.fillRect(0, 120, 180, 1);
        g.fillRect(60, 0, 1, 180);
        g.fillRect(120, 0, 1, 180);
        g.setColor(100, 100, 0);
        g.drawString("1", 30, 25, Graphics.TOP | Graphics.HCENTER);
        g.drawString("2", 90, 25, Graphics.TOP | Graphics.HCENTER);
        g.drawString("3", 150, 25, Graphics.TOP | Graphics.HCENTER);
        g.drawString("4", 30, 85, Graphics.TOP | Graphics.HCENTER);
        g.drawString("5", 90, 85, Graphics.TOP | Graphics.HCENTER);
        g.drawString("6", 150, 85, Graphics.TOP | Graphics.HCENTER);
        g.drawString("7", 30, 140, Graphics.TOP | Graphics.HCENTER);
        g.drawString("8", 90, 140, Graphics.TOP | Graphics.HCENTER);
        g.drawString("9", 150, 140, Graphics.TOP | Graphics.HCENTER);
        }

    

}
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class Principal extends MIDlet {
    Display meuDisplay = Display.getDisplay(this);
    Teclas meuCanvas = new Teclas();
         StreamConnection streamConnection = null; // StreamConnection allows bidirectional communication
         OutputStream outputStream = null;// use OutputStream to send requests
     DataOutputStream dataOutputStream = null;
         InputStream inputStream = null;// use InputStream to receive responses from Web server
     DataInputStream dataInputStream = null;
        String connectString = "socket://localhost:6789"; // specify the connect string
         StringBuffer results;// use a StrignBuffer to store the retrieved page contents
         Display myDisplay = null;
     Form resultScreen;
     OutputStream os=null;
    InputStream is =null;
    SocketConnection sc =null;
     public int keycode;
  
  public Principal() {
        // initializing GUI display
        results = new StringBuffer();
        myDisplay = Display.getDisplay(this);
       // resultScreen = new Form("Page Content:");
    }
 
    public void startApp() {
     meuDisplay.setCurrent(meuCanvas);  
   }
    public void pauseApp() {
    }
 protected void destroyApp(boolean unconditional) {
  
 }   
}

primeiro codigo eh o servidor do jogo, eh um jogo da velha q se conecta ao servidor e envia uma msg com o numero q foi escolhido pelo usuario, eu consigo conectar mas nunca consigo enviar o numero q cliquei no celular de forma alguma, o codigo ta bagunçado ja pq ja tentei de tudo mas nao sei se ele encerra a conexao ou o q acontece se puderem ajudar agradeço

Criado 21 de novembro de 2013
Respostas 0
Participantes 1