Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: SSL Chat implementation

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default SSL Chat implementation

    Hi,
    I would like to program SSL Chat. I have not experence with this stuff. OK I have one question. Is possibility do it that way first thread should be work like Server (SSLServerSockte) this Thread should be reciving data. And second Thread should be work like Client (SSL Socket) and send data to another same program. Or how can i do this? Thanks a lot.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: SSL Chat

    Yes that is possible. ServerSockets and Sockets are essentially the same thing. However a server socket waits for a connection, once it has one it binds it to a normal Socket, thus it can be used the same as a socket. Sockets are capable of both sending and recieving data.

    But what you have said is perfectly possible, I'd suggest reading up on Sockets and ServerSockets. Then looking into how you manage the newer SSL Extensions to the Socket connections. Since data sending etc is done using the same methods, but handshakes etc are required before hand using SSL Sockets.

    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    Koren3 (April 23rd, 2009)

  4. #3
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: SSL Chat

    Thanks Chris. I already did this program. But i am not sure if it is right. Befero I did SocketDatagram it is new stuff for me. Could you look on my code? Thank you.

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.net.ssl.SSLServerSocket;
    import javax.net.ssl.SSLServerSocketFactory;
    import javax.net.ssl.SSLSocket;
    import javax.net.ssl.SSLSocketFactory;
     
    /**
     * SslReverseEchoer.java
     * Copyright (c) 2005 by Dr. Herong Yang
     */
     
    public class Main {
       public static void main(String[] args) {
           String adresaIP;
           if(args.length==0)
           {
                adresaIP="127.0.0.1";
           }
           else
           {
               adresaIP=args[0];
           }
          PrijimaciVlakno prijmout = new PrijimaciVlakno();
          OdesilaciVlakno odeslat = new OdesilaciVlakno(adresaIP);
     
          prijmout.prijimaci.start();
          odeslat.prijimaci.start();
       } 
    }
     
    class PrijimaciVlakno implements Runnable
    {   Thread prijimaci; 
     
        public PrijimaciVlakno()
        {
            prijimaci = new Thread(this,"Prijimaci vlakno");
        }
        public void run()
        {
            try {
                SSLServerSocketFactory sslFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
                SSLServerSocket sslServer = (SSLServerSocket) sslFactory.createServerSocket(5000);
                SSLSocket ssl = (SSLSocket) sslServer.accept();
                System.out.println("Adresa hostitele:"+ssl.getInetAddress().getHostName());
     
                while(true){
     
                    BufferedReader cteniZeSocketu = new BufferedReader(new InputStreamReader(ssl.getInputStream()));
                    String radek = null;
                    while((radek = cteniZeSocketu.readLine())!=null)
                    {
                        System.out.println("Prijata data:"+radek);
                        System.out.flush();
                    }
                }
     
            } catch (IOException ex) {
     
            }
        }
    }
     
    class OdesilaciVlakno implements Runnable
    {   Thread prijimaci;
        private String adresaIP;
        public OdesilaciVlakno(String ip)
        {
            this.adresaIP=ip;
            prijimaci = new Thread(this,"Prijimaci vlakno");
        }
        public void run()
        {   
            try {
                SSLSocketFactory sslFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
                SSLSocket ssl = (SSLSocket) sslFactory.createSocket(this.adresaIP,5000);
     
                BufferedReader klavesnice = new BufferedReader(new InputStreamReader(System.in));
     
                OutputStream vystupniProud = ssl.getOutputStream();
                BufferedWriter zapisDoSocketu = new BufferedWriter(new OutputStreamWriter(vystupniProud));
     
                String radek = null;
                while((radek = klavesnice.readLine())!=null)
                {
                    zapisDoSocketu.write(radek+'\n');
                    zapisDoSocketu.flush();
                    System.out.println("Posilana data:"+radek);
                }
            } catch (IOException ex) {
                Logger.getLogger(OdesilaciVlakno.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }
    }

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: SSL Chat

    Can I ask what the program is supposed to do at this stage, once I know that when I'm next at home I will have a look for you, but in college I'm cannot do any socket programming due to firewalls and port monitoring and blocking :@

    But ye what is it supposed to do

    Thanks,
    Chris

  6. #5
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: SSL Chat

    I hope that This program conect to another same program and it will work like chat. Maybe my code is whole wrong I donīt know. How can i do more simply but both of "programs" must be same. No client server but like server server or client client or my program is hope client and server in one.

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: SSL Chat

    This should help to explain P2P concepts. It also has an Example Java & Python P2P Implementation

    Berry College - Peer to Peer Programming

    Chris

  8. #7
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: SSL Chat

    I know P2P. I need this program that This program can be use for more people you know. Every one who will have this program can communicate but always Peer to Peer. Do you understand me?

Similar Threads

  1. TCP chat attaching cipher is not running
    By Koren3 in forum Java Networking
    Replies: 4
    Last Post: May 19th, 2009, 02:52 AM
  2. Replies: 10
    Last Post: May 8th, 2009, 10:49 AM
  3. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM
  4. Replies: 0
    Last Post: May 3rd, 2009, 10:55 AM
  5. Replies: 1
    Last Post: April 20th, 2009, 11:17 AM