Java sockets and. NET Socket Communication Demo

Source: Internet
Author: User

Terminator Protocol "# #"

ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket;Importjava.net.UnknownHostException;Importjava.util.ArrayList;Importjava.util.List;Importorg.codehaus.jackson.*;ImportOrg.codehaus.jackson.map.ObjectMapper; Public classMain {Private StaticJsongenerator Jsongenerator =NULL; Private StaticObjectmapper Objectmapper =NULL;  Public Static voidMain (string[] args)throwsIOException {objectmapper=NewObjectmapper (); Jsongenerator=objectmapper.getjsonfactory (). Createjsongenerator (System.out, Jsonencoding.utf8); List<Sms> list =NewArraylist<sms>();        List.clear (); Sms Model=NewSms (); Model.setcontent (Content); MODEL.SETMB ("15504718283"); MODEL.SETPB ("123663");        List.add (model);        List.add (model); System.out.println (""+list.size ()); String Str=objectmapper.writevalueasstring (list);        System.out.println (str); String BS=base64.encode (Str.getbytes ("UTF-8"));        SYSTEM.OUT.PRINTLN (BS); SENDMSG (BS+"##"); }         Public Static voidsendmsg (String str)throwsunknownhostexception, IOException {socket socket=NewSocket ("127.0.0.1", 2012); BufferedReader Sin=NewBufferedReader (NewInputStreamReader (system.in)); PrintWriter OS=NewPrintWriter (Socket.getoutputstream ()); BufferedReader is=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); String ReadLine= "Yes";  while(!readline.equals ("Bye") ) {os.println (str);            Os.flush (); System.out.println ("Client:" +str); System.out.println ("Server:" +is.readline ()); ReadLine=Sin.readline (); } os.close (); //close the socket output streamIs.close ();//close the socket input streamSocket.close ();//Close Socket    }            /*(non-java-doc) * @see java.lang.object#object ()*/     PublicMain () {Super(); }}
ImportOrg.codehaus.jackson.annotate.JsonProperty; Public classSms {//@JsonProperty ("Content")    PrivateString Content; //@JsonProperty ("Mb")    PrivateString Mb; //@JsonProperty ("Pb")    PrivateString Pb; @JsonProperty ("Content")      PublicString getcontent () {returnContent; }     Public voidsetcontent (String content) {content=content; } @JsonProperty ("Mb")      PublicString GETMB () {returnMb; }     Public voidSETMB (String MB) {MB=MB; } @JsonProperty ("Pb")      PublicString GETPB () {returnPb; }     Public voidSETPB (String pb) {PB=PB; }}
Importjava.lang.*; ImportJava.io.*;  Public classBase64 {Private Static Char[] Base64encodechars =New Char[] {         ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ',         ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ',         ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ',         ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F ',         ' G ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ',         ' O ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ',         ' W ', ' x ', ' y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ',         ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' + ', '/' }; Private Static byte[] Base64decodechars =New byte[] {     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 62,-1,-1, 1, 63,     52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1, 1,     -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1,-1,-1, 1,     -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1, 1 };  Public StaticString Encode (byte[] data) {StringBuffer SB=NewStringBuffer (); intLen =data.length; inti = 0; intB1, B2, B3;  while(I <Len) {B1= data[i++] & 0xff; if(i = =Len) {Sb.append (base64encodechars[b1>>> 2]); Sb.append (base64encodechars[(B1& 0x3) << 4]); Sb.append ("==");  Break; } B2= data[i++] & 0xff; if(i = =Len) {Sb.append (base64encodechars[b1>>> 2]); Sb.append (base64encodechars[(B1& 0x03) << 4) | ((B2 & 0xf0) >>> 4)]); Sb.append (base64encodechars[(B2& 0x0f) << 2]); Sb.append ("=");  Break; } B3= data[i++] & 0xff; Sb.append (BASE64ENCODECHARS[B1>>> 2]); Sb.append (base64encodechars[(B1& 0x03) << 4) | ((B2 & 0xf0) >>> 4)]); Sb.append (base64encodechars[(B2& 0x0f) << 2) | ((B3 & 0xc0) >>> 6)]); Sb.append (Base64encodechars[b3& 0x3f]); }         returnsb.tostring (); }        Public Static byte[] Decode (String str)throwsunsupportedencodingexception {stringbuffer sb=NewStringBuffer (); byte[] data = Str.getbytes ("Us-ascii")); intLen =data.length; inti = 0; intB1, B2, B3, B4;  while(I <Len) {             /*B1*/              Do{B1= base64decodechars[data[i++]]; }  while(I < len && B1 = =-1); if(B1 = =-1) Break; /*B2*/              Do{B2= base64decodechars[data[i++]]; }  while(I < len && B2 = =-1); if(B2 = =-1) Break; Sb.append ((Char) ((B1 << 2) | ((B2 & 0x30) >>> 4))); /*B3*/              Do{B3= data[i++]; if(B3 = = 61)returnSb.tostring (). GetBytes ("Iso-8859-1"); B3=BASE64DECODECHARS[B3]; }  while(I < len && B3 = =-1); if(B3 = =-1) Break; Sb.append ((Char) (((B2 & 0x0f) << 4) | ((B3 & 0x3c) >>> 2))); /*B4*/              Do{B4= data[i++]; if(B4 = = 61)returnSb.tostring (). GetBytes ("Iso-8859-1"); B4=BASE64DECODECHARS[B4]; }  while(I < len && b4 = =-1); if(B4 = =-1) Break; Sb.append ((Char) (((B3 & 0x03) << 6) |b4)); }         returnSb.tostring (). GetBytes ("Iso-8859-1"); } }

Construct list<sms> to JSON to base64 add Terminator # # Encoding UTF-8

C#

The corresponding resolution convert.frombase64string () can be

JSON standard parsing

Java sockets and. NET Socket Communication Demo

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.