什么是简单的 C 或 C++ TCP 服务器和客户端示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/662328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:34:14  来源:igfitidea点击:

What is a simple C or C++ TCP server and client example?

c++tcpclient-server

提问by Nick Bolton

I need to quickly implement a very small C or C++ TCP server/client solution. This is simply to transfer literally an array of bytes from one computer to another - doesn't need to be scalable / over-complicated. The simpler the better. Quick and dirty if you can.

我需要快速实现一个非常小的 C 或 C++ TCP 服务器/客户端解决方案。这只是将字节数组从一台计算机传输到另一台计算机 - 不需要可扩展/过于复杂。越简单越好。如果可以的话,又快又脏。

I tried to use the code from this tutorial, but I couldn't get it to build using g++ in Linux: http://www.linuxhowtos.org/C_C++/socket.htm

我尝试使用本教程中的代码,但无法在 Linux 中使用 g++ 构建它:http: //www.linuxhowtos.org/C_C++/socket.htm

If possible, I'd like to avoid 3rd party libraries, as the system I'm running this on is quite restricted. This must be C or C++ as the existing application is already implemented.

如果可能,我想避免使用 3rd 方库,因为我运行它的系统受到很大限制。这必须是 C 或 C++,因为现有应用程序已经实现。

Thanks to emg-2's answer, I managed to make the above mentioned code sample compatible with C++ using the following steps:

感谢emg-2的回答,我设法使用以下步骤使上述代码示例与 C++ 兼容:

Add these headers to both client and server:

将这些标头添加到客户端和服务器:

#include <cstdlib>
#include <cstring>
#include <unistd.h>

In server.c, change the type of clilen to socklen_t.

server.c 中,将 clilen 的类型更改为 socklen_t。

int sockfd, newsockfd, portno/*, clilen*/;
socklen_t clilen;

In client.c, change the following line:

client.c 中,更改以下行:

if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) { ... }

To:

到:

if (connect(sockfd,(const sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)

回答by Bill the Lizard

I've used Beej's Guide to Network Programmingin the past. It's in C, not C++, but the examples are good. Go directly to section 6for the simple client and server example programs.

我过去使用过Beej 的网络编程指南。它是用 C 写的,不是用 C++ 写的,但是例子很好。直接转到第 6 节了解简单的客户端和服务器示例程序。

回答by Nick Bolton

If the code should be simple, then you probably asking for C example based on traditional BSD sockets. Solutions like boost::asioare imho quite complicated when it comes to short and simple "hello world" example.

如果代码应该很简单,那么您可能会要求提供基于传统 BSD 套接字的 C 示例。boost::asio当涉及到简短而简单的“hello world”示例时,imho 之类的解决方案非常复杂。

To compile examples you mentioned you must make simple fixes, because you are compiling under C++ compiler. I'm referring to following files:
http://www.linuxhowtos.org/data/6/server.c
http://www.linuxhowtos.org/data/6/client.c
from: http://www.linuxhowtos.org/C_C++/socket.htm

要编译您提到的示例,您必须进行简单的修复,因为您是在 C++ 编译器下编译的。我指的是以下文件:
http: //www.linuxhowtos.org/data/6/server.c
http://www.linuxhowtos.org/data/6/client.c
来自:http://www。 linuxhowtos.org/C_C++/socket.htm

  1. Add following includes to both files:

    #include <cstdlib>
    #include <cstring>
    #include <unistd.h>
    
  2. In client.c, change the line:

    if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
    { ... }
    

    to:

    if (connect(sockfd,(const sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
    { ... }
    
  1. 将以下包含添加到两个文件中:

    #include <cstdlib>
    #include <cstring>
    #include <unistd.h>
    
  2. client.c 中,更改行:

    if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
    { ... }
    

    到:

    if (connect(sockfd,(const sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
    { ... }
    

As you can see in C++ an explicit cast is needed.

正如您在 C++ 中看到的,需要显式转换。

回答by bayda

try boost::asio lib (http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio.html) it have lot examples.

试试 boost::asio lib ( http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio.html) 它有很多例子。

回答by Erwin Coumans

Although many year ago, clsocket seems a really nice small cross-platform (Windows, Linux, Mac OSX): https://github.com/DFHack/clsocket

尽管很多年前,clsocket 似乎是一个非常好的小型跨平台(Windows、Linux、Mac OSX):https: //github.com/DFHack/clsocket

回答by Guy L

Here are some examples for:

以下是一些示例:

1) Simple
2) Fork
3) Threads

1) 简单
2) 叉
3) 线程

based server:

基于服务器:

http://www.martinbroadhurst.com/server-examples.html

http://www.martinbroadhurst.com/server-examples.html