Socket programming --- Seng Functions & Recv Functions

Source: Internet
Author: User
Tags socket error

1. Send Function

Function prototype: int send (socket S, char * Buf, int Len, int flags );

Function: both client and server applications use the send function to send data to the other end of the TCP connection. The client program generally uses the send function to send requests to the server, while the server uses the send function to send responses to the client program.

Parameter 1: Specify the sender socket descriptor;

Parameter 2: buffer zone for storing data to be sent by the application;

Parameter 3: the number of bytes of the data to be sent;

Parameter 4: generally set to 0.


Synchronize the execution process of the send function of the socket. When this function is called, send first compares the length of the data to be sent Len and the length of the sending buffer of socket S (because the data to be sent is copied to the sending buffer of socket s, note that not sending transmits the data in the sending buffer of S to the other end of the connection, but the Protocol. Sending only copies the data in the Buf to the remaining space of the sending buffer of S):

1. If Len is longer than the sending buffer length of S, this function returns socket_error;

2. if Len is smaller than or equal to the length of the sending buffer of S, send first checks whether the protocol is sending data in the sending buffer of S. If yes, wait for the Protocol to finish sending the data, if the Protocol has not started sending data in the sending buffer of S or there is no data in the sending buffer of S, send will compare the remaining space in the sending buffer of S and Len:

(I) If Len is larger than the size of the remaining space, send will wait for the Protocol to finish sending data in the s sending buffer;

(Ii) If Len is smaller than the size of the remaining space, send only the data in the Buf to the remaining space.

3. if the send function successfully copies the data, the actual number of bytes of copy is returned. If an error occurs during sending data copy, the send function returns socket_error; if the network is disconnected when sending data while waiting for the Protocol to send data, the send function also returns socket_error.

Note: After the send function successfully copies the data in the Buf to the remaining space of the s sending buffer, it returns the data, but the data is not necessarily uploaded to the other end of the connection immediately. If a network error occurs during subsequent transmission, the next socket function will return socket_error. (Each socket function except send must wait for the data in the socket sending buffer to be transmitted by the protocol before execution. If a network error occurs while waiting, then the socket function will return socket_error)

Ii. Recv Functions

Function prototype: int Recv (socket S, char * Buf, int Len, int flags)

Function: both client and server applications use the Recv function to receive data from the other end of the TCP connection.

Parameter 1: Specify the receiver socket descriptor;

Parameter 2: specify a buffer to store the data received by the Recv function;

Parameter 3: Specify the Buf length;

Parameter 4: generally set to 0.


Execute the Recv function of the synchronous socket: when the application calls the Recv function, the Recv waits for the data in the s sending buffer to be transmitted by the Protocol,

If a network error occurs when the Protocol sends data in the sending buffer of S, the Recv function returns socket_error;

If there is no data in the sending buffer of s or the data is successfully sent by the protocol, Recv first checks the receiving buffer of socket S. If there is no data in the receiving buffer or the Protocol is receiving data, so Recv will always be
Wait until the Protocol receives the data;

When the Protocol receives the data, the Recv function copies the data in the s receiving buffer to the Buf (note that the data received by the protocol may be larger than the length of the Buf, in this case, you need to call the Recv function several times to copy the data in the s receiving buffer. The Recv function only copies data, and the protocol is used to actually receive data). The Recv function returns the actual number of bytes of copy;

If a Recv error occurs during copy, socket_error is returned. If the Recv function is interrupted while waiting for the Protocol to receive data, 0 is returned.


Iii. Simple Example

Send a GET request to the Web server using the send function, and use the Recv function to receive the data returned from the Web server to Shanghai.

#include<stdio.h>#include<winsock2.h>#include<windows.h>#pragma comment(lib, "ws2_32.lib")using namespace std;typedef struct{char line[256];char head[256];//char body[256];}message;int main(){int num;SOCKET s;WSADATA wsa;struct sockaddr_in serv;message req;char sndBuf[1024], rcvBuf[2048];WSAStartup(MAKEWORD(2, 1), &wsa);if ((s = socket(AF_INET, SOCK_STREAM, 0))<0){perror("socket error!");exit(1);}memset(&serv, 0, sizeof(serv));serv.sin_family = AF_INET;serv.sin_port = htons(80);serv.sin_addr.S_un.S_addr = inet_addr("115.239.210.26");//BING 70.37.92.127//BAIDU 115.239.210.26//baidu api 180.149.132.118//google 74.125.31.99//sina  59.175.132.113//163   222.243.110.164//taobao   218.75.155.151//jd    116.211.94.1if ((connect(s, (struct sockaddr *)&serv, sizeof(serv)))<0){perror("connet error!");exit(1);}memset(sndBuf, 0, 1024);memset(rcvBuf, 0, 2048);strcpy(req.line, "GET /index.html  HTTP/1.0\r\n");strcat(sndBuf, req.line);strcpy(req.head, "\r\n");strcat(sndBuf, req.head);puts(sndBuf);  if ((num = send(s,sndBuf,1024, 0))<0)   {perror("send error!");exit(1);}puts("send success!\n");do{if ((num = recv(s, rcvBuf, 2048, 0))<0){perror("recv error!");exit(1);}else if (num>0){printf("%s", rcvBuf);memset(rcvBuf, 0, 2048);}} while (num>0);puts("\nread success!\n");closesocket(s);system("pause");WSACleanup();return 0;}


Address: http://blog.sina.com.cn/s/blog_8c7bf1970100zmtf.html

Socket programming --- Seng Functions & Recv Functions

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.