Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the relationship between Java socket Communications and C++?

Shulou Source: shulou.com Published: 2022-06-02 12:40:22 10月04日 Update

What is the relationship between Java socket communications and C++? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The communication between c++builer and Java socket is often used in programming, and many people still have a lot of problems to solve. Let's take a look at how to solve the related problems between Java socket communications and C++. I hope you all have some understanding.

Both C++builer and Java provide feature-rich socket controls / classes. Here we will implement the socket communication between c++builer and Java, which enables both parties to send messages to each other. In C++builer, we use ServerSocket control as the server side and Socket class as the client in Java socket communication. All that are sent to each other in this example are structures, which are defined in C++builer as:

Typedef struct {int int1; float f; char ch [20]; double d;} TMyMsg

The sending part of Java is directly used by DataOutputStream's writeXXX method, of course, it can also be converted into byte [] by inner class.

The source code is as follows:

C++builer server-side Unit1.cpp

# include

# pragma hdrstop

# include "Unit1.h"

# include

# pragma package (smart_init)

# pragma resource "* .dfm"

TForm1 * Form1

_ _ fastcall TForm1::TForm1 (TComponent* Owner)

: TForm (Owner)

{

}

Void _ _ fastcall TForm1::Button1Click (TObject * Sender)

{

ServerSocket1- > Active = true

}

Void _ _ fastcall TForm1::BtnSendClick (TObject * Sender)

{

/ / the data sent should be converted in byte order.

TMyMsg sendmsg

Sendmsg.int1 = htonl (30)

Sendmsg.f = ntohf (12.345)

Strcpy (sendmsg.ch, "Test data")

Sendmsg.d = ntohd (67.890)

ServerSocket1- > Socket- > Connections [0]-> SendBuf (& sendmsg)

Sizeof (TMyMsg))

}

Void _ _ fastcall TForm1::FormShow (TObject * Sender)

{

MemoSend- > Lines- > Add ("int: 30")

MemoSend- > Lines- > Add ("float: 12.345")

MemoSend- > Lines- > Add ("char [20]: test data!")

MemoSend- > Lines- > Add ("double: 67.890")

}

Void _ fastcall TForm1::ServerSocket1ClientRead (TObject

* Sender

TCustomWinSocket * Socket)

{

/ / received data byte order conversion

TMyMsg remsg

Socket- > ReceiveBuf (& remsg,sizeof (TMyMsg))

Int int1 = ntohl (remsg.int1)

Float f = ntohf (remsg.f)

Char * ch = new char [20]

Strcpy (ch,remsg.ch)

AnsiString sch = StrPas (ch)

Double d = ntohd (remsg.d)

Delete [] ch

MemoRec- > Lines- > Add (int1)

MemoRec- > Lines- > Add (f)

MemoRec- > Lines- > Add (sch.Trim ())

MemoRec- > Lines- > Add (d)

}

After reading the above, have you mastered the relationship between Java socket communications and C++? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Communication between data problems caching methods content bytes controls more servers services testing helpless for this information functions reasons both parties customers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Linux Redmi Shulou Information