gpt4 book ai didi

java - Android应用程序到服务器套接字程序不从应用程序读取字符串

转载 作者:太空狗 更新时间:2023-10-29 12:42:31 25 4
gpt4 key购买 nike

我有一个 android 应用程序,它将使用以下代码将字符串发送到服务器:

package com.example.testapp;


import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
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.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

private Socket socket;

private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "192.168.1.125";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new Thread(new ClientThread()).start();
}

public void onClick(View view) {
try {
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
} 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();
}

}

}
}

在服务器端,我正在运行如下的 java 程序:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServerExample {

//static ServerSocket variable
private static ServerSocket server;
//socket server port on which it will listen
private static int port = 5000;

public static void main(String args[]) throws IOException, ClassNotFoundException{
//create the socket server object
server = new ServerSocket(port);
//keep listens indefinitely until receives 'exit' call or program terminates
while(true){
System.out.println("Waiting for client request");
//creating socket and waiting for client connection
Socket socket = server.accept();
//read from socket to ObjectInputStream object
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//convert ObjectInputStream object to String
String message = (String) ois.readObject();
System.out.println("Message Received: " + message);
//create ObjectOutputStream object
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
//write object to Socket
oos.writeObject("Hi Client "+message);
//close resources
ois.close();
oos.close();
socket.close();
//terminate the server if client sends exit request
if(message.equalsIgnoreCase("exit")) break;
}
System.out.println("Shutting down Socket server!!");
//close the ServerSocket object
server.close();
}

}

但它没有读取我从 Android 应用程序发送的字符串。相反,当我从应用程序提交时,java 程序显示以下错误:

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 54657374
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:803)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at SocketServerExample.main(SocketServerExample.java:29)

我该如何解决这个问题。我的错误在哪里??是服务器端还是客户端??请帮帮我。

最佳答案

问题分析:

您使用 PrintWriter 从客户端发送,但您在服务器的接收端使用 ObjectInputStream。这两个不兼容。

你必须同时使用一对WriterReader或者一对ObjectOutputStreamObjectInputStream,但你不能混合它们。

解决方案:

我们将使用成对的WriterReader。客户端已经使用了一个Writer和一个Reader,所以我们只需要更改服务器端即可。

在服务器端,使用 BufferedReader 而不是 ObjectInputStream:

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
reader.readLine();

此外,在服务器端使用 PrintWriter 而不是 ObjectOutputStream 发送回客户端:

PrintWriter writer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
writer.println(str);

总而言之,服务器端看起来像这样:

import java.io.IOException;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServerExample {

//static ServerSocket variable
private static ServerSocket server;
//socket server port on which it will listen
private static int port = 5000;

public static void main(String args[]) throws IOException, ClassNotFoundException{
//create the socket server object
server = new ServerSocket(port);
//keep listens indefinitely until receives 'exit' call or program terminates
while(true){
System.out.println("Waiting for client request");
//creating socket and waiting for client connection
Socket socket = server.accept();

// //read from socket to ObjectInputStream object
// ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
// //convert ObjectInputStream object to String
// String message = (String) ois.readObject();
// System.out.println("Message Received: " + message);

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message = reader.readLine();
System.out.println("Message Received: " + message);

// //create ObjectOutputStream object
// ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
// //write object to Socket
// oos.writeObject("Hi Client "+message);

PrintWriter writer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
writer.println("Hi Client "+message);

//close resources
reader.close();
writer.close();
socket.close();
//terminate the server if client sends exit request
if(message.equalsIgnoreCase("exit")) break;
}
System.out.println("Shutting down Socket server!!");
//close the ServerSocket object
server.close();
}

}

关于java - Android应用程序到服务器套接字程序不从应用程序读取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23264150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com