Socket Programming Introduction

table of Contents

This article is the study notes, non-depth study, no codes, such as non-required, please forgive me.

Oh, here have taken good: SOCKET Detailed programming for Linux


background

It took a long time (about a week, I was too much garbage) read the article in English: Bee'j Guide to Network Programming [1] . There is also a book: "TCP / IP Sockets in C - Practical Guide for Programmers" and a PPT: "Introduction to Sockets Programming in C using TCP / IP", but the latter two have not read it wants, but also older, after all, I want to use is IPv6, rather than IPv4. [ Update , now that the second edition of the book has been added to the contents of IPv6, Chinese translation: TCP / IP Sockets programming (C language) ]
In fact, the undergraduate has learned book network, the network is not the most important ISO / OSI, but TCP / IP, after all, according to the teacher said, ISO / OSI is a group of drunken people want to come out, corresponding to the seven dwarfs [2] and, finally, late in the de facto standard for TCP / IP, even by ISO / OSI came, also failed because too complicated.

basis

In TCP / IP, the abstraction layers of a package, the lower layer is not visible to the upper transparent upper layer portion is lower data packet format, the header is some control information.
TCP/IP
Socket Programming located below the application layer, transport layer above. Stream Socket and divided into Datagram Socket, in fact, correspond to the two protocols in the transport layer, TCP and UDP. But the problem comes, if it is DCTCP such advanced stuff is how do it? Talk about it later.
There are C / S structure, in fact, two architectures: P2P and C / S have advantages and disadvantages of it, but this article only involve C / S, B / S is a special case of the C / S, P2P I have not studied . C / S structure is similar to the following:
Client-Server Interaction
By the way, the author is doing * nix platform, Windows only mention the following:Winsock.h

Process

Socket programming process is not really complicated.
Socket Procedure
In this way the whole process came out, that is, to create Socket, then bind (bind), in fact, connect () can be used instead bind (), but the difference is, bind () bind to port, and connect do not care which port to use, of course, the parameters passed it, connect () will help programmers find an unbound port binding. The server listens for connection requests for the port, and the rest is transmitted, received data, and noted here Stream Socket (TCP connection) and Datagram Socket (using the connectionless UDP) transmission and reception functions of use are different. Finally, do not forget to close the Socket.
Functions
In fact, the rest of the main function of which is to see the various parameters are needed, like how to use, in the penultimate chapter of the article to the man Pages and the , not posted. Theoretically below should give a Client / Server program, but I felt no need. Down my connection to [1: 1] in the past, there are many, not posted.
But what can be listed using the largest number of header files.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>  	
 	
#include <unistd.h>
#include <sys/socket.h>	
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

If you have time, I'm fine Arts more time to come back to add.

reference


  1. Beej's Guide to Network Programming ↩︎ ↩︎

  2. OSI model exactly how many people fooled? ↩︎

Guess you like

Origin www.cnblogs.com/basilguo/p/basic-socket-programming-in-C.html