一个简单的客户端 - 服务器 - 机器人<> PC [英] A simple client-server - Android<>PC

查看:118
本文介绍了一个简单的客户端 - 服务器 - 机器人<> PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试Android上但是我有一个问题,socket编程。客户通过主活动基本上发起与发送消息到该服务器和得到一个答复的基本功能的

I am testing socket programming on Android however I am having a problem. The client is basically launched through the main activity with a basic function of sending a message to the server and getting a reply.

客户端活动:

package com.test.socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class socketActivity extends Activity implements OnClickListener {

String input;
private EditText et;
private ObjectOutputStream oos;
private TextView tv;
private String message;
private ObjectInputStream ois;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try{

        InetAddress host = InetAddress.getLocalHost();
        Socket socket = new Socket(host.getHostName(),7777);
        //send to server

        oos = new ObjectOutputStream(socket.getOutputStream());

        et = (EditText) findViewById(R.id.text);
        Button sendButton = (Button) findViewById(R.id.button);
        sendButton.setOnClickListener(this);

        //read from server
        ois = new ObjectInputStream(socket.getInputStream());


        //System.out.println("Message from Server: "+message);
        tv = (TextView) findViewById(R.id.textView);

    }

    catch(UnknownHostException e)
    {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.button:
        input = et.getText().toString();
        try {
            oos.writeObject(input);
            ois.close();
            oos.close();
            message = (String) ois.readObject();
            tv.setText("Message from Server: "+message);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    }
}



}

从JCreator的单独启动的服务器级别的监听端口7777:

The Server class launched separately from JCreator listening to port 7777:

import java.io.*;
import java.lang.*;
import java.net.*;

public class server {

private ServerSocket server;
private int port = 7777;

public server()
{
    try{
        server = new ServerSocket(port);
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }


}

public static void main(String args[])
{
    server example = new server();
    example.handleConnection();
}

public void handleConnection()
{
    System.out.println("Waiting for client message...");

    while(true)
    {
        try{
            Socket socket = server.accept();
            new ConnectionHandler(socket);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

}

该服务器访问ConnectionHandler类:

ConnectionHandler class which the Server accesses:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;


public class ConnectionHandler implements Runnable {

private Socket socket;

public ConnectionHandler(Socket socket)
{
    this.socket = socket;

    Thread t = new Thread(this);
    t.start();
}

@Override
public void run() {
    // TODO Auto-generated method stub
    try{

        //receive from client
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        String message = (String) ois.readObject();

        System.out.println("Message from client: "+message);

        if(message.equals("check"))
        {
            System.out.println("Checking for malicious interference...");
            Thread.sleep(5000);
            System.out.println("Status: Files are good.");
        }

        //send response to client
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());


        if(message.equals("check"))
        {
            oos.writeObject("Checking...");
        }

        else oos.writeObject("Invalid input");



        ois.close();
        oos.close();
        socket.close();

        System.out.println("Waiting for client message...");
    }
    catch(IOException e)
    {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

我已经通过Java应用程序并不Android应用程序测试的JCreator的和Eclipse的code和它完美。然而,当我试图通过活动做它,它不工作。

I have tested the code on JCreator and Eclipse through Java application not Android app and it worked perfect. However when I try doing it through the activity, it's not working.

任何想法?

推荐答案

在Android中,本地主机将指向手机/仿真器本身。它并不指向您的服务器。以下线下方指向您的Andr​​oid设备/模拟器,而不是你的服务器。您需要获取服务器的实际IP来把它从你的Andr​​oid的工作

In Android, localhost would point to the phone/emulator itself. It doesn't point to your server. The following line below is pointing to your Android device/emulator and not your server. You need to get the actual IP of the server to get it working from your Android



    InetAddress host = InetAddress.getLocalHost();
    Socket socket = new Socket(host.getHostName(),7777);

它可以通过Java应用程序,因为当你从Java应用程序的上下文中执行它,本地主机指向同一服务器的位置。

It works through Java App, because when you execute it from the context of Java Application, the localhost is pointing to the same server location.

这篇关于一个简单的客户端 - 服务器 - 机器人<> PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆