[Work notes] Socket communication for Java and C #

Source: Internet
Author: User

In the daily development, different sensors support the development language is often different. For example, Kinect is generally developed in C + +, and leapmotion is much more developed in Java.

A project that was done last summer (17 summer) was to capture five finger joint information with leapmotion and then transfer it to a manipulator with five fingers.

The development framework, LeapMotion, was developed in Java, and the robotic AR10 was developed in C #.

So the Java side can enable the leapmotion to capture the information of five fingers and 10 joints and save it as a text file, respectively, by programming.

The C # end enables the AR10 to read text files and translate them into the position instructions of each joint to achieve the same action instructions.

However, this can only be done offline control, through the socket communication, to achieve real-time data transmission, in order to achieve control.

Java-side added code:

To add a library at the beginning:

Import Java.net.ServerSocket;
Import Java.net.Socket;

Add in the class (where TMP is the data that needs to be sent in real time):

TMP = anglethumb12+ "" +anglethumb23+ "" +angleindex01+ "" +angleindex12+ "" +anglemiddle01+ "" +anglemiddle12+ "" + anglering01+ "+anglering12+" "+anglepinky01+" "+anglepinky12+" "+" \ n ";
System.out.println ("Java Sockets program has started."); intI=0;Try{datagramsocket Socket=NewDatagramsocket (); System.out.println ("Sending the UDP socket ..."); //Send the Message "HI"Socket.send (Todatagram ("", Inetaddress.getbyname ("127.0.0.1"), 3800)); //While (true) {SYSTEM.OUT.PRINTLN (TMP); System.out.println ("Sending hi" +i); Thread.CurrentThread (); Thread.Sleep (10); Socket.send (Todatagram (string.valueof (TMP), Inetaddress.getbyname ("127.0.0.1"), 3800)); I++;}Catch(Exception e) {e.printstacktrace (); }

Code added by C # side

Add the Include header file:

Using System.Net;
Using System.Net.Sockets;

In the GUI interface of C #, you need to trigger a button to receive data, such as

private void Button1_Click (object sender, EventArgs e)
{

}

Add Inside:

      Private voidButton1_Click (Objectsender, EventArgs e) {            stringReturndata; byte[] receivebytes; //consolekeyinfo cki = new Consolekeyinfo ();            using(UdpClient udpclient =NewUdpClient (NewIPEndPoint (Ipaddress.parse ("127.0.0.1"),3800)) {IPEndPoint Remoteipendpoint=NewIPEndPoint (Ipaddress.parse ("127.0.0.1"),3800);  while(true) {receivebytes= Udpclient.receive (refremoteipendpoint); Returndata=Encoding.ASCII.GetString (receivebytes); Textbox1.appendtext (Returndata+Environment.NewLine); //Console.WriteLine (returndata);                    floatAngle1 =0; floatAngle2 =0; floatAngle3 =0; floata0 =0; floatA1 =0; floatA2 =0; floatA3 =0; floata4 =0; floatA5 =0; floatA6 =0; floatA7 =0; floatA8 =0; floatA9 =0; intK =0; string[] arr =New string[Ten]; Arr= Returndata.split (New Char[] {' '}, Stringsplitoptions.removeemptyentries);
foreach(stringIincharr) {Maintextbox.appendtext ("Target:"+ K +"s"+ i +"s"+Environment.NewLine); if(k = =1) {Angle1=Convert.tosingle (i); Angle2= Angle1 * (- -) +2219; if(Angle2 <1621) {Angle2=1621; } if(Angle2 > -) {Angle2= -; } angle3= Angle2 *4; A1=Angle2; Trysettarget ((byte) One, (ushort) angle3); } //.........//if(k = =9) {Angle1=Convert.tosingle (i); Angle2= Angle1 * (- -) +2252; if(Angle2 <992) {Angle2=992; } if(Angle2 > -) {Angle2= -; } angle3= Angle2 *4; A9=Angle2; Trysettarget ((byte) A, (ushort) angle3); } k= k +1; } } } }

[Work notes] Socket communication for Java and C #

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.