聊天交互設(shè)計(jì)vs10_第1頁(yè)
聊天交互設(shè)計(jì)vs10_第2頁(yè)
聊天交互設(shè)計(jì)vs10_第3頁(yè)
聊天交互設(shè)計(jì)vs10_第4頁(yè)
聊天交互設(shè)計(jì)vs10_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論