windows下的C语言socket编程没有相关socket的头文件怎么办?

windows下的C语言socket编程没有相关socket的头文件怎么办?,第1张

//服务端代码\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}

1. 头文件

windows下winsock.h或winsock2.h

linux下netinet/in.h(大部分都在这儿),unistd.h(close函数在这儿),sys/socket.h(在in.h里已经包含了,可以省了)

2. 初始化

windows下需要用WSAStartup启动Ws2_32.lib,并且要用#pragma comment(lib,"Ws2_32")来告知编译器链接该lib。

linux下不需要

3. 关闭socket

windows下closesocket(...)

linux下close(...)

4. 类型

windows下SOCKET

linux下int

5. 获取错误码

windows下getlasterror()/WSAGetLastError()

linux下,未能成功执行的socket *** 作会返回-1;如果包含了errno.h,就会设置errno变量

6. 设置非阻塞

windows下ioctlsocket()

linux下fcntl(),需要头文件fcntl.h

7. send函数最后一个参数

windows下一般设置为0

linux下最好设置为MSG_NOSIGNAL,如果不设置,在发送出错后有可能会导致程序退出

8. 毫秒级时间获取

windows下GetTickCount()

linux下gettimeofday()

//服务端代码

#include<winsock2.h>

#include<stdio.h>

#pragma comment(lib,"ws2_32.lib")//这句关键

void main(){

WORD wV

WSADATA ws

int er

wV=MAKEWORD(1,1)

er=WSAStartup(wV,&ws)

if(er!=0){

return

}

if(LOBYTE(ws.wVersion)!=1||HIBYTE(ws.wVersion)!=1){

WSACleanup()

return

}

SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0)

SOCKADDR_IN addrSrv

addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY)

addrSrv.sin_family=AF_INET

addrSrv.sin_port=htons(6000)

bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR))

listen(sockSrv,5)

SOCKADDR_IN addrClient

int len=sizeof(SOCKADDR)

while(1)

{

SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len)

char sendBuf[100]

sprintf(sendBuf,"welcome %s to wozhe",inet_ntoa(addrClient.sin_addr))

send(sockConn,sendBuf,strlen(sendBuf)+1,0)

char recvBuf[100]

recv(sockConn,recvBuf,100,0)

printf("%s\n",recvBuf)

closesocket(sockConn)

}

}

//客户端

#include<winsock2.h>

#include<stdio.h>

#pragma comment(lib,"ws2_32.lib")

void main(){

WORD wV

WSADATA ws

int er

wV=MAKEWORD(1,1)

er=WSAStartup(wV,&ws)

if(er!=0){

return

}

if(LOBYTE(ws.wVersion)!=1||HIBYTE(ws.wVersion)!=1){

WSACleanup()

return

}

SOCKET sockC=socket(AF_INET,SOCK_STREAM,0)

SOCKADDR_IN addrS

addrS.sin_addr.S_un.S_addr=inet_addr("127.0.0.1")

addrS.sin_family=AF_INET

addrS.sin_port=htons(6000)

connect(sockC,(SOCKADDR*)&addrS,sizeof(SOCKADDR))

char recvB[100]

recv(sockC,recvB,100,0)

printf("%s\n",recvB)

send(sockC,"this is list",strlen("this is list")+1,0)

closesocket(sockC)

WSACleanup()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12065195.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存