简体   繁体   English

Linux中的C ++中的UDP套接字编程

[英]UDP Socket programming in C++ in linux

I am completely new to socket programming and beginner-intermediate in c++. 我完全不熟悉socket编程和c ++中的初学者 - 中级。 I have written a code in c++ and also another application in java. 我用c ++编写了代码,用java编写了另一个应用程序。 The java program will get data from the c++ code which generates data in seconds. java程序将从c ++代码中获取数据,该代码在几秒钟内生成数据。 I am trying to use socket programming to transfer data between these two. 我试图使用套接字编程在这两者之间传输数据。 I have found some code here: Socket programming tutorial . 我在这里找到了一些代码: Socket编程教程 I am using the UDP java client from it. 我正在使用它的UDP java客户端。 However, for the UDP server it only has the C code. 但是,对于UDP服务器,它只有C代码。 I need to embed this code into my C++ app. 我需要将此代码嵌入到我的C ++应用程序中。 therefore I need a c++ version of it. 因此我需要一个c ++版本。 Can anyone help me with this? 谁能帮我这个? or give me a link that gives a tutorial on it. 或者给我一个链接,提供一个教程。 anyhow the C code is: udpserver.c 无论如何C代码是: udpserver.c

You must declare addr_len as socklen_t, not int. 您必须将addr_len声明为socklen_t,而不是int。


That is, the first few lines in main should read something like: 也就是说,main中的前几行应该是这样的:

int sock;
int bytes_read; // <- note how this is now on its own line!
socklen_t addr_len; // <- and this too, with a different type.
char recv_data[1024];

... Leaving everything else as it were. ......留下其他一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM