




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、/ EchoTCPClient.cpp :定義控制臺(tái)應(yīng)用程序的入口點(diǎn)客戶端#include stdafx.h#include time.h#include SocketFrame.hint tcp_client_fun_echo(FILE *fp,SOCKET s);#define ECHOPORT 7210int tcp_client_fun_echo( FILE *fp,SOCKET s );int main(int argc, char* argv)CSocketFrame frame;int iResult;SOCKET ClientSocket;輸入?yún)?shù)合法性檢查if (argc !
2、= 2)printf( usage: EchoTCPClient ;return -1;/Windows Sockets Dll 初始化 frame.start_up();創(chuàng)建客戶端的流式套接字,并與服務(wù)器建立連接printf(連接建立成功,請(qǐng)輸入回射字符串。n);ClientSocket = frame.tcp_client( ( char *)argv1, ECHOPORT );if ( ClientSocket = -1 ) return -1;開(kāi)始回射請(qǐng)求的發(fā)送與接收iResult = tcp_client_fun_echo(stdin,ClientSocket);frame.quit
3、( ClientSocket );return iResult;* 函數(shù)名:tcp_client_fun_echo輸入?yún)?shù):FILE *fp:指向FILE類型的對(duì)象SOCKET s:客戶端的連接套接字輸出參數(shù):0:成功,-1:失敗功能:tcp_client回射客戶端的具體功能函數(shù)int tcp_client_fun_echo( FILE *fp,SOCKET s ) time_t t;t=time(time_t*)NULL);FILE *fpOutint iResult = 0;int h=2;char *client=小芳:;char recvlineMAXLINE,sendlineMAXL
4、INE;for(;)if (h=1)memset(sendline,0,MAXLINE);memset(recvline,0,MAXLINE);接收數(shù)據(jù)iResult = recv( s, recvline, MAXLINE, 0);if (iResult 0) fpOut=fopen(QQ聊天客戶小芳端.txt ,a+);printf(小明時(shí)間:sn, recvline,ctime(&t);fprintf(fpOut,小明:sn,recvline);fprintf(fpOut,”接收時(shí)間:sn,ctime(&t);fclose(fpOut);h=2;continue;)if (h=2) me
5、mset(sendline,0,MAXLINE);memset(recvline,0,MAXLINE);printf(小芳:);if(fgets(sendline,MAXLINE,fp)!=NULL)if ( *sendline = Q) printf( input end!n);/數(shù)據(jù)發(fā)送結(jié)束,調(diào)用shutdown。函數(shù)聲明不再發(fā)送數(shù) 據(jù),此時(shí)客戶端仍可以接收數(shù)據(jù)iResult = shutdown(s, SD_SEND);if (iResult = SOCKET_ERROR) printf( shutdown failed with error: %dn, WSAGetLastError(
6、);return 0;iResult = send(s,sendline,strlen(sendline),0);if(iResult = SOCKET_ERROR)printf(send函數(shù)調(diào)用錯(cuò)誤,錯(cuò)誤號(hào):%ldn,WSAGetLastError();return -1;fpOut=fopen(QQ聊天客戶小芳端.txt,a+);printf( ntt 時(shí)間:%sn, ctime(&t);fprintf(fpOut,小明:sn,recvline);fprintf(fpOut,”發(fā)送時(shí)間:sn,ctime(&t);fclose(fpOut);h=1;continue;服務(wù)端/*_file.c
7、 - Definition of _iob, initializer and terminator.*Copyright (c) Microsoft Corporation. All rights reserved.*Purpose:Defines _iob, the array of stdio control structures, the initializer and terminator routines, and the multithread locking for stdio.*#include #include #include #include #include #incl
8、ude #include #include #include #include /*Buffer for stdin./char_bufin_INTERNAL_BUFSIZ;/*FILE descriptors; preset for stdin/out/err (note that the _tmpnum fieldis not initialized)/FILE _iob_IOB_ENTRIES = /* _ptr, _cnt, _base, _flag, _file, _charbuf, _bufsiz */* stdin (_iob0) */ _bufin, 0, _bufin, _I
9、OREAD | _IOYOURBUF, 0, 0, _INTERNAL_BUFSIZ ,/* stdout (_iob1) */ NULL, 0, NULL, _IOWRT, 1, 0, 0 ,/* stderr (_iob3) */ NULL, 0, NULL, _IOWRT, 2, 0, 0 ,;/* These functions are for enabling STATIC_CPPLIB functionality */ _CRTIMP FILE * _cdecl _iob_func(void)return _iob;/*Pointer to array of FILE * or _
10、FILEX * structures.*/void * _piob;/*Number of open streams (set to _NSTREAM by default)*/#ifdef CRTDLLint _nstream = _NSTREAM_;#else /* CRTDLL */int _nstream;#endif /* CRTDLL */*Initializer and terminator for stdio*/int _cdecl _initstdio( void);void _cdecl _endstdio(void);_CRTALLOC( .CRT$XIC) static
11、 _PIFV pinit = _initstdio;_CRTALLOC( .CRT$XPXA ) static _PVFV pterm = _endstdio;#ifndef CRTDLL/*_cflush is a dummy variable used to pull in _endstdio() when any STDIOroutine is included in the user program./int _cflush = 0;#endif /* CRTDLL */* _initstdio - Initialize the stdio system*Purpose:Create
12、and initialize the _piob array.*Entry: *Exit: Returns _RT_STDIOINIT if error encountered. *Uses:*Exceptions:*int _cdecl _initstdio(void) int i;#ifndef CRTDLL/*If the user has not supplied a definition of _nstream, set itto _NSTREAM_. If the user has supplied a value that is too smallset _nstream to
13、the minimum acceptable value (_IOB_ENTRIES).*/if ( _nstream = 0 )_nstream = _NSTREAM_;else if ( _nstream _IOB_eNtRIES )_nstream = _IOB_ENTRIES;#endif /* CRTDLL */*Allocate the _piob array. Try for _nstream entries first. If thisfails then reset _nstream to _IOB_ENTRIES and try again. If itstill fail
14、s, bail out with an RTE./if ( (_piob = (void *)_calloc_crt( _nstream, sizeof(void *)= NULL ) _nstream = _IOB_ENTRIES;if ( (_piob = (void *)_calloc_crt( _nstream, sizeof(void*) )= NULL )return _RT_STDIOINIT;/*Initialize the first _IOB_ENTRIES to point to the correspondingentries in _iob./for ( i = 0
15、; i _IOB_ENTRIES ; i+ )_piobi = ( void *)&_iobi;for ( i = 0 ; i 3 ; i+ ) if ( (_osfhnd(i) = (intptr_t)INVALID_HANDLE_VALUE) | (_osfhnd(i) = _nO_CONSoLe_FILEnO) | (_osfhnd(i) = 0) )/*For stdin, stdout & stderr, we use_NO_CONSOLE_FILENO (a valuedifferent from _INVALID_HANDLE_VALUE to distinguish betwe
16、ena failure in opening a file & a program run without a console./_iobi._file = _NO_CONSOLE_FILENO;)return 0;)/*_endstdio - Terminate the stdio system* *Purpose:Terminate the stdio system*(1) Flush all streams. (Do this even if were going tocall fcloseall since that routine wont do anything to the* s
17、td streams.)*(2) If returning to caller, close all streams. This is not necessary if the exe is terminating because the OS will close the files for us (much more efficiently, too).*Entry: *Exit: *Uses:*Exceptions:* */ void _cdecl _endstdio(void)/* flush all streams */ _flushall();/* if in callable e
18、xit, close all streams */ if (_exitflag)_fcloseall();_free_crt(_piob);- 一/* _lock*file - Lock a FILE*Purpose:Assert the lock for a stdio-level file*Entry:pf = _piob口 entry (pointer to a FILE or _FILEX)*Exit:*Exceptions:*void _cdecl _lock_file (一FILE *pf 一 )/*The way the FILE (pointed to by pf) is lo
19、cked depends on whetherit is part of _iob口 or not/if ( (pf = Ji0b) & (pf _flag |= _IOLOCKED;) else/*Not part of _iob. Therefore, *pf is a _FILEX and thelock field of the struct is an initialized criticalsection./EnterCriticalSection( &(_FILEX *)pf)-lock);)/*_lock_file2(i, s) - Lock the FILE * *Purpo
20、se:Assert the lock for a stdio-level file given by s = _piobi.*Entry:s = _piobi*Exit:*Exceptions:*void _cdecl _lock_file2 ( int i, void *s)/*The way the FILE is locked depends on whether it is part of _iob_iob or not/if ( i _flag |= _IOLOCKED; else /*Not part of _iob. Therefore, *s is a _FILEX and t
21、helock field of the struct is an initialized criticalsection./EnterCriticalSection( &(_FILEX *)s)-lock);/* unlock file - Unlock a FILE*Purpose:Release the lock for a stdio-level file*Entry:pf = _piob entry (pointer to a FILE or _FILEX)*Exit:*Exceptions:*/void _cdecl _unlock_file ( FILE *pf )/*The way the FILE (pointed to by pf) is unlocked depends on whetherit is part of _iob口 or not/if ( (pf = _iob)
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)村材料合同范本
- 動(dòng)產(chǎn)汽車互易合同范本
- 企業(yè)投資期權(quán)合同范本
- 2024年徐州市凱信電子設(shè)備有限公司招聘考試真題
- 勞動(dòng)關(guān)系合同范本
- 2024年西安醫(yī)學(xué)院第一附屬醫(yī)院灃東醫(yī)院招聘筆試真題
- 2024年臺(tái)州仙居縣人民醫(yī)院醫(yī)共體招聘工作人員筆試真題
- 2024鞍鋼資本金融管理業(yè)務(wù)人才招聘4人筆試參考題庫(kù)附帶答案詳解
- 第16課《誡子書》教學(xué)設(shè)計(jì) 2024-2025學(xué)年統(tǒng)編版語(yǔ)文七年級(jí)上冊(cè)
- 出售環(huán)衛(wèi)用車合同范本
- 2025年湖南城建職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫(kù)新版
- 《中國(guó)古代文學(xué)史及作品選II》教學(xué)大綱
- 代工生產(chǎn)合同范本
- 瑜伽課程合同轉(zhuǎn)讓協(xié)議書范本
- 個(gè)人經(jīng)營(yíng)性貸款合同模板
- 人教版英語(yǔ)2025七年級(jí)下冊(cè) Unit1Animal Friends教師版 語(yǔ)法講解+練習(xí)
- DeepSeek新手入門教程
- 課件:《教育強(qiáng)國(guó)建設(shè)規(guī)劃綱要(2024-2035年)》學(xué)習(xí)宣講
- 2025年山東化工職業(yè)學(xué)院高職單招職業(yè)適應(yīng)性測(cè)試近5年??及鎱⒖碱}庫(kù)含答案解析
- 2025年全國(guó)幼兒園教師資格證考試教育理論知識(shí)押題試題庫(kù)及答案(共九套)
- 2024年鄭州電力高等??茖W(xué)校高職單招職業(yè)適應(yīng)性測(cè)試歷年參考題庫(kù)含答案解析
評(píng)論
0/150
提交評(píng)論