android中服务器到底怎么才连得上啊!!!

写了好久,服务器就是连不上,求大神帮忙看看。
package com.ll.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class server extends Thread{
private Socket socket = null;

public server (Socket socket){


}
static HashMap map = new HashMap();
static{//用户存储,使用map模拟数据库
    map.put("liujiao", "123");
}
static HashMap pools = new HashMap();

public static Set<String> set = new HashSet();
public void run(){
    try{
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
        byte[] b = new byte[100];
        in.read(b);
        String cmd = new String(b).trim();
        if(cmd.startsWith("login")){//登录
            //登录格式 username,password,
            String str[] = cmd.split(",");//按逗号分割
            String username = str[1];
            String password = str[2];
            try{
                if(map.get(username).equals(password)){
                    out.write("ok".getBytes());
                    out.flush();
                    set.add(socket.getInetAddress().getHostAddress());//set不存重复值
                    //set.add(socket.getLocalAddress().getHostAddress());
                    return ;
                }
                throw new Exception();
            }catch(Exception e){
                out.write("error".getBytes());
                out.flush();
            }


        }else if(cmd.startsWith("get")){//下载
            //下载格式 get,16565.amr
            try{
                String filename = cmd.split(",")[1];
                File f = new File("D:\\amr", filename);
                if(f.exists()){
                    out.write(("ok," + f.length()).getBytes());
                    out.flush();
                    byte[] b1 = new byte[10];
                    in.read(b1);
                    FileInputStream fin = new FileInputStream(f);
                    int len = 0;
                    byte[] b2 = new byte[1024];
                    while((len = fin.read(b2)) != -1){
                        out.write(b2,0,len);
                        out.flush();
                    }
                    fin.close();
                    return ;
                }
                throw new Exception();
            }catch(Exception e){
                out.write("error".getBytes());
                out.flush();
            }


        }else if(cmd.startsWith("upload")){//上传
            //上传格式 upload,15644414(size),username
            String s = cmd.split(",")[1];
            String username = cmd.split(",")[2];
            long size = Long.parseLong(s);
            out.write("ok".getBytes());
            out.flush();
            File ff = new File("D:\\amr", (new Date().getTime()) + "R"+ ((int ) (Math.random()*100000)) + ".amr");
            FileOutputStream fout = new FileOutputStream(ff);
            byte[] b1 = new byte[1024];
            int len = 0;
            long length = 0;
            while((len=in.read(b1)) != -1){
                length+=len;
                fout.write(b1,0,len);
                if(length >= size){
                    break;
                }
            }
            fout.close();

            Pool p = new Pool();
            pools.put(p.id, p);
            p.message = username + "," + ff.getName();
            for (String ip:set){
                if(ip.equalsIgnoreCase(socket.getInetAddress().getHostAddress()))
                //if(ip.equalsIgnoreCase(socket.getLocalAddress().getHostAddress()))
                    continue;
                p.ips.add(ip);
            }

            p.start();
        }
    }catch(Exception e){

    }finally{
        try{
            socket.close();
        }catch(Exception e1){
        }
    }
}

public static void openServer() throws Exception{
    ServerSocket server = new ServerSocket(12345);
    while(true){
        new server (server.accept()).start();
        System.out.println("新消息");
    }
}

}

package com.ll.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class StartServer {

public static void main(String[] args){
    new Thread(){
        public void run(){
            try{
                server.openServer();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    };
    new Thread(){
        public void run(){


            try{
                UDPServer.openServer();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    };
}
/*public StartServer(){

    //获得ip
    Socket socket = null;
    OutputStream out = null;
    InputStream in = null;

    try {
        ServerSocket serversocket = new ServerSocket(8008);
        socket = serversocket.accept();
        String ip = socket.getLocalAddress().getHostAddress();
        int port = socket.getPort();
        System.out.println("ip:" + ip +",port:" + port);
        in = socket.getInputStream();
        byte[] b = new byte[1024];
        int i = in.read(b);
        String recevice = new String(b,0,i);
        out = socket.getOutputStream();
        String mes = "";
        out.write(mes.getBytes());
        in.close();
        out.close();
        socket.close();
        serversocket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}*/

}
package com.ll.server;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.swing.text.html.HTMLDocument.Iterator;

public class Pool extends Thread{
public String id;
public String message = "";
public Set ips = new HashSet();
public void remove(String ip){
ips.remove(ip);
}
public boolean b=false;
public void run(){
try{
System.out.println("AAAAAAA");
java.net.DatagramSocket scoket = new DatagramSocket();
for (int i = 0;i < 3; i++){
//Iterator it = ips.iterator();
java.util.Iterator it = ips.iterator();
while (it.hasNext()){
String ip = it.next();
byte[] b = (message + "," + id).getBytes();
//DatagramPacket data = new DatagramPacket(b,b.length,InetAddress.getByName(ip),7777);
DatagramPacket data = new DatagramPacket(b,b.length,7777);
scoket.send(data);
}
try{
Thread.sleep(3000);
}catch(InterruptedException e){

            }
            b=true;

        }
    }catch(Exception e){

    }
}
public Pool(){
    id = new Date().getTime() + "";

}

}

package com.ll.server;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPServer extends Thread{
DatagramPacket data = null;
public UDPServer (DatagramPacket data){
this.data = data;
}

public void run(){
    // 34161352
    String s = new String(data.getData()).trim();
    Pool p = (Pool) server.pools.get(s);
    p.ips.contains(s);
    p.ips.remove(s);
    if(p.b){
        server.pools.remove(s);
    }
}
public static void openServer() throws Exception{
    DatagramSocket socket = new DatagramSocket(12345);
    while(true){
        byte[] b= new byte[1000];
        DatagramPacket data = new DatagramPacket(b,b.length);
        socket.receive(data);
        new UDPServer(data).start();

    }
}

}

3个回答

https://github.com/saiwu-bigkoo/Android-ConvenientBanner
https://github.com/youth5201314/banner

这两个可以帮到你 想用自己的也行 一会发你邮箱

不好意思 兄弟 发错人

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问