Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report