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

  • SEARCH
  • Home
  • 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 7849809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:38:46+00:00 2026-06-02T18:38:46+00:00

//Here is the code to request the server send the public key to client

  • 0

//Here is the code to request the server send the public key to client

package gameserver;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.net.Socket;
import java.io.*;
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;

public class GameClient  extends JFrame implements ActionListener {

    private JButton b1 = new JButton("press");

    public GameClient () {
       GridLayout layout = new GridLayout(3, 0, 0, 0);
       setLayout(layout);
       setSize(320,150);

       add(b1);
       b1.addActionListener(this);

    }

    public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                 GameClient frame = new GameClient();
                 frame.setVisible(true);
            }
      });
    }  

   public void actionPerformed(ActionEvent ae) {
       if (ae.getSource() == b1) {
            Socket socket = null;
            ObjectInputStream in = null;
            ObjectOutputStream out = null;

            Scanner console = new Scanner(System.in);

            try {
                socket = new Socket("127.0.0.1", 12346);
                in = new ObjectInputStream(socket.getInputStream());
                out = new ObjectOutputStream(socket.getOutputStream());
                Object result;
                result = in.readObject();

                System.out.println(result);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    } 
                    if (out != null) {
                        out.close();
                    }
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }           
       }

   } 
}

//server code, to send the public key to client when client connect

package gameserver;

import java.io.*;
import java.net.*;
import java.security.*; 
import javax.crypto.*; 

public class GameServer {
    public static void main(String arg[]) {
        try {
            ServerSocket ss = new ServerSocket(12346);

                        while(true) {
            Socket s = ss.accept();
            ClientHandler ch = new ClientHandler(s);
                        ch.start();

                        }
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

class ClientHandler extends Thread  {

    Socket socket;
    KeyPair keyPair = null;

    public ClientHandler(Socket socket)  {
        this.socket = socket;

    }

    public KeyPair genKeyPair () {

        try {
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); 
            keyGen.initialize(1024); 
            keyPair = keyGen.generateKeyPair(); 
            //PrivateKey privateKey = keyPair.getPrivate();
            //PublicKey publicKey = keyPair.getPublic();

        } catch (NoSuchAlgorithmException e) {
            System.out.println(e.getMessage());
        }   
        return keyPair;
    }

    public void run() {
        try {             

            ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());               
            ObjectOutputStream Oos = new ObjectOutputStream(socket.getOutputStream());

            keyPair = genKeyPair ();
            PublicKey publicKey = keyPair.getPublic();
            Oos.writeObject(publicKey);

            socket.close();
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }

}

the problem is that, when i press the button, the client seems don’t connect to server, and receive nothing, any ideas ? thanks

  • 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-06-02T18:38:52+00:00Added an answer on June 2, 2026 at 6:38 pm

    Your code is blocked at this line:

     ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());
    

    From the documention

    Creates an ObjectInputStream that reads from the specified
    InputStream. A serialization stream header is read from the stream and
    verified. This constructor will block until the corresponding
    ObjectOutputStream has written and flushed the header.

    If a security manager is installed, this constructor will check for
    the “enableSubclassImplementation” SerializablePermission when invoked
    directly or indirectly by the constructor of a subclass which
    overrides the ObjectInputStream.readFields or
    ObjectInputStream.readUnshared methods.

    Fix:

    Change your

    ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());               
    ObjectOutputStream Oos = new ObjectOutputStream(socket.getOutputStream());
    

    to

    ObjectOutputStream Oos = new ObjectOutputStream(socket.getOutputStream());
    ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my code: if Request.Form(authorize) <> and request.form(delete) <> true then post_ids = Request.form(authorize)
Here's the code I'm using: // create a request HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
Here is a snippet of the code : HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(request.RawUrl); WebRequest.DefaultWebProxy =
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
Here is code from a tutorial in A Byte of Python: import sys filename
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
I've a server in Java which listens the request on the UDP socket port
I have the following algorithm implemented in Java which uses TCP/IP: -Client request a
I have set up a UDP client/server model that can send string messages to
I have a problem in my Java webapp. Here is the code in index.jsp:

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.