欢迎分享,转载请注明来源:内存溢出
mfc只是对socket进行了一些封装,大部分人做网络编程都是用的原始的socket,比如如下接口都可以在c下进行调用 1.socket()2.bind()3.connect()4.listen()5.accept()6.send() 和recv()7.sendto() 和recvfrom()8.close() 和shutdown()9.getpeername() 10.gethostname() 这些接口是在Winsock2.h中定义的不是在mfc中定义的,你只需要包含Winsock2.h头文件和Ws2_32.lib库就可以了。//服务端代码\x0d\x0a#include\x0d\x0a#include\x0d\x0a#pragma comment(lib,"ws2_32.lib")//这句关键\x0d\x0avoid main(){\x0d\x0aWORD wV\x0d\x0aWSADATA ws\x0d\x0aint er\x0d\x0awV=MAKEWORD(1,1)\x0d\x0aer=WSAStartup(wV,&ws)\x0d\x0aif(er!=0){\x0d\x0areturn\x0d\x0a\x0d\x0a}\x0d\x0aif(LOBYTE(ws.wVersion)!=1||HIBYTE(ws.wVersion)!=1){\x0d\x0aWSACleanup()\x0d\x0areturn\x0d\x0a}\x0d\x0aSOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0)\x0d\x0aSOCKADDR_IN addrSrv\x0d\x0aaddrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY)\x0d\x0aaddrSrv.sin_family=AF_INET\x0d\x0aaddrSrv.sin_port=htons(6000)\x0d\x0a\x0d\x0abind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR))\x0d\x0a\x0d\x0alisten(sockSrv,5)\x0d\x0a\x0d\x0aSOCKADDR_IN addrClient\x0d\x0aint len=sizeof(SOCKADDR)\x0d\x0awhile(1)\x0d\x0a{\x0d\x0aSOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len)\x0d\x0a\x0d\x0achar sendBuf[100]\x0d\x0asprintf(sendBuf,"welcome %s to wozhe",inet_ntoa(addrClient.sin_addr))\x0d\x0asend(sockConn,sendBuf,strlen(sendBuf)+1,0)\x0d\x0achar recvBuf[100]\x0d\x0arecv(sockConn,recvBuf,100,0)\x0d\x0aprintf("%s\n",recvBuf)\x0d\x0aclosesocket(sockConn)\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a//客户端\x0d\x0a#include\x0d\x0a#include\x0d\x0a#pragma comment(lib,"ws2_32.lib")\x0d\x0avoid main(){\x0d\x0aWORD wV\x0d\x0aWSADATA ws\x0d\x0aint er\x0d\x0awV=MAKEWORD(1,1)\x0d\x0aer=WSAStartup(wV,&ws)\x0d\x0aif(er!=0){\x0d\x0areturn\x0d\x0a\x0d\x0a}\x0d\x0aif(LOBYTE(ws.wVersion)!=1||HIBYTE(ws.wVersion)!=1){\x0d\x0aWSACleanup()\x0d\x0areturn\x0d\x0a}\x0d\x0a\x0d\x0aSOCKET sockC=socket(AF_INET,SOCK_STREAM,0)\x0d\x0aSOCKADDR_IN addrS\x0d\x0aaddrS.sin_addr.S_un.S_addr=inet_addr("127.0.0.1")\x0d\x0aaddrS.sin_family=AF_INET\x0d\x0aaddrS.sin_port=htons(6000)\x0d\x0aconnect(sockC,(SOCKADDR*)&addrS,sizeof(SOCKADDR))\x0d\x0achar recvB[100]\x0d\x0arecv(sockC,recvB,100,0)\x0d\x0aprintf("%s\n",recvB)\x0d\x0asend(sockC,"this is list",strlen("this is list")+1,0)\x0d\x0aclosesocket(sockC)\x0d\x0aWSACleanup()\x0d\x0a}
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
win7系统打天之痕黑屏
上一篇
2023-05-20
obs怎么高帧率140帧
下一篇
2023-05-20
评论列表(0条)