客户端-服务器套接字通信中的对象问题 TCP/IP

Problems with Objects in Client-Server socket comunication TCP/IP

我正在尝试 运行 一个试图与创建服务器线程的套接字 TCP-IP 通信的程序。

当我运行正在运行程序时,它什么也没做。

客户端代码:

package benjagarrido.com.examen2;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public class Mesa1 {
    public static void main(String[] args) {
        Socket soc;
        ObjectOutputStream objSalida=null;
        ObjectInputStream objEntrada=null;
        int puerto = 12345;

        ArrayList<Comensal> listComensales = solicitarComensales();

        try {
            // Creamos el socket de comunicacion
            soc = new Socket(InetAddress.getLocalHost(), puerto);
            // Creamos los canales de comunicación entrada/salida
            // objEntrada= new ObjectInputStream(soc.getInputStream());
            objSalida = new ObjectOutputStream(soc.getOutputStream());
            objSalida.writeUTF("Mesa 1");
            objSalida.writeObject(listComensales);
            soc.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static ArrayList<Comensal> solicitarComensales() {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        System.out.println("¿Cuantos comensales serán? (Máximo 4 comensales)");
        int iNumComensales;
        iNumComensales = sc.nextInt();
        ArrayList<Comensal> listComensales = new ArrayList<>();
        for (int i = 0; i < iNumComensales; i++) {
            Comensal comensal = solicitarMenu(i);
            listComensales.add(comensal);
        }
        return listComensales;
    }

    private static Comensal solicitarMenu(int i) {
        Comensal comensal = new Comensal();
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        System.out
                .println("Comensal" + (i + 1) + " Elija el plato principal: ");
        for (Platos plato : Platos.values()) {
            System.out.println(plato.name());
        }
        comensal.setiPlato(sc.nextInt());
        System.out.println("Comensal" + (i + 1)
                + " Elija la bebida principal: ");
        for (Bebidas bebida : Bebidas.values()) {
            System.out.println(bebida.name());
        }
        comensal.setiBebida(sc.nextInt());
        System.out.println("Comensal" + (i + 1)
                + " Elija el postre principal: ");
        for (Postres postre : Postres.values()) {
            System.out.println(postre.name());
        }
        comensal.setiPostre(sc.nextInt());
        return comensal;
    }
}

服务器代码:

package benjagarrido.com.examen2;

import java.io.*;
import java.net.*;
import java.io.*;
import java.net.*;

@SuppressWarnings("unused")
public class Servidor {

    public static void main(String[] args) {
        ServerSocket servidor;
        Socket serverCliente;
        int puerto = 12345;

        try {
            servidor = new ServerSocket(puerto);
            System.out.println("Servidor arrancado correctamente");
            //while (true) {
                // aceptamos la conexion de cliente
                serverCliente = servidor.accept();
                Thread hilo = new Thread(new ServidorHilo(serverCliente));
                hilo.start();
            //}
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

服务器线程的代码:

package benjagarrido.com.examen2;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;

public class ServidorHilo implements Runnable {
    ObjectOutputStream objSalida=null;
    ObjectInputStream objEntrada=null;
    Socket soc;
    int puerto = 12345;

    public ServidorHilo(Socket sCliente) {
        this.soc = sCliente;
    }

    @Override
    public void run() {
        ArrayList<Comensal> listComensales = new ArrayList<>();
        try {
            // Creamos el socket de comunicacion
            soc = new Socket(InetAddress.getLocalHost(), puerto);
            // Creamos los canales de comunicación entrada/salida
            objEntrada= new ObjectInputStream(soc.getInputStream());
            //objSalida = new ObjectOutputStream(soc.getOutputStream());

            //String sMesa = objEntrada.readUTF();
            listComensales=(ArrayList<Comensal>) objEntrada.readObject();
            Date date = new Date();
            String sFechaHora = date.getDay()+"/"+date.getMonth()+"/"+date.getYear()+"-"+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
            System.out.println("Se ha recibido el pedido de "+""+" con fecha y hora "+sFechaHora);
            //salida.write();
            objSalida.close();
        } catch (IOException e) {
            // nothing
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

我需要你的帮助。

当我尝试创建另一个套接字时,错误出现在 class ServidorHilo 上:

// Creamos el socket de comunicacion
soc = new Socket(InetAddress.getLocalHost(), puerto);

谢谢,抱歉。