Googolflex!!
  • Home
  • About
  • Contracting

Recent Posts

  • Sprint’s new “Simply ‘Almost’ Everything®” Plans
  • CSS Changes in Flex 4
  • Dotted Underline LinkButton (Flex)

About The Author : jwd

This is John Dusbabek's tech blog. John is a software engineer and Flex developer in Provo, UT, where he lives with his lovely wife and four sons.

Recent Comments

  • Nikos on Flex: Binding to an Interface
  • Iain Hosking on Apache mod_proxy_balancer: No Protocol handler was valid

Archive for C++

Aug
18

Simple Client in C++

Posted by: jwd | Comments (0)

Although a TCP client is fundamentally different from a TCP server, setting up the socket is very much the same. For a server, we called these methods in this order:

socket()→bind()→listen()→accept(), followed by sequences of read() and write().

For a client we will call these methods in this order:

socket()→connect(), followed by sequences of write() and read() depending on what your server does.

To illustrate, I will create a simple HTTP client in C++, which has the following usage:

client www.googolflex.com

It will then make a connection on port 80 of the server specified at the command line, send an HTTP 1.1 request, and then dump the response to the console.

It uses the following variables:

int sock;
struct sockaddr_in client;
int PORT = 80;

The first thing to do is initialize the socket structure, for this we need the IP address of the host. You can use the gethostbyname(char *) function to do that, which passes back a hostent structure. I have encapsulated the socket setup into a method, which shows how to extract the necessary values from the hostent struct, here:

void setupSocket(char* hostname) {
	struct hostent * host = gethostbyname(hostname);

	if ( (host == NULL) || (host->h_addr == NULL) ) {
		cout << "Error retrieving DNS information." << endl;
		exit(1);
	}

	bzero(&client, sizeof(client));
	client.sin_family = AF_INET;
	client.sin_port = htons( PORT );
	memcpy(&client.sin_addr, host->h_addr, host->h_length);

	sock = socket(AF_INET, SOCK_STREAM, 0);

	if (sock < 0) {
		cout << "Error creating socket." << endl;
		exit(1);
	}
}

If this method executes correctly, then we have a socket, and an address structure that we can use to call connect() with. Here is my encapsulating method:

void connectToHost(char* hostname) {
	if ( connect(sock, (struct sockaddr *)&client, sizeof(client)) < 0 ) {
		close(sock);
		cout << "Could not connect to " << hostname << endl;
		exit(1);
	}
}

Finally, we can call send()/write() and read()/recv(). This is where your protocol would be implemented, if you were to modify this for use with something other than simple HTTP requests. Here my sendRequest() and getResponse() methods:

void sendRequest(char* hostname) {
	string request = "GET / HTTP/1.1\r\nHost: ";
	request+= string(hostname);
	request += "\r\n\r\n";

	if (send(sock, request.c_str(), request.length(), 0) != (int)request.length()) {
		cout << "Error sending request." << endl;
		exit(1);
	}
}
void getResponse() {
	char cur;
	while ( read(sock, &cur, 1) > 0 ) {
		cout << cur;
	}
}

In main() I call these four methods in sequence, passing in the hostname parameter where needed. And that’s it. The full source is included at the end of the post.

Socket connections really aren’t that difficult once you’ve done it several times. Next I will be demonstrating how to set up a simple client in Flex using a client. Many scripting languages have abstracted away much of the difficulty surrounding socket connections (as we will see with Flex) leaving you to focus on implementing your protocol.

Download the source for simpleclient here. It can be compiled as follows:

g++ -Wall -o client simpleclient.cpp
Categories : Architecture, C++, client/server
Comments (0)
Jul
28

Single Threaded Server in C++

Posted by: jwd | Comments (0)

The versatility of the client-server architecture has always fascinated me. You can write a server once, and plug in whatever protocol you happen to be implementing. The same goes for clients.

I’m going to present here a simple server I implemented in C++. I guess if I had to give a name to the protocol I implemented, I would call it the “Echo Protocol”. There’s only one part of the code I’m going to comment on specifically in this article, and that is the process() method.

There are ample sources on the web and otherwise explaining the use of the socket, bind, listen, and accept methods – which are the basis for socket communication in C and C++.

One book I would recommend for someone serious about network programming (though it hardly needs my recommendation) is UNIX Network Programming, Third Edition by W. Richard Stevens, Bill Fenner and Andrew Rudoff. If you do read this book, be aware that they use wrapper functions throughout the book (i.e. Bind(), Listen()) to add a measure of error checking (see section 1.4 for their explanation). The wrapper functions have the same signature as the corresponding BSD functions, just make them lowercase.

Here is the code for my process() method:

void process() {
	// Read the request
	//------------------
	string request = &quot;&quot;;

	while(! isRequestDone(request) ) {
		// Read from the client
		//-----------------------------
		char buf;
		bzero(&amp;buf, 1);
		nread = recv(client_fd, &amp;buf, 1, 0);
		request += buf;

		if (nread &lt; 0) {
			if (errno == EINTR)
				continue;// Continue reading
			else
				break;	// No more
		}
		else if (nread == 0) {
			break;		// Nothing left to read
		}
	}

	// Do something with the request
	//------------------------------

	// Send the response:
	//------------------------------
	string response = request;

	int num_b = response.length();
	for (int i = 0; i &lt; num_b; i++) {
		char cur = response[i];
		send(client_fd, &amp;cur, 1, 0);
	}

	// Close the connection here
	close(client_fd);
}

This method is where you would implement the protocol (be it HTTP, or what have you). A crucial step in any interesting protocol would be parsing out the request (something you might do where I have the comment “Do something with the request”) and using that information to construct the response. Sometimes the request parsing is handled while the request is being received. I wanted to keep this example simple (so you’ll also notice I’m receiving and sending only 1 byte at a time… which involves a lot of overhead and I wouldn’t recommend for high load servers sending and receiving larger messages). You could increase the number being read at a time by increasing the size of buf and/or cur.

You can download the complete code for this server here: C++ single threaded server

You can compile this code on Linux with the following command:

g++ -Wall  -o bin/server single_threaded_server.cpp

Linux and Windows both use BSD sockets, but I haven’t tried compiling this code on Windows without using g++ on cygwin. You’re on your own there.

This post is part of a series, next I’m going to post a simple C++ client. Then I’m going to present a threaded server architecture, and finally I’m going to post a Flex chat client and implement a simple chat protocol. Stay tuned.

Categories : Uncategorized
Comments (0)

Search

Feedburner

Subscribe to

Get the latest updates delivered via email

Calendar

September 2010
M T W T F S S
« Jul    
 12345
6789101112
13141516171819
20212223242526
27282930  

Archives

  • July 2010 (1)
  • June 2010 (2)
  • May 2010 (1)
  • February 2010 (11)
  • January 2010 (3)
  • December 2009 (5)
  • November 2009 (1)
  • August 2009 (8)
  • July 2009 (8)
  • May 2009 (4)
  • April 2009 (1)
  • March 2009 (6)
  • January 2009 (1)
  • November 2008 (4)
  • October 2008 (5)
  • September 2008 (1)
  • August 2008 (5)
  • July 2008 (1)
  • June 2008 (2)
  • May 2008 (8)
  • April 2008 (5)
  • March 2008 (2)
  • February 2008 (3)
  • January 2008 (1)
  • December 2007 (6)
  • November 2007 (9)
  • October 2007 (1)
  • September 2007 (2)

Categories

Tag Cloud

adobe apache Architecture book review C++ centos client server architecture Custom Components database Design error message fedora flash catalyst flex Flex 3 Flex 4 fms iis 6 Interaction Design load balancing master-master master-slave mod_proxy_balancer Monkey Patching MySQL no protocol p2p peer to peer Perl PHP Red5 regex replication self registration selinux Shell Scripting shortcut manager skins socket policy file sockets states stored procedures stratus tools workflow

Coworkers

  • Casey Jackman
  • Sean Murphy

Family

  • Emily & CJ
  • Family Blog
  • Gary Dusbabek

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

RSS FlexExamples

  • Styling the text selection format on a Spark TextArea control in Flex 4
  • Setting the scale mode on a Spark Image control in Flex Hero
  • Setting the fill mode on a Spark Image control in Flex Hero
  • Setting a bitmap image fill on a Spark Form container in Flex Hero
  • Setting a bitmap image fill on a Spark FormHeading control in Flex Hero

Spam Blocked

847 spam comments
blocked by
Akismet

Sponsored Links

JUICE Chat

BYU Adobe Users Group


Copyright © 2010 All Rights Reserved
Flexx Theme by iThemes
Powered by WordPress