Discover linux socket programming by example pdf, include the articles, news, trends, analysis and practical advice about linux socket programming by example pdf on alibabacloud.com
Socket sockets can be used not only for network communication and LAN communication but also for local process communication. When you create a socket using the local protocol Pf_unix, the socket is divided into a drain socket, a datagram socket.
returned by the socket () call.The second parameter my_addr is a pointer to the sockaddr data structure. The data structure sockaddr contains information about your address, port, and IP address.The third parameter addrlen can be set to sizeof (structsockaddr ).The following is an example:# Include # Include # Include # Define myport 3490Int main (){Int sockfd;Struct sockaddr_in my_addr;Sockfd =
data size.
Using the Readline function can also be considered as a solution to the problem of sticking to the package, that is, ending with '\ n' as a message. For the server side, you can change the do_service function based on the previous fork program as follows:
C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Void do_echoser (INT conn){Char recvbuf [102
. int accept (int sockfd, void *addr, int *addrlen); The second parameter is a struct-sockaddr pointer to the structure type. Once the connection is successful, you can obtain the client's IP address through this pointer. The third parameter is also sizeof (struct sockaddr). If you do not need to know the IP address of the client side, the second third parameter can write null int send (int sockfd, const void *msg, int len, int flags); int recv (int sockfd, void *buf, int len, unsigned int
# # #socketBackground: The data link layer, the network layer, and the Transport Layer protocol are all implemented in the kernel.Purpose: Socket and XTi (the latter is not used)Function:1. copy the application layer data from the user buffer to the TCP/UDP kernel send buffer to deliver the kernel send data (send function) or copy data from the kernel TCP/UDP receive buffer to the user buffer to read the data2. The application can fine-tune the behavi
Common socket typesProvide connection-oriented, reliable transport services, data error-free, no duplication of transmission,and received in the order in which they were sent.provides no connection service. No error-free guarantees are provided, data may be lost or duplicated, and receive order confusion."Linux network Programming" common
When I write this code, I find that many places are easily mistaken. Select may have an error, return-1.Like whatint fd_isset (int fd,fd_set *fdset), void fd_clr (int fd,fd_set *fdset), void fd_set (int fd,fd_set *fdset); void Fd_zero (int fd,fd_set *fdset);Several of the macros here are incoming pointers, not value passing.The SELECT function is declared as follows:int select (int maxfdp1,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout);Usage:1. First define good fd_
first, introduce the common API of network programming through socket based on TCP protocol
1, if the reader is not very familiar with, you can first read a few blog before I wrote, there are sockets, address structure of understanding, more convenient for readers to understand
Addresses are: 1, http://blog.csdn.net/u011068702/article/details/56479927 2), http://blog.csdn.net/ u011068702/article/details/
addrinfo *hints, struct addrinfo);
Parameter description
Hostname: A host name or address string (IPv4 dotted decimal string or IPv6 16 string)
Services: The service name can be a decimal port number, or it can be a defined service name, such as FTP, HTTP, and so on
Hints: Either a null pointer or a pointer to a ADDRINFO structure in which the caller fills in a hint about the type of information expected to return. For example, if the specified s
Through the Linux network programming-the original socket programming, we know that we can get the link layer packets through the raw sockets and recvfrom (), what is the link Layer Packet we receive ? Link Layer envelope formatMAC head (wired LAN)Note : CRC, PAD can be ignored when group packageOne of the scenarios
I reviewed the Linux socket programming, wrote the simplest server and client programs, and passed the test. The server-side programs adopt the loop and busy waiting mechanism, which will be changed to multithreading and thread pool mechanism later.
Server programs
#include
Client Program
#include
The resource temporarily Unavailable,errno code is often present in Linux for non-blocking socket receive data (Eagain), which indicates that you are using blocking operation in nonblocking mode. This error is returned when the operation is not completed, and the error does not break the socket synchronization, and the next loop will be recv. For non-blocking soc
Tag: input int string cannot main char* argv ready hintCleint:#define _gnu_source 1#include Server#define _gnu_source 1#include Linux Socket Talkclient Talkserver example
There is a recent use of the socket to send and receive data to the client at the same time, because it is an embedded Linux device and requires only one client to connect to the port at a time. Given the savings in system resources, only two threads were created to implement the service-side transceiver data. Directly below the code, the code for the PC on the program, has made detailed comments.
Server.c
, discard this request packet, the result, only B machine meet the conditions, B machine to send A alone ARP reply packet, the answer packet with B's IP corresponding to the MAC address, when a received the reply packet, the IP of B and its corresponding MAC address into the native ARP cache. Viewing the ARP cache table on Linux:ARPViewing the ARP cache table in Windows:arp-aARP Header1. Dest Mac: Destination MAC address2. SRC Mac: Source MAC Address3. Frame type: 0x08064. Hardware type: 1 (Ethe
until the data is read or an error is generated, I maintain a unified socket fd table globally, which is determined by the linux features, because the linux socket fd starts from a very small value, in addition, there will be no duplicates within the same time (it is estimated that there is a thread lock inside). All
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.