socket-programming-examples-c

所属分类:网络编程
开发工具:C++
文件大小:0KB
下载次数:0
上传日期:2020-10-02 12:59:13
上 传 者sh-1993
说明:  C++中的套接字编程示例,
(Socket programming examples in C++,)

文件列表:
LICENSE (18031, 2016-09-17)
echo/ (0, 2016-09-17)
echo/Makefile (525, 2016-09-17)
echo/client.cc (3178, 2016-09-17)
echo/client.h (579, 2016-09-17)
echo/echo-client.cc (754, 2016-09-17)
echo/echo-server.cc (630, 2016-09-17)
echo/server.cc (3609, 2016-09-17)
echo/server.h (535, 2016-09-17)
simple-echo/ (0, 2016-09-17)
simple-echo/Makefile (735, 2016-09-17)
simple-echo/client.cc (2111, 2016-09-17)
simple-echo/server.cc (2343, 2016-09-17)
simple-echo/slow-server.cc (2510, 2016-09-17)
tests/ (0, 2016-09-17)
tests/parseTest.py (1604, 2016-09-17)
tests/test-messages.txt (85638, 2016-09-17)
unix-echo/ (0, 2016-09-17)
unix-echo/Makefile (778, 2016-09-17)
unix-echo/client.cc (2299, 2016-09-17)
unix-echo/client.h (527, 2016-09-17)
unix-echo/server.cc (2579, 2016-09-17)
unix-echo/server.h (530, 2016-09-17)
unix-echo/unix-client.cc (736, 2016-09-17)
unix-echo/unix-client.h (236, 2016-09-17)
unix-echo/unix-echo-client.cc (119, 2016-09-17)
unix-echo/unix-echo-server.cc (118, 2016-09-17)
unix-echo/unix-server.cc (1334, 2016-09-17)
unix-echo/unix-server.h (339, 2016-09-17)

# Socket programming examples in C++ Simple examples that show how to do socket programming in C++. Use > make to compile the code in each directory. ## Simple Echo Server and Client The files `simple-echo/server.cc` and `simple-echo/client.cc` contain a basic echo server and client. The server handles only one client at a time and simply sends back to the client whatever it receives. Run `server` with `client` and see what happens. **Caution**: both the client and the server do not properly check the return values of send() and recv(). This means that long messages may not be echoed properly. Better code is in subsequent examples. This code is here just to show the basics. ## Slow Echo Server To illustrate the poor quality of the code in the simple echo server and client, the file `simple-echo/slow-server.cc` is an echo server that sends replies only one character at a time. While this is not how any server is written, it illustrates what can happen when sending larger messages across the network. Those messages can be split into smaller pieces by TCP, so the a developer cannot assume that a single call to recv() will get the entire message. Run `slow-server` with `client` and see what happens. ## Echo Server and Client This code implements a more functional and complete echo server and client. The client sends messages terminated by a newline, and the server parses until it receives a newline. Both client and server check the return value of send() and recv() and use loops to be sure the entire message is sent or received in each case. This code is located in the `echo` directory. The main programs are in `echo-server.cc` and `echo-client.cc`. The server uses `inet-server.cc` and `inet-server.h` to create Internet sockets using IPv4 addresses. These files in turn rely on `server.cc` and `server.h` for the protocol logic and processing. The client uses `inet-client.cc` and `inet-client.h` to create the client version of an Internet socket. These files likewise rely on `client.cc` and `client.h` for the protocol processing. ## UNIX Echo Server and Client This code converts the echo client and server to use UNIX sockets instead of Internet sockets. UNIX sockets use the file system to identify the socket, rather than IP addresses and ports. In this code, we use `/tmp/unix-socket` as the name of the socket. By using a UNIX socket, this code is restricted to communication with local processes only. In addition, it is not portable to operating systems that do not support UNIX sockets. This code is tested only on Linux, but most of it should be portable to BSD and Mac OS X, with some small changes. This code is located in the `unix-echo` directory. The server uses `unix-echo-server.cc`, `unix-server.cc`, and `unix-server.h`. These latter two files derive a class based on `socket.cc` and `socket.h`. The client uses `unix-echo-client.cc`, `unix-client.cc`, and `unix-client.h`. Likewise, these latter two files derive a class based on `client.cc` and `client.h`. Note that this code reuses the same base classes in `socket.h` and `client.h`, showing how you can swap out a different type of socket, but the protocol logic and processing remains the same.

近期下载者

相关文件


收藏者