1

私は長年にわたってこれに取り組んでおり、私が抱えている問題は単純です サーバーからクライアントにデータを印刷することはできません サーバーがクライアントにメッセージを送信するときに電話が取得または印刷されないということだけで、他のすべてが機能しています洞察や助けがあれば素晴らしいと思います。エラーは発生していません

クライアント

        package com.example.handy;

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.io.OutputStream;
    import java.net.UnknownHostException;
    import java.util.Date;
    import java.util.Scanner;

    import android.R.integer;
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.provider.ContactsContract;
    import android.text.format.DateFormat;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity
    {

        private EditText ipaddress;
        private Button connect;
        private Button wipe;
        private static String myIp;



        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            StrictMode.ThreadPolicy policy = new StrictMode.
            ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy); 



            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ipaddress = (EditText) findViewById(R.id.ipaddress_felid);

            connect = (Button) findViewById(R.id.connect);
            wipe =(Button) findViewById(R.id.wipe);



            //Button press event listener
            connect.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                 {
                     setMyIp(ipaddress.getText().toString());
                    // myComs.sending_data(getMyIp() , "Got connected");
                    try
                     {
                        new Incomingdata().execute();
                        InetAddress inet = InetAddress.getByName(getMyIp());
                        Socket s = new Socket(inet, 2000);
                        OutputStream o = s.getOutputStream();
                        PrintWriter p = new PrintWriter(o);





                         p.println("You are connected");
                         p.flush();

                         readContacts();
                         readSms();




                     }



                     catch (UnknownHostException e) 
                     {
                        ipaddress.setText("Unknown host");
                           e.printStackTrace();
                     }
                    catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                 }


             });
            wipe.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                 {
                    String kill = "5";
                     myComs.sending_data(MainActivity.getMyIp(), kill);
                     finish();

                 }
                 });
        }
    public class Incomingdata extends AsyncTask<Void,Void,Void>
    {
        @Override
        protected Void doInBackground(Void... params) 
        {
            try
            {   System.out.println("Test123");
                ServerSocket serverSocket = new ServerSocket(2000);
                Socket s = serverSocket.accept();
                System.out.println("Test1234");
                InputStream in = s.getInputStream();
                Scanner r = new Scanner(in);
                System.out.println("Test1235");
                while(s.isConnected())
                {
                    String input =r.nextLine();
                    System.out.println("Client"+input);
                }
                in.close();
            }
            catch (UnknownHostException e) 
            {
                ipaddress.setText("Unknown host");
                   e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }



}

サーバ

    package handy_server.simple_gui;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


//imports for server
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Pandaboy
 */
class ServerGUI extends JFrame implements ActionListener
{
    private Socket connection;
    private InetAddress ip;
    private JTextField t1 = new JTextField(null);
    private JTextField t2 = new JTextField(null);
    private JTextField t3 = new JTextField(null);
    private JButton b2 = new JButton("Send");
    private JButton b1 = new JButton("Working");

    private JPanel p1 = new JPanel();
    private ServerSocket listeningSocket;
    private int port= 0;
    private ArrayList<Contact> myContacts = new ArrayList<Contact>();

      //-------------------------serverGui------------------------------------------------------------
    public ServerGUI(int port){
        this.port = port;
        init();

    }
    public  void init()
    {

        Container content = getContentPane();
        content.setLayout(new FlowLayout());
        Font f = new Font("TimesRoman", Font.BOLD, 20);
        p1.setLayout(new GridLayout(2, 2));
        content.add(p1);
        p1.add(t1);
        p1.add(b1);
        p1.add(t2);
        p1.add(t3);
        p1.add(b2);
        b1.addActionListener(this);
         b2.addActionListener(this);
        setSize(210, 300);
        setVisible(true);
        System.out.println("Just about to start the server...");
        startServer();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object target = e.getSource();
        if (target == b1)
        {

            t1.setText("Button working");
            MyHelpers.buildConversations(this, myContacts);
        }

        if(target == b2)
        {
              t1.setText("button working2");
            String number = t2.getText();
            int phone_length = number.length();
            if (phone_length <= 20)
            {
                for(int a=1; a <=(20 - phone_length); a++ )
            {
                    number += " ";
            }
            }
            String msg = t3.getText();
            String text = "7"+number+msg;
            System.out.print(""+text);
             OutputStream o = null;
            try
            {
                o = connection.getOutputStream();
            }
            catch (IOException ex)
            {
                Logger.getLogger(ServerGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            PrintWriter p = new PrintWriter(o);
            p.println(text);
            p.flush();
            System.out.print("text sent"+text);




        }
    }
     //-----------------------------------------------------------------------------------------------------
    //------------------------------startServer------------------------------------------------------------
    private void startServer() 
    {
        SwingWorker <Void, String> runningServer = new SwingWorker<Void, String>(){
            protected Void doInBackground() 
            {
                System.out.println("in startserver...");
                try 
                {
                    listeningSocket = new ServerSocket(port);
                    try
                    {
                        ip = InetAddress.getLocalHost();
                        System.out.println("Please enter this in your phone " + ip.getHostAddress());

                    }
                    catch (UnknownHostException e)
                    {
                        e.printStackTrace();
                    }
                    while (true)
                    {
                        System.out.println("Waiting for connection");
                        connection = listeningSocket.accept();


                        InputStream in = connection.getInputStream();
                        Scanner r = new Scanner(in);
                        OutputStream o = connection.getOutputStream();
                        PrintWriter p = new PrintWriter(o);

                        String message = r.nextLine();
                        System.out.println("" + message);
                        t1.setText(message);

                        // get the message type
                        // 0 sms 1 contact 2 incoming call 
                        System.out.println(message);
                        if(message.startsWith("0"))
                        {
                            System.out.println(message);
                            String type = message.substring(1,2);
                            int theType = Integer.parseInt( type );
                            String number = message.substring(7, 21).trim();
                            String theText = message.substring(21);
                            String theName = MyHelpers.getName(number, myContacts);
                            System.out.println("Number = "+number);
                            System.out.println("Sender = "+theName);
                            System.out.println("Text = "+theText);
                            Contact cRef = MyHelpers.getContactReference(number, myContacts);
                            if (cRef != null)
                            {
                                cRef.addsms(theType, theName, theText);
                            }
                        }    



                        if(message.startsWith("1"))
                        {
                           System.out.println(message);
                          String name = message.substring(1, 31).trim();
                          String pnumber = message.substring(31, 51).trim();
                          String email = message.substring(51, 91).trim();

                          myContacts.add(new Contact(name, pnumber, email));
                          System.out.println( name + pnumber + email);  
                        }

                        if(message.startsWith("2"))
                        {
                           String unkown = message.substring(0, 1).trim();

                           String number = message.substring(1, 14).trim();
                           String theName = MyHelpers.getName(number, myContacts);
                           System.out.println(""+unkown+""+theName+" Is calling you");
                        }
                        if(message.startsWith("5"))
                        {
                            System.exit(0);
                        }    

どんな助けでも素晴らしいでしょう、私はこれに困惑しています

4

1 に答える 1

0

サーバーでは、応答の最後に「\ n」を追加して、これを変更します

 PrintWriter p = new PrintWriter(o);
            p.println(text);
            p.flush();
            System.out.print("text sent"+text);

これに:

 PrintWriter p = new PrintWriter(o);
            p.println(text + "\n");
            p.flush();
            System.out.print("text sent"+text);

ハングする理由は、readLine() が '\n' を探していて、それを受信しないためだと思います

while(s.isConnected())
{
    String input =r.nextLine();
    System.out.println("Client"+input);
}
于 2013-08-06T04:53:37.567 に答える