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

TCP vs UDP

  • TCP is used for services with a large data capacity, and a persistent connection
  • „ UDP is more commonly used for quick lookups, and single use query-reply actions.
  • Some common examples of TCP and UDP with their default ports:
    • DNS lookup UDP 53
    • FTP TCP 21
    • HTTP TCP 80
    • POP3 TCP 110
    • Telnet TCP 23

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

http request

Make http request via telnet

Below, we only input two lines. One is telnet linus.nci.nih.gov 80 and the other is HEAD / HTTP/1.0\n\n. Remember the one carriage character and one line feed at the end of request line. We can change the HTTP method in the 2nd input to GET /HTTP/1.0\n\n to fetch the full page. See the book HTTP: The Definitive Guide and wikipedia.

$ telnet linus.nci.nih.gov 80
Trying 137.187.182.124...
Connected to ncias-p942-v-1.nci.nih.gov.
Escape character is '^]'.
HEAD / HTTP/1.0

HTTP/1.1 200 OK
Date: Thu, 21 Mar 2013 14:47:26 GMT
Server: Apache
Last-Modified: Tue, 12 Mar 2013 13:52:32 GMT
ETag: "302a-4d7ba99db0800"
Accept-Ranges: bytes
Content-Length: 12330
Connection: close
Content-Type: text/html

Connection closed by foreign host.
$

Example 1 - Linux/Unix

http://www.linuxhowtos.org/C_C++/socket.htm. The codes are saved under here.

$ 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 (The code is here):

$ ./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 (The code is here):

$ ./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 - Mimic browser request

The code is based on the post. http://codebase.eu/tutorial/linux-socket-programming-c/. My local copy of [1].

This is another similar post. http://www.binarytides.com/receive-full-data-with-recv-socket-function-in-c/ which teaches how to receive full data with recv socket function in C.

Testing tcpclient.c

$ g++ tcpclient.cpp
$ ./a.out
Setting up the structs...
Creating a socket...
Connect()ing...
send()ing message...
Waiting to recieve data...
1000 bytes recieved :
HTTP/1.1 200 OK
Date: Wed, 20 Mar 2013 14:41:54 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=78ef359985426090:FF=0:TM=1363790513:LM=1363790514:S=UO5PtdM9ETqX6Mm_; expires=Fri, 20-Mar-2015 14:41:54 GMT; path=/; domain=.google.com
Set-Cookie: NID=67=ENMHn5Br-_nZOPNeS04OI0z2Q7sUQEsfnP4xL0VGR66nnRyAv56lvXER7yrM2TESKJv2cwWinH6LOsW3ZuZdAL6-MJzmFNJzJ6ogNpOlvItoEPiAEycHvRrPLK11qDQx; expires=Thu, 19-Sep-2013 14:41:54 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked

8000
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find ex
Receiving complete. Closing socket...
$

Testing tcpserver.c and tcpclient2.c

Server side:

$ ./tcpserver
Setting up the structs...
Creating a socket...
Binding socket...
Listen()ing for connections...
Connection accepted. Using new socketfd : 4
Waiting to recieve data...
37 bytes recieved :
GET / HTTP/1.1
host: www.google.com


send()ing back a message...
Stopping server...
$

Client side (modify tcpclient.c to use IP 127.0.0.1 and port 5556):

$ ./tcpclient2
Setting up the structs...
Creating a socket...
Connect()ing...
send()ing message...
Waiting to recieve data...
10 bytes recieved :
thank you.#▒
Receiving complete. Closing socket...
$

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

Basic(s)

  • Type casting by declaiming explicitly like (float)5 or using suffix like 5f.

Debugging

Tools

  • cmake: On Windows, it will be installed on C:\Program Files (x86)\Cmake 2.8 folder. By default, it is not added to system PATH. The 'Cmake' program will ask for source, binary folders and the Compiler option. After clicking 'configure' and 'generate' buttons, it will create VS solution file and we can double click the solution file to open the project in Visual Studio. In Visual Studio, we can just build the solution (Ctrl + Shift + B). When we want to debug the code, we should 1. right click on project and select property. Change the working directory to the source code (note that .exe file will be generated there). 2. Set the project as the starting project.

OpenGL Programming on Windows

We need to include

#include <gl/gl.h>
#include <gl/glu.h>

And go to project's link properties and enter <opengl32.lib> & <glu32.lib>. Check the directory C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include and C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib

header files are: gl\gl.h and glu.h libraries are: openGL32.lib and GLU32.lib

x64 libs can be found in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64 you can put freeglut lib and header files to those locations to use freeglut with visual studio 2010 when you copy freeglut DLLs to C:\Windows\System32 don’t copy 64 bit DLL to syswow64 this gives a freaky error 0xc000007b when running code. Don’t know what it mean, but if you have freeglut only in system32 you going to be fine.

Resource

Example 1

http://openglbook.com/setting-up-opengl-glew-and-freeglut-in-visual-c/

  1. Download freeglut (freeglut-MSVC-2.8.0-1.mp.zip) & glew (glew-1.9.0-win32.zip)
  2. Copy files include and lib to appropriate location
  3. Copy freeglut.dll to the Project's Release or Debug folder

I don't need Step 5 (Compiler) and Step 6 (Linker).

I keep a copy of the instruction in Evernote.

Example 2

Teapot and Glut shapes (WireTeapot, SolidTeapot, SolidCube & SolidSphere) from http://openglsamples.sourceforge.net/

Example 3 (no Glut, Windows OS only)

http://www.nullterminator.net/opengl32.html

Examples from opengl.org

http://www.opengl.org/sdk/docs/tutorials/