C

From 太極
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

View all TCP sockets currently active
$ netstat --tcp

View all UDP sockets
$ netstat --udp

View all TCP sockets in the listening state
$ netstat --listening

View the multicast group membership information
$ netstat --groups

Display the list of masqueraded connections
$ netstat --masquerade

View statistics for each protocol
$ netstat --statistics

Display all traffic on the eth0 interface for the local host
$ tcpdump -l -i eth0

Show all traffic on the network coming from or going to host plato
$ tcpdump host plato

Show all HTTP traffic for host camus
$ tcpdump host camus and (port http)

View traffic coming from or going to TCP port 45000 on the local host
$ tcpdump tcp port 45000

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 (eg server on my Ubuntu and client on my Windows) 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". The problem is we may need to wait until 4 minutes for avoiding this message. See the solution in here or SO_REUSEADDR option in setsockopt(). That is, we just need to change server.c to add the following
 ...
 int iOption = 1; // Turn on keep-alive, 0 = disables, 1 = enables
 ...
 // Immediately after the declaration of sockfd, we do
 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &iOption,  sizeof(int)) == -1) {
    error("setsockopt");
    exit(1);
 }
  • On Windows, we can use TCPView to see which process is listening on which port, socket status. On Linux, we can use netstat -a command (it gives a long output)
mli@PhenomIIx6:~/Downloads$ sudo netstat -a | grep 51717
tcp        0      0 *:51717                 *:*                     LISTEN
  • 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 (in C)

http://www.prasannatech.net/2008/07/socket-programming-tutorial.html

Server side (http://taichi.selfip.net:81/lang/c/socket/example2/tcpserver.c):

$ ./tcpserver

TCPServer Waiting for client on port 5000
 I got a connection from (127.0.0.1 , 36123)
 SEND (q or Q to quit) : yes

 RECIEVED DATA = Got it.
 SEND (q or Q to quit) : how are you

 RECIEVED DATA = I am fine.
 SEND (q or Q to quit) : q

q
^C
$

Client side (http://taichi.selfip.net:81/lang/c/socket/example2/tcpclient.c):

$ ./tcpclient

Recieved data = yes
SEND (q or Q to quit) : Got it.

Recieved data = how are you
SEND (q or Q to quit) : I am fine.

Example 3 - 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.

Debugging