help regarding secure socket communication using java

hello

i am doin my project based upon SSL communication using java..the problem is that i am not getting the output even though both the server and the client programs complie properly but there is no display /output during the run time. i am using NETBEANS 4.1 and both the client and the server are running on the same computer using HOME Edition..

server output show after running:

init:

deps-jar:

Compiling 3 source files to C:\Documents and Settings\OWAIS MANZOOR\Program1\build\classes

Note: C:\Documents and Settings\OWAIS MANZOOR\Program1\src\program1\Server.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

compile:

run:

Usage: java Server [port number]

Java Result: 1

BUILD SUCCESSFUL (total time: 0 seconds)

and the client running shows:

init:

deps-jar:

compile:

run:

Usage: java Client [hostname] [port number]

Java Result: 1

BUILD SUCCESSFUL (total time: 0 seconds)

so i need help in this matter ..programs n classes all hav been created properly..

[1160 byte] By [haroota] at [2007-10-3 6:30:52]
# 1
That didn't help. We need to see your output coding. Also, obviously, depending on your design, both client and server would have to be running at same time - if that's your design.Post code, but edit it for the output.
watertownjordana at 2007-7-15 1:18:00 > top of Java-index,Archived Forums,Socket Programming...
# 2

Well actually it's simpler than that. Both the server and client are printing 'Usage:' messages indicating that whatever parameters if any being passed to their main() methods are not correct. The applications don't seem to even run.

Inspecting the source code of both main() methods will reveal what kind of command lines they're expecting.

ejpa at 2007-7-15 1:18:00 > top of Java-index,Archived Forums,Socket Programming...
# 3

hi every1

i made the changes as mentioned above and the communiction is possible now..now my doubt is after the encryption takes place of a particular data is it posible to display the encrypted message..that iscan we display the encrypted text?if it is plz lemme know the changes i need to make .

i am enclosing the source code of both the client and server

THIS IS CLIENT.JAVA

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.io.*;

import java.net.*;

import java.security.*;

import java.util.*;

import javax.net.*;

import javax.net.ssl.*;

import javax.swing.*;

public class Client extends JFrame implements Runnable

{

/**

* Connection to the client

*/

private DataInputStream din;

/**

* Connection to the client

*/

private DataOutputStream dout;

/**

* KeyStore for storing our public/private key pair

*/

private KeyStore clientKeyStore;

/**

* KeyStore for storing the server's public key

*/

private KeyStore serverKeyStore;

/**

* Used to generate a SocketFactory

*/

private SSLContext sslContext;

/**

* A list of visible postings

*/

private Set postings = new HashSet();

/**

* The font used for all postings

*/

private Font font = new Font( "TimesRoman", Font.PLAIN, 18 );

/**

* Passphrase for accessing our authentication keystore

*/

static private final String passphrase = "clientpw";

/**

* A source of secure random numbers

*/

static private SecureRandom secureRandom;

public Client( String host, int port ) {

super( "Whiteboard" );

setupGUI();

connect( host, port );

new Thread( this ).start();

}

private void setupServerKeystore() throws GeneralSecurityException, IOException {

serverKeyStore = KeyStore.getInstance( "JKS" );

serverKeyStore.load( new FileInputStream( "server.public" ),

"public".toCharArray() );

}

private void setupClientKeyStore() throws GeneralSecurityException, IOException {

clientKeyStore = KeyStore.getInstance( "JKS" );

clientKeyStore.load( new FileInputStream( "client.private" ),

passphrase.toCharArray() );

}

private void setupSSLContext() throws GeneralSecurityException, IOException {

TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );

tmf.init( serverKeyStore );

KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );

kmf.init( clientKeyStore, passphrase.toCharArray() );

sslContext = SSLContext.getInstance( "TLS" );

sslContext.init( kmf.getKeyManagers(),

tmf.getTrustManagers(),

secureRandom );

}

private void connect( String host, int port ) {

try {

setupServerKeystore();

setupClientKeyStore();

setupSSLContext();

SSLSocketFactory sf = sslContext.getSocketFactory();

SSLSocket socket = (SSLSocket)sf.createSocket( host, port );

InputStream in = socket.getInputStream();

OutputStream out = socket.getOutputStream();

din = new DataInputStream( in );

dout = new DataOutputStream( out );

} catch( GeneralSecurityException gse ) {

gse.printStackTrace();

} catch( IOException ie ) {

ie.printStackTrace();

}

}

private void setupGUI() {

getContentPane().setLayout( new BorderLayout() );

ClientCanvas cc = new ClientCanvas();

getContentPane().add( cc, BorderLayout.CENTER );

setSize( 400, 250 );

setVisible( true );

cc.requestFocus();

}

public void run() {

try {

while (true) {

Posting posting = Posting.read( din );

postings.add( posting );

repaint();

}

} catch( IOException ie ) {

ie.printStackTrace();

}

}

private void newPosting( Posting posting ) {

postings.add( posting );

try {

posting.write( dout );

} catch( IOException ie ) {

ie.printStackTrace();

}

}

class ClientCanvas extends JPanel

{

private String currentString;

private int currentX=-1;

private int currentY=-1;

public ClientCanvas() {

setBackground( Color.white );

addMouseListener( new MouseListener() {

public void mousePressed( MouseEvent me ) {

}

public void mouseEntered( MouseEvent me ) {

}

public void mouseExited( MouseEvent me ) {

}

public void mouseReleased( MouseEvent me ) {

clickAt( me.getX(), me.getY() );

}

public void mouseClicked( MouseEvent me ) {

}

} );

addKeyListener( new KeyListener() {

public void keyPressed( KeyEvent ke ) {

}

public void keyReleased( KeyEvent ke ) {

}

public void keyTyped( KeyEvent ke ) {

key( ke.getKeyChar() );

}

} );

}

public void paintComponent( Graphics g ) {

super.paintComponent( g );

g.setFont( font );

for (Iterator it=postings.iterator(); it.hasNext();) {

Posting posting = (Posting)it.next();

Color color = posting.color();

if (color==null) {

color = Color.black;

}

g.setColor( color );

g.drawString( posting.text(), posting.x(), posting.y() );

}

if (currentString != null) {

g.setColor( Color.black );

g.drawString( currentString+"|", currentX, currentY );

}

}

private void finishPosting() {

if (currentString!=null && !currentString.equals( "" )) {

Posting posting = new Posting( currentString, currentX, currentY );

newPosting( posting );

}

currentString = null;

repaint();

}

private void backspace() {

if (currentString != null && currentString.length()>0) {

currentString = currentString.substring( 0, currentString.length()-1 );

repaint();

}

}

private void clickAt( int x, int y ) {

finishPosting();

currentX = x;

currentY = y;

currentString = "";

repaint();

}

private void key( int key ) {

if (currentString==null || currentX==-1) {

return;

}

if (key=='\n') {

finishPosting();

} else if (key==8) {

backspace();

} else {

if (currentString==null) {

currentString = "";

}

currentString += (char)key;

repaint();

}

}

}

static public void main( String args[] ) {

if (args.length != 2) {

System.err.println( "Usage: java Client [hostname] [port number]" );

System.exit( 1 );

}

String host = args[0];

int port = Integer.parseInt( args[1] );

System.out.println( "Wait while secure random numbers are initialized...." );

secureRandom = new SecureRandom();

secureRandom.nextInt();

System.out.println( "Done." );

new Client( host, port );

}

}

THIS IS SERVER.JAVA

import java.io.*;

import java.net.*;

import java.security.*;

import java.util.*;

import javax.net.*;

import javax.net.ssl.*;

public class Server implements Runnable

{

/**

* The port we will listen on

*/

private int port;

/**

* A list of open connections

*/

private Set connections = new HashSet();

/**

* KeyStore for storing our public/private key pair

*/

private KeyStore clientKeyStore;

/**

* KeyStore for storing the server's public key

*/

private KeyStore serverKeyStore;

/**

* Used to generate a SocketFactory

*/

private SSLContext sslContext;

/**

* A list of visible postings

*/

private Set postings = new HashSet();

/**

* Passphrase for accessing our authentication keystore

*/

static private final String passphrase = "serverpw";

/**

* A source of secure random numbers

*/

static private SecureRandom secureRandom;

/**

* Create a Server that listens on the given port.

* Start the background listening thread

*/

public Server( int port ) {

this.port = port;

new Thread( this ).start();

}

private void setupClientKeyStore() throws GeneralSecurityException, IOException {

clientKeyStore = KeyStore.getInstance( "JKS" );

clientKeyStore.load( new FileInputStream( "client.public" ),

"public".toCharArray() );

}

private void setupServerKeystore() throws GeneralSecurityException, IOException {

serverKeyStore = KeyStore.getInstance( "JKS" );

serverKeyStore.load( new FileInputStream( "server.private" ),

passphrase.toCharArray() );

}

private void setupSSLContext() throws GeneralSecurityException, IOException {

TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );

tmf.init( clientKeyStore );

KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );

kmf.init( serverKeyStore, passphrase.toCharArray() );

sslContext = SSLContext.getInstance( "TLS" );

sslContext.init( kmf.getKeyManagers(),

tmf.getTrustManagers(),

secureRandom );

}

/**

* Background thread: accept new connections

*/

public void run() {

try {

setupClientKeyStore();

setupServerKeystore();

setupSSLContext();

SSLServerSocketFactory sf = sslContext.getServerSocketFactory();

SSLServerSocket ss = (SSLServerSocket)sf.createServerSocket( port );

// Require client authorization

ss.setNeedClientAuth( true );

System.out.println( "Listening on port "+port+"..." );

while (true) {

Socket socket = ss.accept();

System.out.println( "Got connection from "+socket );

ConnectionProcessor cp = new ConnectionProcessor( this, socket );

connections.add( cp );

}

} catch( GeneralSecurityException gse ) {

gse.printStackTrace();

} catch( IOException ie ) {

ie.printStackTrace();

}

}

/**

* Remove a connection that has been closed from our set

* of open connections

*/

void removeConnection( ConnectionProcessor cp ) {

connections.remove( cp );

}

/**

* Return an iteration over open connections

*/

Iterator getConnections() {

return connections.iterator();

}

/**

* Add a posting to the list of postings

*/

void addPosting( Posting posting ) {

postings.add( posting );

System.out.println( "list is "+postings.size() );

}

/**

* Return an iteration over visible postings

*/

Iterator getPostings() {

return postings.iterator();

}

/**

* Create and start a Server. The port number must

* be provided on the command line

*/

static public void main( String args[] ) {

if (args.length != 1) {

System.err.println( "Usage: java Server [port number]" );

System.exit( 1 );

}

int port = Integer.parseInt( args[0] );

System.out.println( "Wait while secure random numbers are initialized...." );

secureRandom = new SecureRandom();

secureRandom.nextInt();

System.out.println( "Done." );

new Server( port );

}

}

haroota at 2007-7-15 1:18:00 > top of Java-index,Archived Forums,Socket Programming...
# 4
I'm not going to chase you every time you start a new thread for the same topic, I'm staying here, but there is no encrypted file here, and with SSL for Java there is no way to see the encrypted data: well, no easy way.
ejpa at 2007-7-15 1:18:00 > top of Java-index,Archived Forums,Socket Programming...