Java Socket practice 5 use encryption protocol to transmit objects

Source: Internet
Author: User

Address: http://blog.csdn.net/kongxx/article/details/7259837

One of Java Socket practices: Single-thread Communication

Java Socket practice 2 multi-thread Communication

Java Socket 3 transmission object

Java Socket practice 4 transmission of compressed objects

The previous blog posts mentioned some common socket usage, but for some applications with security requirements, data needs to be encrypted for transmission, and sslsocket is required.

You need a simple Java object that implements the java. Io. serializable interface.

<Textarea readonly name = "code" class = "Java"> package COM. googlecode. garbagecan. test. socket. SSL; </P> <p> public class user implements Java. io. serializable {<br/> Private Static final long serialversionuid = 1l; <br/> private string name; <br/> private string password; </P> <p> public user () {</P> <p >}</P> <p> public user (string name, string password) {<br/> This. name = Name; <br/> This. password = password; <br/>}</P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p> Public String GetPassword () {<br/> return password; <br/>}</P> <p> Public void setpassword (string password) {<br/> This. password = password; <br/>}</P> <p >}</textarea>The serversocketfactory class is used to create an sslserversocket class instance, and then an sslsocket instance is obtained through sslserversocket. The concept of object-oriented interface-oriented programming is considered here. Code Sslserversocket and sslsocket are not found in, but they use their parent serversocket and socket. After obtaining the serversocket and socket instances, the remaining code is the same as that without encryption.

Package COM. googlecode. garbagecan. test. socket. SSL; </P> <p> Import Java. io. bufferedinputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. objectinputstream; <br/> Import Java. io. objectoutputstream; <br/> Import java.net. serversocket; <br/> Import java.net. socket; <br/> Import Java. util. logging. level; <br/> Import Java. util. logging. logger; </P> <p> Import javax.net. serversocketfactory; <br/> Import javax.net. SSL. sslserversocketfactory; </P> <p> public class myserver {</P> <p> private final static logger = logger. getlogger (myserver. class. getname (); </P> <p> Public static void main (string [] ARGs) {<br/> try {<br/> serversocketfactory factory = sslserversocketfactory. getdefault (); <br/> serversocket Server = factory. createserversocket (10000); </P> <p> while (true) {<br/> Socket socket = server. accept (); <br/> invoke (socket); <br/>}< br/>} catch (exception ex) {<br/> ex. printstacktrace (); <br/>}</P> <p> Private Static void invoke (final Socket socket) throws ioexception {<br/> New thread (New runnable () {<br/> Public void run () {<br/> objectinputstream is = NULL; <br/> objectoutputstream OS = NULL; <br/> try {<br/> is = new objectinputstream (New bufferedinputstream (socket. getinputstream (); <br/> OS = new objectoutputstream (socket. getoutputstream (); </P> <p> Object OBJ = is. readobject (); <br/> User user = (User) OBJ; <br/> system. out. println ("User:" + User. getname () + "/" + User. getPassword (); </P> <p> User. setname (user. getname () + "_ new"); <br/> User. setpassword (user. getPassword () + "_ new"); </P> <p> OS. writeobject (User); <br/> OS. flush (); <br/>}catch (ioexception ex) {<br/> logger. log (level. severe, null, ex); <br/>} catch (classnotfoundexception ex) {<br/> logger. log (level. severe, null, ex); <br/>}finally {<br/> try {<br/> is. close (); <br/>}catch (exception ex) {}< br/> try {<br/> OS. close (); <br/>}catch (exception ex) {}< br/> try {<br/> socket. close (); <br/>}catch (exception ex) {}< br/>}< br/> }). start (); <br/>}< br/>}The ssl client class is similar to the SSL server class, but the method for obtaining the socket is changed. The rest of the code is the same as that without encryption.

Package COM. googlecode. garbagecan. test. socket. SSL; </P> <p> Import Java. io. bufferedinputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. objectinputstream; <br/> Import Java. io. objectoutputstream; <br/> Import java.net. socket; <br/> Import Java. util. logging. level; <br/> Import Java. util. logging. logger; </P> <p> Import javax.net. socketfactory; <br/> Import javax.net. SSL. sslsocketfactory; </P> <p> Publ IC class myclient {</P> <p> private final static logger = logger. getlogger (myclient. class. getname (); </P> <p> Public static void main (string [] ARGs) throws exception {<br/> for (INT I = 0; I <100; I ++) {<br/> Socket socket = NULL; <br/> objectoutputstream OS = NULL; <br/> objectinputstream is = NULL; </P> <p> try {<br/> socketfactory factory = sslsocketfactory. getdefault (); <br/> socket = factory. cr Eatesocket ("localhost", 10000); </P> <p> OS = new objectoutputstream (socket. getoutputstream (); <br/> User user = new user ("User _" + I, "password _" + I); <br/> OS. writeobject (User); <br/> OS. flush (); </P> <p> is = new objectinputstream (New bufferedinputstream (socket. getinputstream (); <br/> Object OBJ = is. readobject (); <br/> If (OBJ! = NULL) {<br/> User = (User) OBJ; <br/> system. out. println ("User:" + User. getname () + "/" + User. getPassword (); <br/>}< br/>} catch (ioexception ex) {<br/> logger. log (level. severe, null, ex); <br/>}finally {<br/> try {<br/> is. close (); <br/>}catch (exception ex) {}< br/> try {<br/> OS. close (); <br/>}catch (exception ex) {}< br/> try {<br/> socket. close (); <br/>}catch (exception ex) {}< br/>}< br/>}After writing the code, you need to generate the keystore file and run the following command:

Keytool-genkey-alias mysocket-keyalg RSA-keystore mysocket. jksIn the prompt input item, if the password item is set by yourself, press enter without changing anything else. Here, the password I use is "mysocket ".

Run Server

Java -djavax.net. SSL. keystore = mysocket. jks -djavax.net. SSL. keystorepassword = mysocket com. googlecode. garbagecan. Test. Socket. SSL. myserverRun Client

Java -djavax.net. SSL. truststore = mysocket. jks -djavax.net. SSL. truststorepassword = mysocket com. googlecode. garbagecan. Test. Socket. SSL. myclient

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.