2

我有一个小问题,我已经设法将信息从客户端发送到服务器等......但我也希望能够通过 telnet 来做到这一点(打开它并说 go telnet 127.0.0.1 4444,然后输入 1 2 3 然后它出现在服务器中,就像通过客户端发送一样。目前我收到此错误:

java.io.StreamCorruptedException: invalid stream header: 310D0A32
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at ConnectionHandler.run(server1.java:73)
at java.lang.Thread.run(Unknown Source)

如果我做错了什么,请告诉我:

我的主要目标是拥有它,这样我就可以输入用户名、ID 和姓名,然后能够在一段时间内回忆它们,就像一个非常简单的签入签出系统。真的很想得到一些帮助<3 :)

客户:

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class client1 {

public static void main(String[] args) {
    try {
// Create a connection to the server socket on the server application
        Socket socket = new Socket("localhost", 7777);

// Send a message to the client application
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        //oos.writeObject("A B C");
        String data[]=new String[3];
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter details for the array");
        for(int x=0;x<3;x++){
            System.out.print("Enter word number"+(x+1)+":");
            data[x]=br.readLine();
        }
        oos.writeObject(data);
        System.out.println("Details sent to server...");
        oos.close();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

服务器:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.ClassNotFoundException;
import java.lang.Runnable;
import java.lang.Thread;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class server1
{
private ServerSocket server;
private int port = 4444;

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

}

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

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

// The server do a loop here to accept all connection initiated by the

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

class ConnectionHandler implements Runnable 
{
private Socket socket;
public ConnectionHandler(Socket socket)
{
    this.socket = socket;
    Thread t = new Thread(this);
    t.start();
}
public void run()
{
    try
    {
// Read a message sent by client application
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        String message[] = (String[]) ois.readObject();
        //System.out.println("Message Received from client: " + message);
        //b(message);
                    printArray(message);    
        ois.close();
        socket.close();
        System.out.println("Waiting for client message is...");
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    } 
    catch (ClassNotFoundException e) 
    {
        e.printStackTrace();
    }
}
private void b(String message) {
     List<String> list = new ArrayList<String>();
        String[] arr = list.toArray(new String[0]);
        System.out.println("Array is " + Arrays.toString(arr));

}
    private void printArray(String[] arr){
       for(String s:arr){
          System.out.println(s);
        }
}
4

0 回答 0