1
    I'm send data from two clint to server 
    and save the one clint in object waiting anther  clint to send the server 
    server send massages to two clint but massage arrive to clint and clint massege error.
  1. I'm send data from two clint to server
  2. and save the one clint in object waiting anther clint to send the server
  3. server send massages to two clint but massage arrive to clint and clint massege error.

Error anther Clint Error Enter your name : mahmoud waiting for user comming... Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1696) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:501) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:459) at javaapplication4.clint.main(clint.java:57)

server

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication4;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import static javaapplication4.ThreadServer.ID;
import static javaapplication4.ThreadServer.usersList;


public class ThreadServer implements  TecTakInterface {
   // public static data [] users =new data[30];
    
    
     public static List <data> usersList=new ArrayList<data>();  
     public static long  ID=1;
     public static void main(String[] args) throws IOException {
    
        final int PORT = 4040;
        ServerSocket serverSocket = null;
       // ServerSocket serverSocket = new ServerSocket(PORT);
         try {
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            e.printStackTrace();

        }
        System.out.println("Server started...");
        System.out.println("Wating for clients...");
        Socket clientSocket=null;
        while (true) {
            try {
               clientSocket = serverSocket.accept();
            } catch (IOException e) {
                System.out.println("I/O error: " + e);
            }
          new EchoThread(clientSocket).start();
        }
        
   
    }   

    @Override
    public String Tostring() throws RemoteException {
          String a = "";
          if(usersList.isEmpty()==false){
             for (data s: usersList){
               a +="Id : " +s.id+ ", name palyer : " + s.name+"  \n";
                             
               }}else{
                 a="0";
                }
                          
        return a;
    }
}
class data {
        public long id;
        public String name;
        public Socket socket;
        public int [] paly={1,2,3,4,5,6,7,8,9};
        public data(long id,String name,Socket clientSocket){
        this.id=id;
        this.name=name;
        this.socket = clientSocket;
        
        }  
        
        void cupy(data p){
            this.id=p.id;
            this.name=p.name;
          //  this.paly=p.paly;
            this.socket=p.socket;
        }
}



class EchoThread extends Thread {
    protected Socket socket;
    

    public EchoThread(Socket clientSocket) {
        this.socket = clientSocket;
    }
    @Override
                public void run() {
                    try (
                        OutputStream out = socket.getOutputStream();
                        Scanner in = new Scanner(socket.getInputStream());
                    ) {
                        while (in.hasNextLine()) {
                            String input = in.nextLine();
                          
                       ObjectOutputStream oout = new ObjectOutputStream(out);
                          
                          String a = "";
                          if(usersList.isEmpty()==false){
                          for (data s: usersList){
                             a +="Id : " +s.id+ ", name palyer : " + s.name+"  \n";
                             
                             }}else{
                              a="0";
                            }
                        //  a+="\0";
                           //System.out.println(" client: " + a);
                           //out.println(a);
                           oout.writeObject(a);
                            // oout.close();

                         data s1= new data(ID,input,socket);
                           usersList.add(s1); //  out.println("waiting for user comming...");
                               ID++;
                                final DataInputStream InFromClient = new DataInputStream(socket.getInputStream());
                                final DataOutputStream OutToClient = new DataOutputStream(socket.getOutputStream());
                            data playo = new data(s1.id,s1.name,s1.socket);
                          if(!a.equals("0")){
                        long idPlayer=(long) InFromClient.readInt();
                       //  long idPlayer = Long.parseLong(in.next());
                        System.out.println(idPlayer +"   ");
                         for (data s: usersList){
              if(s.id==idPlayer){ 
                   System.out.println(s.id+"   "+ s.name);
                 // playo.cupy(s);
                playo.id=s.id;
            playo.name=s.name;
          //  playo.paly=s.paly;
            playo.socket=s.socket;
              break;
              }
                 }
                  System.out.println(playo.id+" ss   "+ playo.name);
                  
                        try (
                        //out = socket.getOutputStream();
                        //in = new Scanner(socket.getInputStream());
                        OutputStream out1 = playo.socket.getOutputStream();
                        Scanner in1 = new Scanner(playo.socket.getInputStream());

                           ){
                        ObjectOutputStream oout1 = new ObjectOutputStream(out1);
                          oout.writeObject("start in  x "); 

                           oout.writeObject("the paly  start : ");
                        
                           oout.writeObject("He/she is player x (: ");


                           `Exception erorr send data to Clint sockat`
                            oout1.writeObject("the paly  start : ");
                             // oout1 = new ObjectOutputStream(out1);

                            oout1.writeObject("He/she is player O  (: ");
                            //oout1 = new ObjectOutputStream(out1);
                            oout1.writeObject("start in  x ");
                             
                          
                          
                   
                   }catch (IOException e) { 
                     System.out.println("erorr111 "+e);
                    
                    }       
            
                         
                            }

                            System.out.println("Received radius from client: " + input);

                        }
                    } catch (IOException e) { 
                     System.out.println("erorr"+e);
                    
                    }
                }
}



    Why is it giving the following error? in the seved  socket Clint  and send in anther  thread

error anther clint

clint


package javaapplication4;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;

import java.util.Scanner;

/**
 *
 * @author 5442
 */
public class clint {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        final String HOST = "127.0.0.1";
        final int PORT = 4040;
        
        //System.out.println("Client started.");
        
        try (
            Socket socket = new Socket(HOST, PORT);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
          //  Scanner in = new Scanner(socket.getInputStream());
            Scanner s = new Scanner(System.in);
            InputStream in = socket.getInputStream();
      final DataInputStream FromServer = new DataInputStream(socket.getInputStream());
       final DataOutputStream ToServer = new DataOutputStream(socket.getOutputStream());
        ) {
            
                System.out.print("Enter your name : ");
                String input = s.nextLine();
                out.println(input);
                 
               ObjectInputStream oin = new ObjectInputStream(in);
               String stringFromServer = (String) oin.readObject();
                // System.out.println(in.);
                if("0".equals(stringFromServer)){
                 System.out.println("waiting for user comming...");
                }
                else{
                System.out.println(stringFromServer);
                System.out.println("Enter the Id of player");
                //System.out.print("Enter your name : ");
                int idPalyer  = s.nextInt();
                System.out.println(idPalyer);
               // out.println(idPalyer);
                ToServer.writeInt(idPalyer);
    // than use ToServer.writeInt() and FromServer.readInt()
                }
                         //      ObjectInputStream oin = new ObjectInputStream(in);
                      //  oin.close();

               stringFromServer = (String)oin.readObject();
                 System.out.println(stringFromServer);
                stringFromServer = (String) oin.readObject();
                 System.out.println(stringFromServer);
                 stringFromServer = (String) oin.readObject();
                 System.out.println(stringFromServer);
     
                         
          
        }
    }
 
}



Naeem Odeh
  • 11
  • 1
  • maybe this answer will help you https://stackoverflow.com/questions/2393179/streamcorruptedexception-invalid-type-code-ac – Boris Azanov Aug 06 '21 at 20:38

0 Answers0