Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7667899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:13:16+00:00 2026-05-31T15:13:16+00:00

I have been strugling to get the client and server to send and recieve

  • 0

I have been strugling to get the client and server to send and recieve the right information. The program should be able to send a query from the client to the server. The server should then retrive the necessary information from the database and send in to the client as an array.

Java Server:

    public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket m_ServerSocket = new ServerSocket(1211, 2, InetAddress.getLocalHost());
        int id = 0;
        while (true) {
            Socket clientSocket = m_ServerSocket.accept();
            System.out.println(clientSocket.isConnected());


            ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
            cliThread.start();
        }
     }
     }

    class ClientServiceThread extends Thread
    {
    ResultSet res = null;
    Connection conn;


    ObjectOutputStream objOut;
    PrintWriter   out;
    BufferedReader   in;

    Socket clientSocket;
    int clientID = -1;
    boolean running = true;

    ClientServiceThread(Socket s, int i)
    {
        clientSocket = s;
        clientID = i;
    }

    public void run()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/videostore", "root", "IronFire");

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        try 
        {
            System.out.println("Server");
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            objOut= new ObjectOutputStream(clientSocket.getOutputStream());

            while (running)
            {
                String command = in.readLine();
                System.out.println("Query:  " + command);
                if(command.substring(0, command.indexOf(" ")).equalsIgnoreCase("SELECT+"))
                {
                    System.out.println("IN IF"); // for test
                    surname(command);
                }
                if (command.equalsIgnoreCase("quit"))
                {
                    running = false;
                    System.out.print("Stopping client thread for client : " + clientID);
                }
                else 
                {
                    out.println(command);
                    out.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void surname(String query)
    {
        try
        {
            System.out.println("In Surname"); // for test
            int count = 0; 
            int whileCounter = 0;
            Statement state = conn.createStatement();

            res = state.executeQuery(query.replace("+", ""));

            while(res.next())
            {
                count++;
            }
            String [] stringResult = new String[count];
            byte[] buff = new byte[count];
            res.beforeFirst();

            while(res.next())
            {
                stringResult[whileCounter] = res.getString(1) +"  "+  res.getString(2) +"  " + res.getString(3)  +"  "+ res.getString(5)  +"  "+ res.getString(10) +"  "+ res.getString(11) +"";
                whileCounter++;
            }

            for(int i = 0; i < stringResult.length; i++)
            {
                System.out.println(stringResult[i]);
            }

            objOut.reset();
            objOut.writeObject(stringResult);
            objOut.flush();


            System.out.println("After object");
            //objOut.close();


        }
        catch(Exception ex)
        {

        }

    }
}

Java Client:

    public class FuncListener2 extends MainGui implements Runnable
    {
    ResultSet res = null;
    Connection conn = null;

    Socket clientSocket = null;
    PrintStream os = null;
    ObjectInputStream objIn = null;
    ObjectOutputStream objOut = null;


    public FuncListener2()
    {
        String hostName = "";

        try {
            hostName = InetAddress.getLocalHost().toString(); // to get the hostName for the exceptions
            clientSocket = new Socket(InetAddress.getLocalHost(), 1211);
            System.out.println(clientSocket.isConnected());

            os = new PrintStream(clientSocket.getOutputStream());
            objIn = new ObjectInputStream(clientSocket.getInputStream());
            listeners();

            clientSocket.setKeepAlive(true);
        } 
        catch (UnknownHostException e)
        {
            System.err.println("Don't know about host "+hostName);
            e.printStackTrace();
        }
        catch (IOException e) 
        {
            System.err.println("Couldn't get I/O for the connection to the host "+hostName);
            e.printStackTrace();
        }
        catch(Exception ex)
        {
            System.err.println(ex.getMessage());
        }
    }

    public void listeners()
    {
        System.out.println("in listener");

        butCheckOutSearchSurname.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    System.out.println("surname search");
                    int count = 0;
                    Object obj = null;
                    Object [] objArray = null;
                    String [] array = null;

                    // the reason for the plus sign is to make sure the correct if statement is executed in the server
                    String query = "Select+ * FROM clientDet WHERE clientSurname = \'" + textCheckOutSearchSurname.getText() + "\'";
                    os.println(query);

                    if(objIn.readObject() != null)
                    {
                        objArray = (Object[]) objIn.readObject();
                    }
                    else
                    {
                        System.out.println("object empty");
                    }
                    objIn.reset();

                    listCheckOutClients.setListData(array);

                    System.out.println("iets");
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        });
    }
    public void run()
    {       

    }
    }

This exception is so far my biggest problem.

java.io.StreamCorruptedException: invalid type code: 53
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at FuncListener2$1.actionPerformed(FuncListener2.java:108)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:13:17+00:00Added an answer on May 31, 2026 at 3:13 pm

    did you mean to call readObject twice? you’re checking it for null, but shouldn’t you be saving it so you aren’t performing a second read?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Day, I have been struggling to get this right for quite some time
I have been struggling to send emails form within a Haskell program for a
I have been struggling to get this right! Can anyone help me to convert
I'm experimenting with the unobtrusive client validation and have been struggling to get it
I have been struggling all afternoon trying to get a simple mailto: tag to
I have been getting a stackoverflow exception in my program which may be originating
I have been struggling with a problem trying to get PLJava to work on
I have been struggling to get a 64-bit version of PyPlot. I have found
I have been struggling to get fetched properties working correctly in my app and
I have been struggling all day to get data to show in a jqgrid.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.