门外汉 求c++ 解释代码 简单socket 通信
winsock实现基于TCP的客户端/服务器通信。。越详细越好本人一点不懂希望能逐行解释serve端代码#include"stdafx.h"#include<Winsoc...
winsock实现基于TCP的客户端/服务器通信。。
越详细越好 本人一点不懂 希望能逐行解释
serve端代码
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#define PORT 5000
int main(int argc, char* argv[])
{
int port=PORT;
WSADATA wsaData;
SOCKET sListen,sAccept;
int iLen;
int iSend;
char buf[]="您好!";
//服务器、客户的地址
struct sockaddr_in serv,cliet;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
//return;
}
sListen=socket(AF_INET,SOCK_STREAM,0);
if(sListen==INVALID_SOCKET)
{
printf("socket failed:%d\n",WSAGetLastError());
return;
}
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sListen,(LPSOCKADDR)&serv,sizeof(serv))==SOCKET_ERROR)
{
printf("bind()failed:%d\n",WSAGetLastError());
return;
}
if(listen(sListen,5)==SOCKET_ERROR)
{
printf("listen()failed:%d\n",WSAGetLastError());
return;
}
iLen=sizeof(cliet);
while(1)
{
sAccept=accept(sListen,(struct sockaddr*)&cliet,&iLen);
if(sAccept==INVALID_SOCKET)
{
printf("accept()failed:%d\n",WSAGetLastError());
break;
}
printf("accepted client IP:[%s],port:[%d]\n",inet_ntoa(cliet.sin_addr),ntohs(cliet.sin_port));
//给连接的客户发送信息
iSend=send(sAccept,buf,sizeof(buf),0);
if(iSend==SOCKET_ERROR)
{
printf("send()failed:%d\n",WSAGetLastError());
break;
}
else if(iSend==0)
break;
else
printf("send()byte:%d\n",send);
closesocket(sAccept);
}
closesocket(sListen);
WSACleanup();
}
=============================================
client端代码
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#define PORT 5000
#define BUFFER 1024
void main(int argc,char *argv[])
{
WSADATA wsaData;
SOCKET client;
int port=PORT;
int iLen;
char buf[BUFFER];
struct sockaddr_in serv;
memset(buf,0,sizeof(buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
return;
}
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=inet_addr("192.168.0.1");
client=socket(AF_INET,SOCK_STREAM,0);
if(client==INVALID_SOCKET)
{
printf("socket()failed:%d\n",WSAGetLastError());
return;
}
if(connect(client,(struct sockaddr*)&serv,sizeof(serv))==INVALID_SOCKET)
{
printf("connet()failed:%d\n",WSAGetLastError());
return;
}
else
{
iLen=recv(client,buf,sizeof(buf),0);
if(iLen==0)
return;
else if(iLen==SOCKET_ERROR)
{
printf("recv()failed:%d\n",WSAGetLastError());
return;
}
printf("recv()data from server:%s\n",buf);
}
closesocket(client);
WSACleanup();
printf("press any key to continue");
while(1);
}
谢谢各位大神
最好能逐行。。。。。
好像很麻烦哈。。。 展开
越详细越好 本人一点不懂 希望能逐行解释
serve端代码
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#define PORT 5000
int main(int argc, char* argv[])
{
int port=PORT;
WSADATA wsaData;
SOCKET sListen,sAccept;
int iLen;
int iSend;
char buf[]="您好!";
//服务器、客户的地址
struct sockaddr_in serv,cliet;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
//return;
}
sListen=socket(AF_INET,SOCK_STREAM,0);
if(sListen==INVALID_SOCKET)
{
printf("socket failed:%d\n",WSAGetLastError());
return;
}
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sListen,(LPSOCKADDR)&serv,sizeof(serv))==SOCKET_ERROR)
{
printf("bind()failed:%d\n",WSAGetLastError());
return;
}
if(listen(sListen,5)==SOCKET_ERROR)
{
printf("listen()failed:%d\n",WSAGetLastError());
return;
}
iLen=sizeof(cliet);
while(1)
{
sAccept=accept(sListen,(struct sockaddr*)&cliet,&iLen);
if(sAccept==INVALID_SOCKET)
{
printf("accept()failed:%d\n",WSAGetLastError());
break;
}
printf("accepted client IP:[%s],port:[%d]\n",inet_ntoa(cliet.sin_addr),ntohs(cliet.sin_port));
//给连接的客户发送信息
iSend=send(sAccept,buf,sizeof(buf),0);
if(iSend==SOCKET_ERROR)
{
printf("send()failed:%d\n",WSAGetLastError());
break;
}
else if(iSend==0)
break;
else
printf("send()byte:%d\n",send);
closesocket(sAccept);
}
closesocket(sListen);
WSACleanup();
}
=============================================
client端代码
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#define PORT 5000
#define BUFFER 1024
void main(int argc,char *argv[])
{
WSADATA wsaData;
SOCKET client;
int port=PORT;
int iLen;
char buf[BUFFER];
struct sockaddr_in serv;
memset(buf,0,sizeof(buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
return;
}
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=inet_addr("192.168.0.1");
client=socket(AF_INET,SOCK_STREAM,0);
if(client==INVALID_SOCKET)
{
printf("socket()failed:%d\n",WSAGetLastError());
return;
}
if(connect(client,(struct sockaddr*)&serv,sizeof(serv))==INVALID_SOCKET)
{
printf("connet()failed:%d\n",WSAGetLastError());
return;
}
else
{
iLen=recv(client,buf,sizeof(buf),0);
if(iLen==0)
return;
else if(iLen==SOCKET_ERROR)
{
printf("recv()failed:%d\n",WSAGetLastError());
return;
}
printf("recv()data from server:%s\n",buf);
}
closesocket(client);
WSACleanup();
printf("press any key to continue");
while(1);
}
谢谢各位大神
最好能逐行。。。。。
好像很麻烦哈。。。 展开
展开全部
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#define PORT 5000
int main(int argc, char* argv[])
{
int port=PORT;
WSADATA wsaData;
SOCKET sListen,sAccept;
int iLen;
int iSend;
char buf[]="您好!";
//服务器、客户的地址
struct sockaddr_in serv,cliet; //声明两个地址结构体变量
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
//return;
} //加载套接字库
//创建一个套接字 AD_INET(IPV4协议) SOCK_STREAM(TCP协议)
sListen=socket(AF_INET,SOCK_STREAM,0);
//判断是否失败
if(sListen==INVALID_SOCKET)
{
printf("socket failed:%d\n",WSAGetLastError());
return;
}
为服务器的地址结构体赋值
serv.sin_family=AF_INET; //必须和创建套接字时候一样(socket)
serv.sin_port=htons(port); //端口 htons转换为网络字节序
serv.sin_addr.s_addr=htonl(INADDR_ANY); //绑定的地址 htonl转换为网络字节序
//INADDR_ANY 默认的网卡地址 本机
if(bind(sListen,(LPSOCKADDR)&serv,sizeof(serv))==SOCKET_ERROR)
{
printf("bind()failed:%d\n",WSAGetLastError());
return;
}//bind 绑定地址 并且判断是否成功
if(listen(sListen,5)==SOCKET_ERROR)
{
printf("listen()failed:%d\n",WSAGetLastError());
return;
}//监听 并且判断是否成功
iLen=sizeof(cliet); 得到地址结构体的长度 accept函数需要
//循环简体客户连接
while(1)
{
sAccept=accept(sListen,(struct sockaddr*)&cliet,&iLen); //accept阻塞 知道有新的连接
if(sAccept==INVALID_SOCKET)
{
printf("accept()failed:%d\n",WSAGetLastError());
break;
}
printf("accepted client IP:[%s],port:[%d]\n",inet_ntoa(cliet.sin_addr),ntohs(cliet.sin_port));
//给连接的客户发送信息
iSend=send(sAccept,buf,sizeof(buf),0);
if(iSend==SOCKET_ERROR)
{
printf("send()failed:%d\n",WSAGetLastError());
break;
}
else if(iSend==0)
break;
else
printf("send()byte:%d\n",send);
closesocket(sAccept);
}
closesocket(sListen);//关闭套接字
WSACleanup();//关闭套接字库
}
=============================================
client端代码
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#define PORT 5000
#define BUFFER 1024
void main(int argc,char *argv[])
{
WSADATA wsaData;
SOCKET client;
int port=PORT;
int iLen;
char buf[BUFFER];
struct sockaddr_in serv;
memset(buf,0,sizeof(buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
return;
}
为地址结构体赋值
serv.sin_family=AF_INET; //地址族 和上面一样
serv.sin_port=htons(port); //服务端监听的端口
serv.sin_addr.s_addr=inet_addr("192.168.0.1"); //这应该是服务端的IP地址
client=socket(AF_INET,SOCK_STREAM,0); //创建 套接字
if(client==INVALID_SOCKET) //判定是否成功
{
printf("socket()failed:%d\n",WSAGetLastError());
return;
}
//连接服务器 并且判断是否成功
if(connect(client,(struct sockaddr*)&serv,sizeof(serv))==INVALID_SOCKET)
{
printf("connet()failed:%d\n",WSAGetLastError());
return;
}
else
{
iLen=recv(client,buf,sizeof(buf),0); //recv读取消息
if(iLen==0)
return;
else if(iLen==SOCKET_ERROR)
{
printf("recv()failed:%d\n",WSAGetLastError());
return;
}
printf("recv()data from server:%s\n",buf);
}
closesocket(client); 关闭套接字
WSACleanup();关闭套接字库
printf("press any key to continue");
while(1);//防止程序退出
}
#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#define PORT 5000
int main(int argc, char* argv[])
{
int port=PORT;
WSADATA wsaData;
SOCKET sListen,sAccept;
int iLen;
int iSend;
char buf[]="您好!";
//服务器、客户的地址
struct sockaddr_in serv,cliet; //声明两个地址结构体变量
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
//return;
} //加载套接字库
//创建一个套接字 AD_INET(IPV4协议) SOCK_STREAM(TCP协议)
sListen=socket(AF_INET,SOCK_STREAM,0);
//判断是否失败
if(sListen==INVALID_SOCKET)
{
printf("socket failed:%d\n",WSAGetLastError());
return;
}
为服务器的地址结构体赋值
serv.sin_family=AF_INET; //必须和创建套接字时候一样(socket)
serv.sin_port=htons(port); //端口 htons转换为网络字节序
serv.sin_addr.s_addr=htonl(INADDR_ANY); //绑定的地址 htonl转换为网络字节序
//INADDR_ANY 默认的网卡地址 本机
if(bind(sListen,(LPSOCKADDR)&serv,sizeof(serv))==SOCKET_ERROR)
{
printf("bind()failed:%d\n",WSAGetLastError());
return;
}//bind 绑定地址 并且判断是否成功
if(listen(sListen,5)==SOCKET_ERROR)
{
printf("listen()failed:%d\n",WSAGetLastError());
return;
}//监听 并且判断是否成功
iLen=sizeof(cliet); 得到地址结构体的长度 accept函数需要
//循环简体客户连接
while(1)
{
sAccept=accept(sListen,(struct sockaddr*)&cliet,&iLen); //accept阻塞 知道有新的连接
if(sAccept==INVALID_SOCKET)
{
printf("accept()failed:%d\n",WSAGetLastError());
break;
}
printf("accepted client IP:[%s],port:[%d]\n",inet_ntoa(cliet.sin_addr),ntohs(cliet.sin_port));
//给连接的客户发送信息
iSend=send(sAccept,buf,sizeof(buf),0);
if(iSend==SOCKET_ERROR)
{
printf("send()failed:%d\n",WSAGetLastError());
break;
}
else if(iSend==0)
break;
else
printf("send()byte:%d\n",send);
closesocket(sAccept);
}
closesocket(sListen);//关闭套接字
WSACleanup();//关闭套接字库
}
=============================================
client端代码
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#define PORT 5000
#define BUFFER 1024
void main(int argc,char *argv[])
{
WSADATA wsaData;
SOCKET client;
int port=PORT;
int iLen;
char buf[BUFFER];
struct sockaddr_in serv;
memset(buf,0,sizeof(buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
return;
}
为地址结构体赋值
serv.sin_family=AF_INET; //地址族 和上面一样
serv.sin_port=htons(port); //服务端监听的端口
serv.sin_addr.s_addr=inet_addr("192.168.0.1"); //这应该是服务端的IP地址
client=socket(AF_INET,SOCK_STREAM,0); //创建 套接字
if(client==INVALID_SOCKET) //判定是否成功
{
printf("socket()failed:%d\n",WSAGetLastError());
return;
}
//连接服务器 并且判断是否成功
if(connect(client,(struct sockaddr*)&serv,sizeof(serv))==INVALID_SOCKET)
{
printf("connet()failed:%d\n",WSAGetLastError());
return;
}
else
{
iLen=recv(client,buf,sizeof(buf),0); //recv读取消息
if(iLen==0)
return;
else if(iLen==SOCKET_ERROR)
{
printf("recv()failed:%d\n",WSAGetLastError());
return;
}
printf("recv()data from server:%s\n",buf);
}
closesocket(client); 关闭套接字
WSACleanup();关闭套接字库
printf("press any key to continue");
while(1);//防止程序退出
}
展开全部
serve端代码
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#define PORT 5000// 通信端口
int main(int argc, char* argv[])
{
int port=PORT;
WSADATA wsaData;
SOCKET sListen,sAccept;//声明 两个套接字
int iLen;
int iSend;
char buf[]="您好!"; //传输的数据
//服务器、客户的地址
struct sockaddr_in serv,cliet; //
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
//return;
}
sListen=socket(AF_INET,SOCK_STREAM,0);
/* 服务器端开始建立socket 描述符 */ if(sListen==INVALID_SOCKET)
{
printf("socket failed:%d\n",WSAGetLastError());
return;
}
/* 服务器端填充 sockaddr 结构 */
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sListen,(LPSOCKADDR)&serv,sizeof(serv))==SOCKET_ERROR)
/* 捆绑sockfd 描述符 */ {
printf("bind()failed:%d\n",WSAGetLastError());
return;
}
if(listen(sListen,5)==SOCKET_ERROR)
/* 监听sockfd 描述符 */ {
printf("listen()failed:%d\n",WSAGetLastError());
return;
}
iLen=sizeof(cliet);
/* 服务器阻塞,直到客户程序建立连接 */
while(1)
{
sAccept=accept(sListen,(struct sockaddr*)&cliet,&iLen);
if(sAccept==INVALID_SOCKET)
{
printf("accept()failed:%d\n",WSAGetLastError());
break;
}
printf("accepted client IP:[%s],port:[%d]\n",inet_ntoa(cliet.sin_addr),ntohs(cliet.sin_port));
//给连接的客户发送信息
iSend=send(sAccept,buf,sizeof(buf),0);
if(iSend==SOCKET_ERROR)
{
printf("send()failed:%d\n",WSAGetLastError());
break;
}
else if(iSend==0)
break;
else
printf("send()byte:%d\n",send);
closesocket(sAccept);/* 这个通讯已经结束 */
}
closesocket(sListen);
WSACleanup();
}
=============================================
client端代码
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#define PORT 5000
#define BUFFER 1024
void main(int argc,char *argv[])
{
WSADATA wsaData;
SOCKET client;
int port=PORT;
int iLen;
char buf[BUFFER];
struct sockaddr_in serv;
memset(buf,0,sizeof(buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
return;
}
/* 客户程序填充服务端的资料 */
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=inet_addr("192.168.0.1");
client=socket(AF_INET,SOCK_STREAM,0);
/* 客户程序开始建立 sockfd 描述符 */
if(client==INVALID_SOCKET)
{
printf("socket()failed:%d\n",WSAGetLastError());
return;
}
if(connect(client,(struct sockaddr*)&serv,sizeof(serv))==INVALID_SOCKET) /* 客户程序发起连接请求 */
{
printf("connet()failed:%d\n",WSAGetLastError());
return;
}
else
{
/* 连接成功了 */ iLen=recv(client,buf,sizeof(buf),0); //接收
if(iLen==0)
return;
else if(iLen==SOCKET_ERROR)
{
printf("recv()failed:%d\n",WSAGetLastError());
return;
}
printf("recv()data from server:%s\n",buf);
}
closesocket(client);
WSACleanup();
printf("press any key to continue");
while(1);
}
总的来说网络程序是由两个部分组成的--客户端和服务器端.它们的建立步骤一般是:
服务器端
socket-->;bind-->;listen-->;accept
客户端
socket-->;connect
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#define PORT 5000// 通信端口
int main(int argc, char* argv[])
{
int port=PORT;
WSADATA wsaData;
SOCKET sListen,sAccept;//声明 两个套接字
int iLen;
int iSend;
char buf[]="您好!"; //传输的数据
//服务器、客户的地址
struct sockaddr_in serv,cliet; //
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
//return;
}
sListen=socket(AF_INET,SOCK_STREAM,0);
/* 服务器端开始建立socket 描述符 */ if(sListen==INVALID_SOCKET)
{
printf("socket failed:%d\n",WSAGetLastError());
return;
}
/* 服务器端填充 sockaddr 结构 */
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sListen,(LPSOCKADDR)&serv,sizeof(serv))==SOCKET_ERROR)
/* 捆绑sockfd 描述符 */ {
printf("bind()failed:%d\n",WSAGetLastError());
return;
}
if(listen(sListen,5)==SOCKET_ERROR)
/* 监听sockfd 描述符 */ {
printf("listen()failed:%d\n",WSAGetLastError());
return;
}
iLen=sizeof(cliet);
/* 服务器阻塞,直到客户程序建立连接 */
while(1)
{
sAccept=accept(sListen,(struct sockaddr*)&cliet,&iLen);
if(sAccept==INVALID_SOCKET)
{
printf("accept()failed:%d\n",WSAGetLastError());
break;
}
printf("accepted client IP:[%s],port:[%d]\n",inet_ntoa(cliet.sin_addr),ntohs(cliet.sin_port));
//给连接的客户发送信息
iSend=send(sAccept,buf,sizeof(buf),0);
if(iSend==SOCKET_ERROR)
{
printf("send()failed:%d\n",WSAGetLastError());
break;
}
else if(iSend==0)
break;
else
printf("send()byte:%d\n",send);
closesocket(sAccept);/* 这个通讯已经结束 */
}
closesocket(sListen);
WSACleanup();
}
=============================================
client端代码
#include "stdafx.h"
#include<Winsock2.h>
#include<stdio.h>
#define PORT 5000
#define BUFFER 1024
void main(int argc,char *argv[])
{
WSADATA wsaData;
SOCKET client;
int port=PORT;
int iLen;
char buf[BUFFER];
struct sockaddr_in serv;
memset(buf,0,sizeof(buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Winsock load failed\n");
return;
}
/* 客户程序填充服务端的资料 */
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=inet_addr("192.168.0.1");
client=socket(AF_INET,SOCK_STREAM,0);
/* 客户程序开始建立 sockfd 描述符 */
if(client==INVALID_SOCKET)
{
printf("socket()failed:%d\n",WSAGetLastError());
return;
}
if(connect(client,(struct sockaddr*)&serv,sizeof(serv))==INVALID_SOCKET) /* 客户程序发起连接请求 */
{
printf("connet()failed:%d\n",WSAGetLastError());
return;
}
else
{
/* 连接成功了 */ iLen=recv(client,buf,sizeof(buf),0); //接收
if(iLen==0)
return;
else if(iLen==SOCKET_ERROR)
{
printf("recv()failed:%d\n",WSAGetLastError());
return;
}
printf("recv()data from server:%s\n",buf);
}
closesocket(client);
WSACleanup();
printf("press any key to continue");
while(1);
}
总的来说网络程序是由两个部分组成的--客户端和服务器端.它们的建立步骤一般是:
服务器端
socket-->;bind-->;listen-->;accept
客户端
socket-->;connect
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询