版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
SERVER#include<winsock2.h>#include<ws2tcpip.h>#include<windows.h>#include<direct.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#pragmacomment(lib,"ws2_32.lib")TOC\o"1-5"\h\z#define PORT 10000#define NO_ERROR 0#defineERROR 1#define DATABUFFSIZE 1500#defineINVALID_IP 100WSADATAWsaData;SOCKETlistenSocket;SOCKETdataSocket;char sendBuff[DATABUFFSIZE];char recvBuff[DATABUFFSIZE];intinitialize(void);intbindlPandPORT(void);intsendAndReceive(void);intmain(void){initialize();bindlPandPORT();sendAndReceive();return0;}intinitialize(){/*初始化WSA*/if(WSAStartup(MAKEWORD(2,2),&WsaData)!=0)returnERROR;returnNO_ERROR;}intbindIPandPORT()sockaddr_inlistenSocketAddr;/*創(chuàng)建監(jiān)聽套接字*/listenSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);/*綁定套接字*/listenSocketAddr.sin_addr.s_addr=INADDR_ANY;listenSocketAddr.sin_port=htons(PORT);listenSocketAddr.sin_family=AF_INET;if(bind(listenSocket,(structsockaddr*)&listenSocketAddr,sizeof(listenSocketAddr))==SOCKET_ERROR)returnERROR;if(listen(listenSocket,5)==SOCKET_ERROR){returnERROR;}dataSocket=accept(listenSocket,NULL,NULL);returnNO_ERROR;}intsendAndReceive(void){intsent;while(1){ZeroMemory(sendBuff,DATABUFFSIZE);fflush(stdin);printf("server:");scanf_s("%s",sendBuff,DATABUFFSIZE);sent=send(dataSocket,sendBuff,(int)strlen(sendBuff),0);ZeroMemory(recvBuff,DATABUFFSIZE);recv(dataSocket,recvBuff,DATABUFFSIZE,O);printf("client:%s\n",recvBuff);}returnNO_ERROR;}Client#include<winsock2.h>#include<ws2tcpip.h>#include<windows.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#pragmacomment(lib,"ws2_32.lib")#definePORT10000#defineIP"127.0.0.1"#defineNO_ERROR0#defineERROR1#defineDATABUFFSIZE1500#defineINVALID_IP100WSADATAWsaData;SOCKETconnSocket;sockaddr_inconnSocketAddr;char sendBuff[DATABUFFSIZE];char recvBuff[DATABUFFSIZE];intinitialize(void);intsendAndReceive(void);intmain(){initialize();sendAndReceive();}intinitialize(void){if(WSAStartup(MAKEWORD(2,2),&WsaData)!=0)returnERROR;connSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);connSocketAddr.sin_family=AF_INET;connSocketAddr.sin_port=htons(PORT);connSocketAddr.sin_addr.s_addr=inet_addr(IP);if(connSocket==INVALID_SOCKET){WSACleanup();returnERROR;}returnNO_ERROR;}intsendAndReceive(void){if(connect(connSocket,(LPSOCKADDR)&connSocketAddr,sizeof(connSocketAddr))!=SOCKET_ERROR){printf("connecttoserver'n");}intsent,receive;while(1){ZeroMemory(recvBuff,DATABUFFSIZE);receive=recv(connSocket,recvBuff,DATABUFFSIZE,O);printf("server:%s\n",recvBuff);ZeroMemory(sendBuff,DATABUFFSIZE);fflush(stdin);printf("client:");scanf_s("%s",sendBuff,DATABUFFSIZE);sent=send(connSocket,sendBuff,(int)strlen(sendBuff),0);}returnNO_ERROR;}下載的讀文件到緩沖區(qū)并發(fā)送int_downLoad(char*filename){intsent,sizeFileLow;DWORDsizeRead;HANDLEhFile=NULL;charsize[10];intcurrentsize=0;hFile=CreateFile(filename,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);while(1){ZeroMemory(dataBuffer,DATA_BUFFER_SIZE);ReadFile(hFile,dataBuffer,DATA_BUFFER_SIZE,&sizeRead,NULL);i”sizeRead==0)break;sent=send(connSocket,dataBuffer,sizeRead,0);currentsize+=sent;CloseHandle(hFile);returnNO_ERR;將收到的數(shù)據(jù)存文件int_downLoad(char*filename){HANDLEhFile=NULL;intsizeReceived=O;intcurrentSize=O;intsizeWritten=0;intsize=0;chardatasize[10];PBYTEvirtualMemory=NULL;hFile=CreateFile(filename,GENERIC_READ|GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);while(1){ZeroMemory(dataBuffer,DATA_BUFFER_SIZE);sizeReceived=recv(dataSocket,dataBuffer,DATA_BUFFER_SIZE,0);WriteFile(hFile,dataBuffer,sizeReceived,&sizeWritten,NULL);printf("%d\n",sizeWritten);if(sizeReceived<DATA_BUFFER_SIZE)break;}SetEndOfFile(hFile);CloseHandle(hFile);returnNO_ERR;}發(fā)送根目錄的代碼intsendList(void){chdir("c:\\");WIN32_FIND_DATAfindFileData;HANDLEhFind=INVALID_HANDLE_VALUE;intsent=0,currentsize=0;charsize[30];ZeroMemory(dataBuff,DATABUFFSIZE);hFind=FindFirstFile("*.*",&findFileData);do{if(findFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)strcat(dataBuff,"【文件夾】");elsestrcat(dataBuff,"\t");itoa(findFileData.nFileSizeLow,size,10);strcat(dataBuff,"\t");strcat(dataBuff,size);strcat(dataBuff,"B\t");strcat(dataBuff,findFileData.cFileName);strcat(dataBuff,"\r\n");}while(FindNextFile(hFind,&findFileData)!=O);strcat(dataBuff,"\O");FindClose(hFind);sent=send(dataSocket,dataBuff,(int)strlen(dataBuff),0);returnNO_ERR;}關(guān)于多線程的無(wú)聊代碼#include<stdio.h>#include<string.h>#include<stdlib.h>#include<Windows.h>DWORDWINAPIplusfunc(LPVOIDlpParam);DWORDWINAPIminusfunc(LPVOIDlpParam);typedefstructThreadData{inta;intb;}ThreadData;CRITICAL_SECTIONcs;intshare=0;voidmain(){DWORDdwThreadld;ThreadDatapara;para.a=1;para.b=0;InitializeCriticalSection(&cs);HANDLEhThread=CreateThread(NULL,O,plusfunc,¶,O,&dwThreadld);HANDLEhThread2=CreateThread(NULL,0,minusfunc,¶,O,&dwThreadld);system("pause");intx;intn=0;EnterCriticalSection(&cs);while(n<10)x=((ThreadData*)lpParam)->a;share++;printf("%d\n",share);n++;LeaveCriticalSection(&cs);return0;DWORDWINAPIminusfunc(LPVOIDlpParam)intx;intn=0;while(n<10)EnterCriticalSection(&cs);x=((ThreadData*)lpParam)->b;share--;LeaveCriticalSection(&cs);printf("%d\n",share);n++;return0;UDP/*server*/#include<stdio.h>#include<winsock2.h>#include<stdlib.h>#pragmacomment(lib,"ws2_32.lib")#defineIP"127.0.0.1"voiderrexit(constchar*format,...){va_listargs;va_start(args,format);vfprintf(stderr,format,args);va_end(args);WSACIeanup();exit(1);}intmain(){charmsg[512];charbuf[512];WSADATAwsaData;if(WSAStartup(MAKEWORD(2,2),&wsaData))errexit("WSAStartupfailed!\n");〃設(shè)置地址端口信息SOCKADDR_INsvrAddr;svrAddr.sin_family=AF_INET;svrAddr.sin_addr.s_addr=inet_addr(IP);svrAddr.sin_addr.S_un.S_addr=inet_addr(IP);svrAddr.sin_port=htons(16000);SOCKETsvr;〃創(chuàng)建套接字svr=socket(AF_INET,SOCK_DGRAM,0);if(svr==INVALID_SOCKET){errexit("can'tcreatesocket:%d",GetLastError());}if(bind(svr,(SOCKADDR*)&svrAddr,sizeof(SOCKADDR_IN))==SOCKET_ERROR){errexit("can'tbind%d\n",GetLastError());}while(true){printf("<--");SOCKADDR_INaddrClient;memset(buf,0,512);intsize=sizeof(addrClient);memset(&addrClient,O,size);〃讀取數(shù)據(jù)intcc=recvfrom(svr,buf,1024,0,(sockaddr*)&addrClient,&size);if(cc==SOCKET_ERROR){errexit("can'treceive%d\n",GetLastError());}printf("%s\n",buf);〃輸入數(shù)據(jù)進(jìn)行發(fā)送printf("-->");intcnt=0;memset(msg,0,512);fflush(stdin);while(((msg[cnt]=getchar())!='\n')&&cnt!=512)++cnt;msg[cnt]='\0';if(sendto(svr,msg,512,0,(sockaddr*)&addrClient,size)==SOCKET_ERROR){errexit("sendfailed:%d\n",GetLastError());}}closesocket(svr);WSACleanup();return0;}/*client*/#include<stdio.h>#include<winsock2.h>#include<stdlib.h>#pragmacomment(lib,"ws2_32.lib")#defineIP"127.0.0.1"voiderrexit(constchar*format,...)va_listargs;va_start(args,format);vfprintf(stderr,format,args);va_end(args);WSACleanup();exit(1);intmain(){charmsg[512];charbuf[512];WSADATAwsaData;if(WSAStartup(MAKEWORD(2,2),&wsaData))errexit("WSAStartupfailed!\n");〃設(shè)置服務(wù)器端地址SOCKADDR_INsvrAddr;svrAddr.sin_family=AF_INET;svrAddr.sin_addr.s_addr=inet_addr(IP);svrAddr.sin_addr.S_un.S_addr=inet_addr(IP);svrAddr.sin_port=htons(16000);SOCKETs;s=socket(AF_INET,SOCK_DGRAM,0);if(s==INVALID_SOCKET){errexit("can'tcreatesocket:%d\n",GetLastError());}else{while(true){〃輸入數(shù)據(jù)進(jìn)行發(fā)送intsize=sizeof(SOCKADDR_IN);printf("-->");intcnt=0;memset(msg,0,512);fflush(stdin);while(((msg[cnt]=getchar())!='\n')&&cnt!=512)++cnt;msg[cnt]='\0';if(sendto(s,msg,512,0,(sockaddr*)&svrAddr,size)==SOCKET_ERROR){errexit("sendfailed:%d\n",GetLastError());}〃收數(shù)據(jù)printf("<--");SOCKADDR_INaddrClient;memset(buf,0,512);intcc=recvfrom(s,buf,512,0,(sockaddr*)&addrClient,&size);if(cc==SOCKETERROR)鉤子/*hookdll*/#include<Windows.h>extern"C"__declspec(dllexport)voidSetHook();staticHHOOKhMouseHook=0,hKeyboardHook=0;staticHINSTANCEhInstance;LRESULTCALLBACKprocKeyboard(intnCode,WPARAMwParam,LPARAMlParam){if(wParam==VK_F5){UnhookWindowsHookEx(hKeyb
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五版建筑工程主體承包合同(含建筑垃圾資源化處理)范本6篇
- 二零二五年度食堂服務(wù)員派遣合同2篇
- 二零二五年度二手?jǐn)嚢柙O(shè)備二手交易碳排放交易合同3篇
- 二零二五年進(jìn)出口貨物檢驗(yàn)檢疫合同3篇
- 二零二五版房屋抵押貸款合同樣本編制指南6篇
- 石場(chǎng)生產(chǎn)線承包合同2025年度規(guī)范文本6篇
- 標(biāo)題14:2025年度網(wǎng)絡(luò)安全監(jiān)測(cè)與預(yù)警服務(wù)合同2篇
- 二零二五年技術(shù)轉(zhuǎn)讓合同具體條款2篇
- 二零二五年度酒吧經(jīng)營(yíng)場(chǎng)所租賃合同范本(專業(yè)解析版)2篇
- 二零二五年度建筑工地環(huán)境監(jiān)測(cè)與節(jié)能管理系統(tǒng)合同3篇
- EPC總承包項(xiàng)目中的質(zhì)量管理體系
- 滬教版小學(xué)語(yǔ)文古詩(shī)(1-4)年級(jí)教材
- 外科醫(yī)生年終述職總結(jié)報(bào)告
- 橫格紙A4打印模板
- CT設(shè)備維保服務(wù)售后服務(wù)方案
- 重癥血液凈化血管通路的建立與應(yīng)用中國(guó)專家共識(shí)(2023版)
- 兒科課件:急性細(xì)菌性腦膜炎
- 柜類家具結(jié)構(gòu)設(shè)計(jì)課件
- 陶瓷瓷磚企業(yè)(陶瓷廠)全套安全生產(chǎn)操作規(guī)程
- 煤炭運(yùn)輸安全保障措施提升運(yùn)輸安全保障措施
- JTGT-3833-2018-公路工程機(jī)械臺(tái)班費(fèi)用定額
評(píng)論
0/150
提交評(píng)論