1.linuxä¸çç¨åºå¦ä½å¨windowsä¸è¿è¡
linuxä¸çç¨åºå¦ä½å¨windowsä¸è¿è¡
æ¬è´¨åºå«æ¯è¿æ ·çï¼linuxä¸ç¨çæ¯ä¼¯å å©socketï¼windowsåºä¸ç¨çWinSocket.
两è å ¶å®æ¯å¤§åå°å¼çï¼ä¸åçå°æ¹å¨ä»¥ä¸å ç¹ï¼
1 头æ件ä¸åï¼å¨linuxä¸ç¨å°çå ³é®å¤´æ件<sys/socket.h>
windowsä¸ç¨å°çæ¯ <Winsock2.h>
2 socketçåå§åä¸ä¸æ ·ï¼å¨windowsä¸è¦æä¸ä¸ªWSAStartupï¼èlinux ä¸æ²¡æã
3 å ·ä½çç»èä¸ä¸æ ·ï¼linuxä¸çcåwindowsä¸çcçä¸äºè¯æ³æäºåºå ¥ï¼ä½ èªå·±æ¾æ¾ã
ä¸é¢ææwindowsä¸çsocketé讯çæåºæ¬çç»æç»ä½ ï¼ä½ å¯¹ç §çä¿®æ¹å°±å¯ä»¥äºï¼ä¸ä¸çç¨åºæè°è¯äºä¸ä¸ï¼ç¼è¯è¿äºã
客æ·ç«¯
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1,源码洛阳源码系统 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr(".0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons();
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[];
recv(sockClient,recvBuf,,0);
printf("%s\n",recvBuf);
send(sockClient,"This is lisi",strlen("This is lisi")+1,0);
closesocket(sockClient);
WSACleanup();
}
æå¡å¨ç«¯
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.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();
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[];
sprintf(sendBuf,"æ¥èªæå¡å¨ç«¯",
inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[];
recv(sockConn,recvBuf,,0);
printf("%s\n",recvBuf);
closesocket(sockConn);
}
}
2025-01-19 02:291520人浏览
2025-01-19 02:181056人浏览
2025-01-19 02:142022人浏览
2025-01-19 02:081044人浏览
2025-01-19 00:461943人浏览
2025-01-19 00:092320人浏览
陶俑、珠寶、銅鏡、瓷器、杯盞、金銀瓶罐……匯聚105件中國珍貴文物,呈現一名女性躍升一代女皇的傳奇歷程,「一代女皇武則天展」26日在荷蘭北部城市呂伐登開展。展覽現場。(中國文物交流中心 供圖) 當日
中国消费者报北京讯记者万晓东)明明6月中旬才成熟的“北寨红杏”,5月上旬就已经开始在网店公开销售。为遏制侵权假冒行为,自5月底以来,北京市平谷区市场监管局启动“北寨红杏”“平谷大桃”地理标志保护专项行
中国消费者报南宁讯庞悦记者顾艳伟)金风轻拂、稻香飘外,八桂大地万里稻田传递着丰收的喜悦。广西兴业县市场监管局以服务“三农”为切入点,赶在夏粮征收入库之时,组织计量检定人员到粮食收购企业及基层粮食管理所