liujiao_jiaoliu 2016-12-11 01:40 采纳率: 0%
浏览 1109

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条回答 默认 最新

  • day_moon 2016-12-11 03:01
    关注
    评论
  • qqranmian 2016-12-11 19:01
    关注

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

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

    评论
  • qqranmian 2016-12-11 19:22
    关注

    不好意思 兄弟 发错人

    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部