Giter Club home page Giter Club logo

socket-programming-examples-c's Introduction

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.

socket-programming-examples-c's People

Contributors

zappala avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.