Discussion of communication between C # Server and Java client based on Networkcomms V3 communication framework

Source: Internet
Author: User

First of all, the NETWORKCOMMS communication Framework is a commercial version, this article does not provide.

Not familiar with Java, just installed Eclipse, communication only implements the characters from the Java client to the C # server, the server receives a message, returns a character message to the Java client, the Java client displays the received message.

Server-side based Networkcomms V3 C # communication framework.

Server-side code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingnetworkcommsdotnet;usingnetworkcommsdotnet.dpsbase;usingNetworkcommsdotnet.tools;usingnetworkcommsdotnet.connections;usingNetworkCommsDotNet.Connections.TCP;usingSystem.Net;usingMessageContract;namespaceserver{ Public Partial classform1:form {sendreceiveoptions SRO=NewSendreceiveoptions (dpsmanager.getdataserializer<nullserializer> (),NULL,NULL);  PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            //server starts listening for client requests//start listening on a port//communication with other languages disabled ApplicationlayerprotocolstatusTcpconnectionlistener listener=NewTcpconnectionlistener (sro,applicationlayerprotocolstatus.disabled); Connection.startlistening (Listener,NewIPEndPoint (Ipaddress.parse (Txtip.text),int.                      Parse (Txtport.text)); Button1. Text="Listening in"; Button1. Enabled=false; //This method contains server-specific processing methods. startlistening (); }        Private voidstartlistening () {networkcomms.disablelogging (); //Array received bytesNetworkcomms.appendglobalincomingunmanagedpackethandler (header, connection, array) =            {                //C # language UTF-8 byte array converted to string                stringRecvstr = Encoding.UTF8.GetString (Array,0, Array.                                 Length); //record the proof that the message has been received                stringNewstr ="the received string is"+Recvstr; Loginfo.logmessage (Newstr,"log. txt"); //return message to Java client                stringServerstr ="message sent by the server 123"; byte[] Newbyte =Encoding.UTF8.GetBytes (SERVERSTR); //Send byte dataconnection.             Sendunmanagedbytes (Newbyte);                     }); }             Private voidForm1_formclosing_1 (Objectsender, FormClosingEventArgs e)            {Networkcomms.shutdown ();        Environment.exit (Environment.exitcode); }    }}

Client interface:

Client Java code (not very familiar with Java, the code may be inappropriate):

Package Com.yee4.liu;import Java.io.BufferedReader;  Import Java.io.InputStream;  Import Java.io.InputStreamReader;  Import Java.io.OutputStream;  Import Java.io.PrintWriter;   Import Java.net.Socket;  Public classHelloWorld extends thread{ Public Static byte[] Readstream (InputStream instream) throws Exception {intCount =0;  while(Count = =0) {Count=instream.available (); }          byte[] B =New byte[Count];          Instream.read (b); returnb; }      /** * @param args*/     Public Static voidMain (string[] args) throws Exception {socket Socket=NULL; intj =0; Try{Socket=NewSocket ("127.0.0.1", the); if(socket.isconnected ()) {System. out. println ("A connection has been established!---"); String strcontent="messages sent by Java"; byte[] bs = Strcontent.getbytes ("UTF-8"); //2. Get socket read/write streamOutputStream os=Socket.getoutputstream (); //3. Use the stream to read and write the socket according to certain operationOs.write (BS);                                                                      Os.flush (); System. out. println ("----------------------------------"); InputStream IPs=Socket.getinputstream (); byte[] Rebyte =Readstream (IPS); String ResultStr=NewString (Rebyte,0, Rebyte.length,"UTF-8"); System. out. println ("Receive host message:"+resultstr); }                     }                   Catch(Exception s) {System. out. Print (s); }            }    }
View Code

Review the process

(1): Open C # program, start listening

(2): Run the Java client and send a message to the C # server

    String strcontent="messages sent by Java ";                             byte [] bs = Strcontent.getbytes ("UTF-8");                                 // 2. Get socket read/  write stream                OutputStream os=socket.getoutputstream ();                                    // 3. Use the stream to read and write  the socket according to certain operation                             Os.write (BS);                Os.flush ();               

(3): After the C # server receives the message, it parses

<1>:

  //   C # language UTF-8 byte array converted to string                string 0 , Array. Length);                                  // record the proof that the message has been received                string " the received string is " + recvstr;                 " log. txt ");

<2> return message to the Java client:

  // return message to Java client                string " message sent by the server 123 " ;                 byte [] Newbyte = Encoding.UTF8.GetBytes (serverstr);                 // Send byte data                

(4): The Java client receives the message and displays it

  InputStream ips = socket.getinputstream ();                   byte [] Rebyte = readstream (IPs);                                        New 0 " UTF-8 " );                 System. out. println (" received host message:" + resultstr);  

The Java program appears to have exited after receiving the message, and I don't know how to handle it.

The program is only successful in My Computer test, Win7 system, not tested on other systems.

C # project file download (without communication framework)

Java files

Reference article:

Http://www.cnblogs.com/iyangyuan/archive/2012/12/23/2829712.html

http://cuisuqiang.iteye.com/blog/1434416

http://blog.csdn.net/mecho/article/details/7342654

http://www.cnblogs.com/iyangyuan/archive/2012/12/23/2829712.html#3140522

http://cuisuqiang.iteye.com/blog/1434416

http://blog.csdn.net/paincupid/article/details/10635323

Discussion of communication between C # Server and Java client based on Networkcomms V3 communication framework

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.