C

From 太極
Revision as of 15:35, 3 March 2013 by Brb (talk | contribs)
Jump to navigation Jump to search

Socket programming

Terms

There are two types of address domains.

  • the unix domain for two processes which share a common file system, and
  • the Internet domain for any two hosts on the Internet.

The symbol constant AF_UNIX is used for the former, and AF_INET for the latter.

There are two types of sockets.

  • a stream socket in which characters are read in a continuous stream as if from a file or pipe, and
  • a datagram socket, in which messages are read in chunks.

The two symbolic constants are SOCK_STREAM and SOCK_DGRAM.

There are two types of ports:

  • well known Ports | those that rarely change overtime. For instance, servers that provide mail, �file transfer, remote login,

etc.

  • dynamic ports | typically used only for the life of a process. For instance, pipes can be implemented using message passing

Examples of servers:

  • mail server
  • login server
  • file server
  • web server
  • streaming server

General Idea

The steps involved in establishing a socket on the client side are as follows:

  1. Create a socket with the socket() system call
  2. Connect the socket to the address of the server using the connect() system call
  3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

The steps involved in establishing a socket on the server side are as follows:

  1. Create a socket with the socket() system call
  2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
  3. Listen for connections with the listen() system call
  4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
  5. Receive and send data by using read() and write().

Internet Hearsay

Example 1 - Linux/Unix

http://www.linuxhowtos.org/C_C++/socket.htm

$ gcc server.c -o server
$ gcc client.c -o client
$ ./server 51717
$ ./client 192.168.0.21 51717
Please enter the message: Who are you?
I got your message

where 192.168.0.21 is the ip address on the server. The server side will show

Here is the message: Who are you?

If everything works correctly, the server will display your message on stdout, send an acknowledgement message to the client and terminate. The client will print the acknowledgement message from the server and then terminate.

  • We can actually run the client on a different machine although we can also run both client and server on the same machine.
  • Once we use the port (51717) one time, we can not use the same port to run it again??? The screen shows an error "ERROR on binding: Address already in use"
  • We can choose any port number between 2000 and 65535.
  • If we use 51717 port for example, the server will open that port. But once the program is finished, the port will be closed immediately. Use linux command netstat -lp --inet to check which ports are opened.

Similar examples:

Example 2 - Windows socket (almost implies C++)

Windows programming

Resource

Difference between Win32 project and CLR (common language runtime) project

See here.

A Win32 project is used if you want to end up with a DLL or a Win32 application usually using the bare WinAPI. A CLR project is used to create C++/CLI project, i.e. to use C++/CLI to target the .NET platform.

The main difference between projects is what Visual Studio comes up with in terms of pre-created files. A windowed Win32 application for example (what you get when you choose Win32 project, but not a DLL) is created with a file for resources (menus, acceleators, icons etc.) and some default code to create and register a window class and to instantiate this window.

Quoted from http://en.wikipedia.org/wiki/Common_Language_Runtime

The Common Language Runtime (CLR) is the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as Just-in-time compilation, the compiled code is converted into machine instructions that, in turn, are executed by the computer's CPU. The CLR provides additional services including memory management, type safety and exception handling. All programs written for the .NET framework, regardless of programming language, are executed by the CLR. It provides exception handling, garbage collection and thread management. CLR is common to all versions of the .NET framework.

The CLR is Microsoft's implementation of the Common Language Infrastructure (CLI) standard.

Difference between Win32, MFC and .NET

http://stackoverflow.com/questions/821676/how-do-i-decide-whether-to-use-atl-mfc-win32-or-clr-for-a-new-c-project

Using the CLR will provide you with the most expressive set of libraries (the entire .NET framework), at the cost of restricting your executable to requiring the .NET framework to be installed at runtime, as well as limiting you to the Windows platform (however, all 4 listed technologies are windows only, so the platform limitation is probably the least troublesome).

However, CLR requires you to use the C++/CLI extensions to the C++ language, so you'll, in essense, need to learn some extra language features in order to use this. Doing so gives you many "extras," such as access to the .net libraries, full garbage collection, etc.

Using Win32 directly provides the smallest executables, with the fewest dependencies, but is more work to write. You have the least amount of helper libraries, so you're writing more of the code.

Win32 is the raw, bare-metal way of doing it. It's tedious, difficult to use, and has alot of small details you need to remember otherwise things will fail in relatively mysterious ways.

MFC builds upon Win32 to provide you an object oriented way of building your application. It's not a replacement for Win32, but rather an enhancement - it does alot of the hard work for you.

System.Windows.Forms (which is what I assume you meant by CLR) is completely different, but has large similarities to MFC from its basic structure. It's by far the easiest to use, but requires the .NET framework, which may or may not be a hindrance in your case.