使用java的Android Multi Client-Server

时间:2015-02-28 11:27:49

标签: java android multithreading tcpclient

我和朋友做了一个项目,我们遇到了下一个问题; 程序大多数连接到java中的服务器并发送消息但是没有从服务器获取任何内容,事情是我已经在清单中提供连接到Internet的权限并将此代码用作MainActivity

package Er.net.connectserver;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

private int port=0;
private String IP="";
private cliente LevClit;
private MyActivity clt= new MyActivity();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
}

public void Connect(){
    EditText textoIP = (EditText) findViewById(R.id.InIP);
    EditText textoPort = (EditText) findViewById(R.id.InPort);
    String IP= textoIP.getText().toString();
    int port =Integer.parseInt(textoPort.getText().toString());
    this.port=port;
    this.IP=IP;
}

public void listend(View v){
    if(v.getId()==R.id.BtnConnect){
        try{
            Connect();
            arrancar();
            /*Toast.makeText(getApplicationContext(), "connection apro.", Toast.LENGTH_SHORT).show();
            clt.setCliente(LevClit);
            Intent pantalla2= new Intent(this, MyActivity.class);
            startActivity(pantalla2);*/
        }catch(Exception e ){
            Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_SHORT).show();
            System.out.println(e);
        }
    }
}

public void arrancar(){
    try{
        LevClit= new cliente(IP, port, 3);
        LevClit.start();
        //Toast.makeText(getApplicationContext(), "pass the .start()", Toast.LENGTH_SHORT).show();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "fail in .start()", Toast.LENGTH_SHORT).show();
        System.out.println(e);
    }
}

public cliente getCliente(){
    return LevClit;
}

}

这部分是针对客户端的,我用来与java建立服务器连接。

package Er.net.connectserver;


import java.io.*;
import java.net.*;
import java.util.*;

public class cliente extends Thread{

private Socket socket;
//private DataInputStream Datos_Entrada;
private DataOutputStream Datos_Salida;
private int puerto;
private boolean bandera;
private String ip;
private String mensaje;
private  int id;;

public cliente(String ip, int puerto, int id){
    mensaje="";
    this.ip= ip;
    bandera= true;
    this.puerto= puerto;
    this.id= id;
    System.out.println("cliente inicializado");
}

@Override
public void run(){
    try{
        System.out.println("entra al run");
        socket= new Socket(ip, puerto);
        System.out.println("espera mensaje");
        //Datos_Entrada= new DataInputStream(socket.getInputStream());
        Datos_Salida= new  DataOutputStream(socket.getOutputStream());
        System.out.print("usuario"+ id + "mensaje enviado");
        while (bandera){
            if(mensaje.length()>0)
                Datos_Salida.writeUTF(setMensaje());
        }
        //Datos_Entrada.close();
        Datos_Salida.close();
        socket.close();
    }catch(IOException e){
        System.out.print(e);
    }
}

public void getMensaje(String mensaje){
    this.mensaje=mensaje;
}

public String setMensaje(){
    return mensaje;
}

public void setBandera(boolean bandera){
    this.bandera= bandera;
}

}

这里的事情是,连接不起作用,但在java应用程序中工作的客户端工作完美,但我不知道如何使这个东西在Android中工作,我不知道是不是因为威胁或因为客户端类。

这是我正在使用的服务器,旁边就是服务器的威胁

package servidor;
import java.net.*;
import java.io.*;


public class Servidor {
    //private static final int puerto= 3601;
    private ServerSocket servidor;
    private Socket socket;
    private boolean listening;
    private int ID;
    private int puerto;

    public Servidor() throws IOException{
        listening=true;
        ID=1;
        puerto=5005;
        try{
            servidor = new ServerSocket(puerto);
            System.out.println("esperando cliente");
            while (listening){
                socket= servidor.accept();
                System.out.print("socket de cliente"+ ID +" :" +socket);
                ((MultiServer)new MultiServer(socket, ID )).start();
                ID++;
            }
        }catch(IOException e){
                System.out.println(e.getMessage());
        }   
    }
    public int getPuerto(){
        return puerto;
    }
    public static void main(String [] args) throws IOException{
    Servidor SR= new Servidor();
    }
}

服务器威胁

package servidor;

import java.io.*;
import java.net.*;
public class MultiServer extends Thread{

    private DataInputStream datos_entrada;
    //private DataOutputStream datos_salida;
    private String mensajeI;
    private Socket socket;
    private int ID;
    private EnvioDatosjuegos getDatos;

    public MultiServer(Socket socket , int ID){
        getDatos= new EnvioDatosjuegos();
        this.socket= socket;
        this.ID=ID;
        try{
            datos_entrada= new DataInputStream(socket.getInputStream());
            //datos_salida= new DataOutputStream(socket.getOutputStream());
        }catch(IOException e){
              System.out.println(e);
        }
    }

    public void desconectar(){
        try{
            socket.close();
        }catch(IOException e){
            System.out.println(e);
        }
    }

    @Override
    public void run(){
        System.out.println("in the method run");
        try{
            mensajeI=datos_entrada.readUTF();
            if (mensajeI.equals("out")){
                System.out.println("Cliente ID:"+ ID+ ", socket:"+ socket+"; is out");
                desconectar();
             }
         else {
               System.out.print("Cliente ID:"+ ID+ ", socket:"+ socket+"; sending message");
               getDatos.verificarMensaje(mensajeI);

         }

       }catch(IOException e){
              System.out.println("P1"+e.getMessage());
       }
    }

    /*
    public void enviarDato( String mensaje){
          try{
          datos_salida.writeUTF(mensaje);
          System.out.println("resultado = "+ datos_entrada.read());
    }catch(IOException e){
        System.out.println("P"+e.getMessage());
    }
}*/

}

请帮助mi,抱歉语法,并且不是母语为英语的人。

0 个答案:

没有答案