1 Network Programming API
(1) The IP address of the network layer can uniquely identify the host in the network, and the transport layer uniquely identifies the application in the host through Protocol + port. This has been the process of identifying the network using triples (addresses, protocols, ports).
(2) Socket---> socket (vulgar haha), as a file descriptor, Linux basic philosophy of all documents, then can also read and write off such a habitual operation. Yes, almost.
(3) To see the TCP interaction process
OK, look at each function.
First: int socket (int domain,int type,int protocol)
Parameter description:
Domain: Protocol field, which determines the Socekt address type
The Type:socket type.
Protocol: Specifies the protocol.
Call this function to return the socket descriptor in the protocol family space, but there is no specific address, so we give it address there is the Le bind
Second: int bind (int socketfd,const struct sockaddr *addr,socklen_t addrlen)
Parameter introduction
Addr: The protocol address of the SOCKFD to the semi-butyl. differs depending on the address protocol family when the Socekt is created
Third and fourth: Listen Connect
Listen to the client and then the link is scattered
Fifth one: Accept
When the client sends a link request, it begins to accept it. If you accept it, you can start communicating.----> for IO operations.
Note: Accept succeeds, the return value is automatically generated by the kernel a new descriptor oh.
Sixth, group sixth.
Read () write ()
Recv () Send ()
Readv () Writev ()
Recvmsg () sendmsg ()
Recvfrom () sendto ()
The read read success will put back the number of bytes actually read. 0 sign read to me at the end of the meeting.
Another example
1//client.cpp2 #include<stdio.h>3 #include<stdlib.h>4 #include<string.h>5 #include<errno.h>6 #include<sys/types.h>7 #include<sys/socket.h>8 #include<netinet/in.h>9 #include<arpa/inet.h>Ten #include<unistd.h> One #define MAXLINE 4096 A -int main (int argc, char**argv) { - int sockfd, n; theChar recvline[4096], sendline[4096]; - struct sockaddr_in servaddr; - - if(ARGC! = 2){ +printf"usage:./client <ipaddress>\n"); - return0; + } A at if((SOCKFD = socket (af_inet, sock_stream, 0)) <0) { -printf"Create socket Error:%s (errno:%d) \ n", Strerror (errno), errno); - return0; - } - -memset (&servaddr, 0, sizeof (SERVADDR)); inservaddr.sin_family =af_inet; -Servaddr.sin_port = htons (6666); to if(Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <=0) { +printf"Inet_pton error for%s\n", argv[1]); - return0; the } * $ if(Connect (SOCKFD, (struct sockaddr*) &servaddr, sizeof (SERVADDR)) <0) {Panax Notoginsengprintf"Connect Error:%s (errno:%d) \ n", Strerror (errno), errno); - return0; the } + Aprintf"send msg to server: \ n"); theFgets (Sendline, 4096, stdin); + if(Send (SOCKFD, Sendline, strlen (Sendline), 0) <0) { -printf"Send msg Error:%s (errno:%d) \ n", Strerror (errno), errno); $ return0; $ } - Close (SOCKFD); - return0; the}
View Code
1 #include<stdio.h>2 #include<stdlib.h>3 #include<string.h>4 #include<errno.h>5 #include<sys/types.h>6 #include<sys/socket.h>7 #include<netinet/in.h>8 #include<unistd.h>9 Ten #define MAXLINE 4096 One Aint main (int argc, char**argv) { -int LISTENFD, connfd;//here are two of the sockets that are created to create a socket descriptor after a connection is established Fuha - struct sockaddr_in servaddr; theChar buff[4096]; - int n; - - if((LISTENFD = socket (af_inet, sock_stream, 0)) = =-1 ){ +printf"Create socket Error:%s (errno:%d) \ n", Strerror (errno), errno); - return0; + } A atmemset (&servaddr, 0, sizeof (SERVADDR)); -servaddr.sin_family =af_inet; -SERVADDR.SIN_ADDR.S_ADDR =htonl (inaddr_any); -Servaddr.sin_port = htons (6666); - - if(Bind (LISTENFD, (struct sockaddr*) &servaddr, sizeof (servaddr)) = =-1){ inprintf"bind socket Error:%s (errno:%d) \ n", Strerror (errno), errno); - return0; to } + - if(Listen (LISTENFD, 10) = =-1){ theprintf"Listen socket Error:%s (errno:%d) \ n", Strerror (errno), errno); * return0; $ }Panax Notoginseng -printf"======waiting for client ' s request======\n"); the while(1){ + if((CONNFD = Accept (LISTENFD, (struct sockaddr*) null, null)) = =-1){ Aprintf"Accept Socket Error:%s (errno:%d)", Strerror (errno), errno); the Continue; + } -n =recv (CONNFD, Buff, MAXLINE, 0); $Buff[n] =' /';//Watch out for the output string, and add this thing. $printf"recv msg from client:%s\n", buff); - Close (CONNFD); - } the Close (LISTENFD); - return0;Wuyi}
View Code
1 All:server Client2 SERVER:SERVER.O3g++-G-o server server.o4 CLIENT:CLIENT.O5g++-G-o client client.o6 Server.o:server.cpp7g++-G-C Server.cpp8 Client.o:client.cpp9g++-G-C client.cppTen Clean:all OneRM All
View Code
First run the./server and then./client 127.0.0.1 as shown
Ok warmed up, what byte order and so on the next article in summary le. Come on....
Network Programming in Linux <1>