java—如何从客户机通过套接字发送字符串,然后从服务器获得返回?

hsvhsicv  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(194)

我正在尝试制作一个简单的javaandroid应用程序我想把订单从我的电脑(客户端)发送到我的android应用程序(服务器)手机(服务器)必须把退货发送回客户端(计算机)
客户端(计算机)

public class Main {

    public static void main(String[] args) {
        Scanner sx = new Scanner(System.in);
        String ln = sx.next();
        try {
            Socket s = new Socket("192.168.2.2", 4040);
            s.setSoTimeout(4000);
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            dos.writeUTF(ln);
            dos.flush();
            s.close();
            System.out.println("Done....");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

服务器(android应用程序)

public class MainActivity extends Activity {
    EditText portfield, ipfield;
    LinearLayout player;
    SharedPreferences sp;
    SharedPreferences.Editor ed;
    String myip, myport;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        player = (LinearLayout) findViewById(R.id.player);
        portfield = (EditText) findViewById(R.id.portfield);
        ipfield = (EditText) findViewById(R.id.ipfield);
        sp = getSharedPreferences("okayawalas2423", MODE_PRIVATE);
        ed = sp.edit();
        myport = sp.getString("myport", null);
        myip = sp.getString("myip", null);
        runit();
    }

    public void runit() {
        if (myport != null && myip != null) {
            final String myport;
            final String myip;
            startspy();
        }
    }

    public void startspy() {
        player.removeAllViews();
        Thread th = new Thread(new Runnable() {
            public void run() {
                eim();
            }
        });
        th.start();
    }

    public void eim() {
        try {
            ServerSocket server = new ServerSocket(Integer.parseInt(myport));
            say("Server Connected Successfully .... ");
            while (true) {
                Socket skt = server.accept();
                say("The Server Request Has Been Accepted .... ");
                DataInputStream dis = new DataInputStream(skt.getInputStream());
                String purein = dis.readUTF();
                dsay("Data Has Been Sent Right Now To  /// and it contains : \n [[[ " + dis.readUTF() + " ]]]");
                DataOutputStream dos = new DataOutputStream(skt.getOutputStream());
                String outword = doit(purein);
                dos.writeUTF(outword);
                dos.flush();
                dis.close();
                dos.close();
                skt.close();
            }
        } catch (Exception e) {
            TextView tz = new TextView(this);
            tz.setText(e.toString());
            player.setGravity(Gravity.CENTER);
            player.addView(tz);
        }
    }

    public void stt(View v) {
        if (portfield.getText().toString().equals("") || ipfield.getText().toString().equals("")) {
            Toast.makeText(this, "Insert All Data !!", Toast.LENGTH_LONG).show();
        } else {
            ed.putString("myport", portfield.getText().toString());
            ed.putString("myip", ipfield.getText().toString());
            ed.commit();
            startspy();
        }
    }

    public String doit(String p) {
        String funRes = "none";
        funRes = p;
        return funRes;
    }

    public void say(String s) {
        TextView tz = new TextView(this);
        tz.setText(s);
        player.setGravity(Gravity.CENTER);
        player.addView(tz);
    }

    public void dsay(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }
}

我在电脑(客户端)上输入消息,当我按回车键时,我得到“完成…”但是安卓应用程序掉下来了!!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题