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

时间:2022-09-06 21:32:35

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.

如果可能的话,我希望避免第三方库,因为我正在运行的系统非常受限制。这必须是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.

在服务器上。c,将clilen的类型改为socklen_t。

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

In client.c, change the following line:

在客户端。c,更改如下一行:

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

To:

:

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

5 个解决方案

#1


40  

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

我以前使用过Beej的网络编程指南。它是用C写的,不是c++,但是例子很好。直接访问第6部分,了解简单的客户机和服务器示例程序。

#2


13  

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

如果代码应该很简单,那么您可能需要基于传统BSD套接字的C示例。例如boost::asio对于简单的“hello world”示例来说非常复杂。

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:

    在客户端。c,改变线:

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

    to:

    :

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

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

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

#3


7  

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)它有很多示例。

#4


2  

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

#5


1  

Here are some examples for:

以下是一些例子:

1) Simple
2) Fork
3) Threads

1)简单的2)Fork线程

based server:

基于服务器:

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

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

#1


40  

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

我以前使用过Beej的网络编程指南。它是用C写的,不是c++,但是例子很好。直接访问第6部分,了解简单的客户机和服务器示例程序。

#2


13  

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

如果代码应该很简单,那么您可能需要基于传统BSD套接字的C示例。例如boost::asio对于简单的“hello world”示例来说非常复杂。

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:

    在客户端。c,改变线:

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

    to:

    :

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

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

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

#3


7  

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)它有很多示例。

#4


2  

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

#5


1  

Here are some examples for:

以下是一些例子:

1) Simple
2) Fork
3) Threads

1)简单的2)Fork线程

based server:

基于服务器:

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

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