send function(Windows)

SOCKET

實(shí)質(zhì)是一個內(nèi)核對象的句柄. 創(chuàng)建一個socket,就是在系統(tǒng)內(nèi)部創(chuàng)建了一個內(nèi)核對象.

send

The Windows Sockets send function sends data on a connected socket.

int send (SOCKET s, const char FAR * buf, int len, int flags); 

Parameters

  • s [in]
    A descriptor identifying a connected socket.
  • buf [in]
    A buffer containing the data to be transmitted.
  • len [in]
    The length of the data in buf.
  • flags [in]
    An indicator specifying the way in which the call is made.

Remarks

  • outgoing data
    The send function is used to write outgoing data on a connected socket.

  • send completion
    The successful completion of a send does not indicate that the data was successfully delivered.
    If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in a nonblocking mode. On nonblocking stream oriented sockets, the number of bytes written can be between 1 and the requested length, depending on buffer availability on both client and server machines.

  • message data is too long
    For message-oriented sockets, care must be taken not to exceed the maximum packet size of the underlying provider, which can be obtained by using getsockopt to retrieve the value of socket option SO_MAX_MSG_SIZE.
    If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned, and no data is transmitted.

Return Values

If no error occurs, send returns the total number of bytes sent, which can be less than the number indicated by len for nonblocking sockets.
Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Error Codes

  • WSANOTINITIALISED
    A successful WSAStartup must occur before using this function.
  • WSAEWOULDBLOCK
    The socket is marked as nonblocking and the requested operation would block.
  • WSAEMSGSIZE
    The socket is message oriented, and the message is larger than the maximum supported by the underlying transport.
  • WSAETIMEDOUT
    The connection has been dropped, because of a network failure or because the system on the other end went down without notice.

Using Example

int iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0); 
if (SOCKET_ERROR == iResult) 
{ 
    wprintf(L"send failed with error: %d\n", WSAGetLastError()); 
    closesocket(ConnectSocket); 
    WSACleanup(); 
    return 1;
 }

References
https://msdn.microsoft.com/en-us/library/ms740149(VS.85).aspx

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末见秽,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子锻离,更是在濱河造成了極大的恐慌怀大,老刑警劉巖渔肩,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異榴嗅,居然都是意外死亡妄呕,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門录肯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來趴腋,“玉大人,你說我怎么就攤上這事论咏∮啪妫” “怎么了?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵厅贪,是天一觀的道長蠢护。 經(jīng)常有香客問我,道長养涮,這世上最難降的妖魔是什么葵硕? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任眉抬,我火速辦了婚禮,結(jié)果婚禮上懈凹,老公的妹妹穿的比我還像新娘蜀变。我一直安慰自己,他們只是感情好介评,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布库北。 她就那樣靜靜地躺著,像睡著了一般们陆。 火紅的嫁衣襯著肌膚如雪寒瓦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天坪仇,我揣著相機(jī)與錄音杂腰,去河邊找鬼。 笑死椅文,一個胖子當(dāng)著我的面吹牛喂很,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播雾袱,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼恤筛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了芹橡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤望伦,失蹤者是張志新(化名)和其女友劉穎林说,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體屯伞,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡腿箩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了劣摇。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片珠移。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖末融,靈堂內(nèi)的尸體忽然破棺而出钧惧,到底是詐尸還是另有隱情,我是刑警寧澤勾习,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布浓瞪,位于F島的核電站,受9級特大地震影響巧婶,放射性物質(zhì)發(fā)生泄漏乾颁。R本人自食惡果不足惜涂乌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望英岭。 院中可真熱鬧湾盒,春花似錦、人聲如沸诅妹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽漾唉。三九已至荧库,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間赵刑,已是汗流浹背分衫。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留般此,地道東北人蚪战。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像铐懊,于是被迫代替她去往敵國和親邀桑。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容