我的套接字应用程序有问题。 我试图在java serverocket和Android客户端之间更改字符串。 服务器从客户端获取字符串,但是客户端未从服务器收到答案。 如果我调试该应用程序,则它会从input.readline()直接跳转到ioexception ...服务器在Eclipse上在笔记本电脑上运行,而android-app在我的塔式计算机上的模拟器上运行。

这是我的Android客户端。

 package com.example.user.clientsocket;


         import java.io.BufferedReader;
         import java.io.BufferedWriter;
         import java.io.IOException;
         import java.io.InputStreamReader;
         import java.io.OutputStreamWriter;
         import java.io.PrintWriter;
         import java.net.InetAddress;
         import java.net.Socket;
         import java.net.UnknownHostException;
         import android.app.Activity;
         import android.graphics.Color;
         import android.os.Bundle;
         import android.view.View;
         import android.widget.EditText;
         import android.widget.TextView;

 public class TheClient extends Activity {

     private Socket socket;
     private static final int SERVERPORT = 1343;
     private static final String SERVER_IP = "192.168.0.102";
     //private BufferedReader input;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_the_client);
         new Thread(new ClientThread()).start();
     }

     public void onClick(View view) {     
         try {  
             EditText et =  findViewById(R.id.EditText01);
             String str = et.getText().toString();
             PrintWriter out = new PrintWriter(new BufferedWriter(
                     new OutputStreamWriter(socket.getOutputStream())),
                     true);

             BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             String read = input.readLine();
             out.println(str);
             out.flush();
             out.close();          
             //Input


             //BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

             //String read = ("da website");
             TextView url = findViewById(R.id.url);
             url.setText(read);
             //Input
             socket.close();     
         } catch (UnknownHostException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

     class ClientThread implements Runnable {
         @Override
         public void run() { 
             try {
                 InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                 socket = new Socket(serverAddr, SERVERPORT);
             } catch (UnknownHostException e1) {
                 e1.printStackTrace();
             } catch (IOException e1) {
                 e1.printStackTrace();
             }
         }
     } 
}

这就是清单

 <?xml version="1.0" encoding="utf-8"?> <manifest
 xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.user.clientsocket">


     <uses-permission android:name="android.permission.INTERNET" >
     </uses-permission>
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
     </uses-permission>


     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
         <activity android:name=".TheClient">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
     </application>

 </manifest>

至少这是我的Java服务器

public class WinCCSocket
{
    public static void main(String args[]) throws IOException
    {
        String URL="https://10.11.226.7/";

        String number, temp;
        ServerSocket s1 = new ServerSocket(1343);
        Socket ss = s1.accept();
        Scanner sc = new Scanner(ss.getInputStream());
        number = sc.nextLine();

        temp = number;
        System.out.println(temp);

        PrintStream p = new PrintStream(ss.getOutputStream());
        p.println(URL);
        p.flush();
        p.close();

        s1.close();
        sc.close();
        xml_write(temp);                        
    }

    public static void xml_write(String temp)
    {
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            Element rootElement = doc.createElement("Liste");
            doc.appendChild(rootElement);

         // letter
            Element Eintrag = doc.createElement("Eintrag");
            rootElement.appendChild(Eintrag);

            // name 
            Element Telefon = doc.createElement("Telefon");
            Telefon.appendChild(doc.createTextNode(temp));
            Eintrag.appendChild(Telefon);

            /*// street
            Element street = doc.createElement("street");
            street.appendChild(doc.createTextNode(temp));
            letter.appendChild(street);*/

            // als XML schreiben
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            //Wincc vm folder
            StreamResult result = new StreamResult(new File("C:/Users/Public/Documents/stringtest.xml")); 
            //testfolder local
            //StreamResult result = new StreamResult(new File("F:/example.xml"));  
            //WinCC VM location
            //StreamResult result = new StreamResult(new File("C:/Users/Public/Documents/Siemens/WinCCProjects/DasProjekt/NFCCode.xml"));

            transformer.transform(source, result);

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        }                   
    }                       
}

  ask by Florida Hill translate from so

本文未有回复,本站智能推荐: