PC上的客户端程序连接到Android上的服务器程序

时间:2017-04-14 12:36:38

标签: java android

我正在尝试创建一个简单的服务器应用程序和客户端程序以扩展

他们的工作在以后的时间在我的毕业设计项目中使用

我将客户端代码作为Java简单程序在我的PC上

我有一个Android线程,其中我将我的服务器程序放在Android手机上

服务器应该监听端口

并在Accept

上注册

问题是当我运行模拟器时,当我使用本地地址“127.0.0.1”

时 客户端和服务器上的

首先启动服务器并启动线程成功

但是当运行客户端程序时,它会捕获“连接拒绝异常”

当我构建APK并在我的Android手机上通过ip“192.168.3.100”启动服务器时服务器启动成功

客户端也正常运行,但是当我发送任何号码时,我的Android活动中都没有显示

非常感谢任何建议

我的服务器代码

          package com.example.laptophome.securesys;

/**
  * Created by laptop home on 13-Apr-17.
  */
import android.content.Context;
import android.os.Looper;
import android.widget.Toast;

import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

public class Connection extends Thread {

    private boolean isBinded = false;
    private String IpAddress;
    private Context cx;
    private int num;
    //private Socket socket;
    private InputStreamReader inRead;
    public ServerSocketChannel server;
   // private volatile  boolean isRunning =true;
        public Connection(String IpAddress ,Context cx) {
        this.IpAddress = IpAddress;
        this.cx = cx;

       }

    @Override
    public void run() {

        try {

            Selector selector = Selector.open();


            int[] ports = {5050, 4001, 6000};
         for (int port : ports) {
                server = ServerSocketChannel.open();
                server.configureBlocking(false);
                server.socket().bind(new InetSocketAddress(IpAddress, port));

                server.register(selector, SelectionKey.OP_ACCEPT);

         }
           while (selector.isOpen()) {

               selector.select();

               Iterator iterator = selector.selectedKeys().iterator();
              if (!iterator.hasNext()){Toast.makeText(cx, "iterrator is empty ", Toast.LENGTH_SHORT).show();}
            while (iterator.hasNext()) {
                SelectionKey key = (SelectionKey) iterator.next();
                if (key.isAcceptable()) {

                    SocketChannel client = server.accept();
                    if (client != null) {
                        Toast.makeText(cx, "New Image Recieved", Toast.LENGTH_SHORT).show(); //This Toas Doesn't Appear EVER
                        Socket socket = client.socket();
                        inRead = new InputStreamReader(socket.getInputStream());
                        num = inRead.read();

                        Toast.makeText(cx, "New = " + num, Toast.LENGTH_SHORT).show();


                    }


                }
            }
        }
    } catch (Exception e) {
        Looper.prepare();

     Toast.makeText(cx,"Error" + e.getMessage().toString(),Toast.LENGTH_SHORT).show();
          } }}

我的客户计划

   public class Client {

     public static void main(String args[]) throws IOException{
           String num=" ";
           Scanner sc= new Scanner(System.in);
           try {
              Socket  s = new Socket("192.168.3.100",5050);
                while(true){
                  PrintStream p = new PrintStream(s.getOutputStream());
                  System.out.println(p.toString());
                  num=sc.next();
                  p.println(num);
                 }

            }
             catch (Exception e ) {
                 System.out.println(e);}}}

0 个答案:

没有答案