socketproj - "Linux Gazette.making Linux just a little...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
"Linux Gazette. .. making Linux just a little more fun! " Linux Socket Programming In C++ By Rob Tougher Contents 1. Introduction 2. Overview of Client-Server Communications 3. Implementing a Simple Server and Client 3.1 Server - establishing a listening socket 3.2 Client - connecting to the server 3.3 Server - Accepting the client's connection attempt 3.4 Client and Server - sending and receiving data 4 Compiling and Testing Our Client and Server 4.1 File list 4.2 Compile and test 5. Conclusion 1. Introduction Sockets are a mechanism for exchanging data between processes. These processes can either be on the same machine, or on different machines connected via a network. Once a socket connection is established, data can be sent in both directions until one of the endpoints closes the connection. I needed to use sockets for a project I was working on, so I developed and refined a few C++ classes to encapsulate the raw socket API calls. Generally, the application requesting the data is called the client, and the application servicing the request is called the server. I created two primary classes, ClientSocket and ServerSocket , that the client and server could use to exchange data. The goal of this article is to teach you how to use the ClientSocket and ServerSocket classes in your own
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
applications. We will first briefly discuss client-server communications, and then we will develop a simple example server and client that utilize these two classes. 2. Overview of Client-Server Communications Before we go jumping into code, we should briefly go over the set of steps in a typical client-server connection. The following table outlines these steps: Server Client 1. Establish a listening socket and wait for connections from clients. 2. Create a client socket and attempt to connect to server. 3. Accept the client's connection attempt. 4. Send and receive data. 4. Send and receive data. 5. Close the connection. 5. Close the connection. That's basically it. First, the server creates a listening socket, and waits for connection attempts from clients. The client creates a socket on its side, and attempts to connect with the server. The server then accepts the connection, and data exchange can begin. Once all data has been passed through the socket connection, either endpoint can close the connection. 3. Implementing a Simple Server and Client Now its time to dig into the code. In the following section we will create both a client and a server that perform all of the steps outlined above in the overview. We will implement these operations in the order they typically happen - i.e. first we'll create the server portion that listens to the socket, next we'll create the client
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

socketproj - "Linux Gazette.making Linux just a little...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online