I. Port
1. 0-1023: Reserved port, used by Super Users
2. 1024-49151: The registered port number
3. 49152-65535: freely available port or dynamic port
Ii. Socket Type
1. SOCK_STREAM (byte stream socket): connection-oriented, reliable full-duplex byte stream. For AF_INET, establish a TCP connection
2. SOCK_DGRAM (datagram socket): the distribution of data is not guaranteed, and the Training Order of sent data is not guaranteed. For AF_INET, establish a UDP connection
3. SOCK_RAW (original socket)
3. Establish a socket
Int fd;
Fd = socket (AF_INET, SOCK_STREAM, 0); // except for SOCK_RAW, the last protocol is set to 0, and AF_INET is set to an interface.
Iv. Application
1. server
Deepfuture @ deepfuture-laptop :~ /Private/mytest $./testtcps
Server wait ....
............................
Server read: myhaspl
Server send: hello
.....
Server read: myhaspl
Server send: hello
2. client
Deepfuture @ deepfuture-laptop :~ /Private/mytest $./testtcpc
Client send ....
Client send: myhaspl
Client read: hello
Deepfuture @ deepfuture-laptop :~ /Private/mytest $./testtcpc
Client send ....
Client send: myhaspl
Client read: hello
Deepfuture @ deepfuture-laptop :~ /Private/mytest $
All content of this blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/
3. source
1) server
C code
# Include
# Include
# Include
# Include
# Include
// Myhaspl
Ssize_t readn (int fd, void * ptr, size_t maxcn) {// read n characters, maxc is the number of reads
Size_t noreadcn, readcn;
Char * buf = ptr;
Noreadcn = maxcn;
While (noreadcn> 0 ){
If (readcn = read (fd, buf, noreadcn) <0) {// read data
If (errno = EINTR) {// before data reading, the operation is interrupted
Perror ("interrupt error ");
Readcn = 0;
}
Else {return-1;} // The error cannot be fixed and an error is returned.
}
Else if (readcn = 0) break; // EOF
Noreadcn-= readcn; // The read is successful. However, if the number of characters to be read is smaller than that of maxc, the read continues because data may be sent over the network.
Buf + = readcn;
If (* buf = 0) break; // exit if you read the end sign of the string. This statement is required; otherwise, an endless loop occurs.
}
Return (maxcn-noreadcn );
}
Ssize_t writen (int fd, void * ptr, size_t maxcn) {// write n characters
Size_t nowritecn, writecn;
Char * buf = ptr;
Nowritecn = maxcn;
While (nowritecn> 0 ){
If (writecn = write (fd, buf, nowritecn) <= 0) {// write data
If (errno = EINTR) {// before data writing, the operation is interrupted
Perror ("interrupt error ");
Writecn = 0;
}
Else {return-1;} // The error cannot be fixed and an error is returned.
}
Nowritecn-= writecn;
Buf + = writecn;
}
Return (maxcn-nowritecn );
}
Int main (void ){
Int fd;
Int addresslen;
Struct sockaddr_in address; // address information structure
Int pid;
Int rc;
Fd_set fdset;
// Create a socket
Fd = socket (AF_INET, SOCK_STREAM, 0); // fd is socket
If (fd =-1) {// error, the type is obtained from errno
Perror ("error"); // perror outputs the parameter first, followed by ":" with a space, followed by the error message (not the error code) corresponding to the errno value, and finally a line break.
}
// Bind to socket fd
Address. sin_family = AF_INET; // IPV4 protocol, AF_INET6 is IPV6
Address. sin_addr.s_addr = htonl (INADDR_ANY); // l indicates 32-bit. htonl can ensure the same byte order of different CPUs.
Address. sin_port = htons (1253); // port number. s indicates 16 bits.
Addresslen = sizeof (address );
Bind (fd, (struct sockaddr *) & address, addresslen); // bind
// Create a socket queue and specify the maximum number of accepted connections
Rc = listen (fd, 32); // receives a maximum of 32 connections and starts listening.
// Int listen (int sockfd, int backlog): 0 -- success,-1 -- Failure
// The kernel maintains a queue in its own process space to track the established connections, but the server process has not taken over the processing or ongoing connections.
If (rc =-1 ){
Perror ("listen error"); // listener failed
Exit (1 );
}
Printf ("server wait... \ n ");
While (1 ){
Struct sockaddr_in clientaddress;
Int address_len;
Int client_sockfd;
Char mybuf [100];
Char * buf = "hello \ n ";
Struct timeval timeout; // timeout struct
// Timeout is 2 seconds
Timeout. TV _sec = 1;
Timeout. TV _usec = 0;
// Set fdset
FD_ZERO (& fdset); // clear fdset
FD_CLR (fd, & fdset); // clear the fd flag
FD_SET (fd, & fdset); // sets the flag.
// Select
If (select (fd + 1, & fdset, NULL, NULL, & timeout) <0 ){
Perror ("select error ");
Fflush (stdout );
}
// Wait for the connection and use a new process or thread to process the connection
Fflush (stdout );
Address_len = sizeof (clientaddress );
If (FD_ISSET (fd, & fdset )){
// If a connection arrives
Client_sockfd = accept (fd, (struct sockaddr *) & clientaddress, & address_len); // client_sockfd can be understood as a file handle and can be operated by read and write operations. Client_address is the client information structure myhaspl
// Fork process to process the connection of each customer
Pid = fork ();
If (pid <0) {// Error
Printf ("error: % s \ n", strerror (errno); // strerror maps errno to an error message string myhaspl
Close (client_sockfd );
Exit (1 );
}
If (pid = 0) {// The sub-process processes the data of each client.
Close (fd); // The Listener resource that the sub-process does not need to process
// Read data myhaspl
Bzero (mybuf, 100 );
Readn (client_sockfd, (void *) mybuf, 100 );
Printf ("\ nserver read: % s", mybuf );
// Send data
Writen (client_sockfd, (void *) buf, strlen (buf) + 1 );
Printf ("\ nserver send: % s", buf );
Close (client_sockfd );
Exit (0 );
}
Else {// parent process
Close (client_sockfd); // The parent process does not process client connections, so it is disabled, but it does not mean that the processing handle of the child process is closed, because the child process inherits the client_sockfd resource myhaspl of the parent process.
}
} Else {
Printf (".");
Fflush (stdout );
}
}
}
2) client
C code
# Include
# Include
# Include
# Include
# Include
// Myhaspl
Ssize_t readn (int fd, void * ptr, size_t maxcn) {// read n characters, maxc is the number of reads
Size_t noreadcn, readcn;
Char * buf = ptr;
Noreadcn = maxcn;
While (noreadcn> 0 ){
If (readcn = read (fd, buf, noreadcn) <0) {// read data
If (errno = EINTR) {// before data reading, the operation is interrupted by the signal myhaspl
Perror ("interrupt error ");
Readcn = 0;
}
Else {return-1;} // The error cannot be fixed and an error is returned.
}
Else if (readcn = 0) break; // EOF myhaspl
Noreadcn-= readcn; // The read is successful. However, if the number of characters to be read is smaller than that of maxc, the read continues because data may be sent over the network.
Buf + = readcn;
If (* buf = 0) break; // exit if you read the end sign of the string. This statement is required; otherwise, the myhaspl will be infinite.
}
Return (maxcn-noreadcn );
}
Ssize_t writen (int fd, void * ptr, size_t maxcn) {// write n characters
Size_t nowritecn, writecn;
Char * buf = ptr;
Nowritecn = maxcn;
While (nowritecn> 0 ){
If (writecn = write (fd, buf, nowritecn) <= 0) {// write data
If (errno = EINTR) {// before data writing, the operation is interrupted
Perror ("interrupt error ");
Writecn = 0;
}
Else {return-1;} // The error cannot be fixed and an error is returned.
}
Nowritecn-= writecn;
Buf + = writecn;
}
Return (maxcn-nowritecn );
}
Int main (void ){
Int fd;
Int addresslen;
Struct sockaddr_in address; // address information structure myhaspl
Int pid;
Char mybuf [100];
Char * buf = "myhaspl \ n ";
Int rc;
Fd = socket (AF_INET, SOCK_STREAM, 0); // create a socket
If (fd =-1) {// error, the type is obtained from errno
Perror ("error"); // perror outputs the parameter first, followed by ":" with a space, followed by the error message (not the error code) corresponding to the errno value, and finally a line break. Myhaspl
}
Printf ("client send... \ n ");
Fflush (stdout );
// Connection
Address. sin_family = AF_INET; // IPV4 protocol. AF_INET6 is IPV6 myhaspl.
Address. sin_addr.s_addr = inet_addr ("127.0.0.1"); // l indicates 32 bits. htonl ensures the same byte order of different CPUs.
Address. sin_port = htons (1253); // port number. s indicates 16-bit myhaspl.
Addresslen = sizeof (address );
Rc = connect (fd, (struct sockaddr *) & address, addresslen); // connection server myhaspl
If (rc =-1) {// rc = 0 succeeded, rc =-1 failed myhaspl
Perror ("connection error ");
Exit (1 );
}
// Send data
Writen (fd, (void *) buf, strlen (buf) + 1 );
Printf ("client send: % s \ n", buf );
// Read data
Bzero (mybuf, 100 );
Readn (fd, (void *) mybuf, 100 );
Printf ("client read: % s \ n", mybuf );
Close (fd );
Exit (0 );
}