Linux Sockets-Basic Socket links

Source: Internet
Author: User
Tags time limit server port

0x0000 Linux Socket function
bindlistenconnectacceptsendrecvreadwrite
0x0001server not tied up with IP

Error location in bind function

[[email protected] 01]# ./server 191.168.80.151 1588191.168.80.151 : 1588Bind: Cannot assign requested address
Port is already occupied

Error location in bind function

[[email protected] 01]# ./server 192.168.80.151 1588192.168.80.151 : 1588Bind: Address already in use
No this NIC/port number is not going out

Error

Connect: No route to host

Consider if your firewall is blocking this port.

telnet 自己ip 端口

Operation:

1. 关防火墙(不推荐)2. 向防火墙添加这个端口放过的规则
Recv after CTRL + C end Client

End client in new window after recv

0x0002 Server not connected (port number wrong, the other side IP error)

Error:

Connect: Network is unreachable

Consider the server firewall is not a difficult

telnet 对方ip 端口
Second time to the same server port

Error in Connect

[[email protected] 01]# ./client 192.168.80.151 1588192.168.80.151 : 1588Connect: Connection refused
Then kill Clientserver to exit without exception (error not reported)

Client:

[[email protected] 01]# ./client 192.168.80.151 1588192.168.80.151 : 1588Connect: SuccessEnter message to send:^Z[1]+  已停止                  ./client 192.168.80.151 1588[[email protected] 01]# ps   PID TTY          TIME CMD  8114 pts/0    00:00:00 bash 11288 pts/0    00:00:00 client 11290 pts/0    

Server:

Kill Serverclient when connected and exit normally (without reporting error)

Server:

Client:

[[email protected] 01]# ./client 192.168.80.151 1588192.168.80.151 : 1588Connect: SuccessEnter message to send:HelloServerreceived:Enter message to send:HelloServer[[email protected] 01]#
Start client from new session after 0x0003 link is successful

Use the Bind method on both the client and server using the specified port

Client3 connecting IP that does not exist
[[email protected] 03]# ./tcp_client3 192.168.122.1server_port: 192server_ip: INADDR_ANYclient_port: 0Connect: Network is unreachable255.255.255.255:192

Setting multiple IP masks on the same NIC it's not on a network segment. Can you be a generalist?

0x0004 Read Write recv send0x0004-1-1read write Test

Read once after reading a buffer to return immediately, can not read full after the return

Write reads the same content as the client

0x0004-1-2 Delay Write

0X0004-2-1RECV Send Test

The main difference between recv and send is that with a fourth parameter, you can control blocking and non-blocking information.

MSG_DONTROUTE | 不查找表 |MSG_OOB | 接受或者发送带外数据 |MSG_PEEK | 查看数据,并不从系统缓冲区移走数据 |MSG_WAITALL | 等待所有数据 |

The result of recv and send is the same as read write

0x0004-2-2 Delay Send

0x0005 Kernel buffer test

Test Max write Post blocking

Netstat commands and their parameters
-a (all)显示所有选项,默认不显示LISTEN相关-t (tcp)仅显示tcp相关选项-u (udp)仅显示udp相关选项-n 拒绝显示别名,能显示数字的全部转化成数字。-l 仅列出有在 Listen (监听) 的服務状态-p 显示建立相关链接的程序名-r 显示路由信息,路由表-e 显示扩展信息,例如uid等-s 按各个协议进行统计-c 每隔一个固定时间,执行该netstat命令。提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到
setsockopt function
Sets the options for the socket interface. #include <sys/types.h> #include <sys/socket.h>int setsockopt (int sockfd, int level, int optname,const void * Optval, socklen_t Optlen); SOCKFD: A descriptive word that identifies a set of interfaces. Level: The hierarchy of option definitions; Sol_socket, ipproto_tcp, Ipproto_ip, and Ipproto_ipv6 are supported. Optname: Options to be set. Optval: Pointer to the buffer that holds the new value to be set for the option. Optlen:optval the buffer length. 1.closesocket (usually does not close immediately and undergoes time_wait process) want to continue to reuse the Socket:bool breuseaddr=true;setsockopt (S,sol_socket, SO_REUSEADDR, ( Const char*) &breuseaddr,sizeof (BOOL)); 2. If you want a soket that is already in the connected state to force shutdown after calling Closesocket, you do not experience the time_wait process: BOOL Bdontlinger = false;setsockopt (s,sol_socket,so_ Dontlinger, (const char*) &bdontlinger,sizeof (BOOL)); 3. In the Send (), recv () process sometimes due to network conditions and other reasons, the collection can not be expected to proceed, and set the transceiver time: int NNETTIMEOUT=1000;//1 sec//Send time limit setsockopt (Socket,sol_s0cket,so_sndtimeo, (char *) &nnettimeout,sizeof (int));// Receive Time setsockopt (Socket,sol_s0cket,so_rcvtimeo, (char *) &nnettimeout,sizeof (int)); 4. At Send (), the bytes that were actually sent are returned ( synchronization) or bytes sent to the socket buffer (asynchronous), the system default state send and receive once is 8688 bytes (about 8.5K), in the actual process of sending data and receiving a large amount of data, you can set the socket buffer, and avoid send (), recv () Continuous cyclic transceiver:// The receive buffer int nrecvbuf=32*1024;//is set to 32Ksetsockopt (S,sol_socket,so_rcvbuf, (const char*) &nrecvbuf,sizeof (int));// The Send buffer int nsendbuf=32*1024;//is set to 32Ksetsockopt (S,sol_socket,so_sndbuf, (const char*) &nsendbuf,sizeof (int)); 5. If you are sending data, you do not want to experience the performance of a program that is affected by a copy of the system buffer to the socket buffer: int nzero=0;setsockopt (SOCKET,SOL_S0CKET,SO_SNDBUF, (char *) &nzero , sizeof (Nzero)); 6. Same as above in recv () (the default is to copy the contents of the socket buffer to the system buffer): int nzero=0;setsockopt (socket,sol_s0cket,so_ RCVBUF, (char *) &nzero,sizeof (int)); 7. In general, when sending a UDP datagram, you want the data sent by the socket to have broadcast characteristics: BOOL bbroadcast=true;setsockopt (s), Sol_socket,so_broadcast, (const char*) &bbroadcast,sizeof (BOOL)); 8. In the Client connection server process, if the SOCKET in non-blocking mode is in Connect ( ), you can set the Connect () delay until Accpet () is called (This function setting has a significant effect in the non-blocking process and is not very useful in blocking function calls) BOOL bconditionalaccept=true;setsockopt ( S,sol_socket,so_conditional_accept, (const char*) &bconditionalaccept,sizeof (BOOL)); 9. If the data is being sent (send () is not completed, And the data is not sent) and called Closesocket (), previously we generally take the measure is "calmly shut down" shutdown (s,sd_both), but the data is definitely lost, how to set the program to meet the requirements of the specific application ( That is, to let the data sent out after the end of the socket is closed?struct Linger {u_short l_onoff;u_short l_linger;}; Linger m_slinger;m_slinger.l_onoff=1;//(in closesocket () call, but there is no data sent over the time allowed to stay)//if m_slinger.l_onoff=0; then function and 2.) m_slinger.l_linger=5;//(Allowed to stay for 5 seconds) setsockopt (S,sol_socket,so_linger, (const char*) &m_slinger,sizeof ( Linger));
After sizing the kernel buffers with setsockopt

(smaller accidentally)

0x00060x0006.1

Both sides enter read blocking state, unable to transfer data

0x0006.2

Read-write size consistent, can be transmitted normally

The read-write size is inconsistent, and the redundant data ends up blocking the write on the side
(picture same, omitted)

0x0006.3

The first two are normal transceiver
(Omit other pictures)

Third server, client 700 700
Client completed a round of write read server read did not finish it blocked

0x0006.4

The first two are normal transceiver
(Same as 3)

Third server, client 700 700
After client has finished writing, blocking in read server read did not finish, blocking

Linux Sockets-Basic Socket links

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.