主题:[讨论]请教个问题??
头文件
#if !defined _INITSOCK
#define _INITSOCK
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
class CInitSock
{
public:
CInitSock()
{
WSAData wsaData;
if(WSAStartup(0x0202, &wsaData) != 0)
exit(0);
}
~CInitSock()
{
WSACleanup();
}
};
#endif
服务器端:
#include "iostream.h"
#include "InitSock.h"
#include"math.h"
CInitSock initSock;
void main()
{
SOCKET sock,sock1;
sockaddr_in ServerAddr;
sockaddr_in ClientAddr;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==SOCKET_ERROR)
{
cout<<"creat sock error"<<endl;
return;}
ServerAddr.sin_family=AF_INET;
ServerAddr.sin_addr.s_addr=htonl(INADDR_ANY);
ServerAddr.sin_port=htons(10003);
if(bind(sock,(struct sockaddr FAR*)&ServerAddr,sizeof(ServerAddr))==SOCKET_ERROR)
{cout<<"bind error"<<endl;
return;
}
if(listen(sock,5)<0)
{cout<<"listen error"<<endl;
return;
}
else
{cout<<"listening......."<<endl;
}
char buff[20];
int buf[400];
int len=sizeof(sockaddr_in);
int shu;
for(;;)
{sock1=accept(sock,(struct sockaddr FAR*)&ClientAddr,&len);
for(;;)
{memset(buff,0,20);
if(recv(sock1,buff,20,0)<=0)
{break;}
shu=*(int*)buff;
cout<<"收到的整数为:"<<shu<<endl;
int i=0;
while(pow(2,i)<=shu) i++;
for(int j=i-1;j>=0;j--)
{int b;
b=shu%2;
shu=shu/2;
buf[j]=b;}
cout<<"将转换好的二进制数发送给客户端 "<<endl;
send(sock1,(char*)buf,sizeof(int)*i,0);
//send(sock1,(char*)&i,20,0);
}
}
}
客户端:
#include "iostream.h"
#include "InitSock.h"
#include"math.h"
CInitSock initSock;
void main()
{
SOCKET sock,sock1;
sockaddr_in ServerAddr;
sockaddr_in ClientAddr;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==SOCKET_ERROR)
{
cout<<"creat sock error"<<endl;
return;}
ServerAddr.sin_family=AF_INET;
ServerAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
ServerAddr.sin_port=htons(10003);
if(connect(sock,(struct sockaddr FAR*)&ServerAddr,sizeof(ServerAddr))==SOCKET_ERROR)
{cout<<"connect error"<<endl;
//WSACleanup();
closesocket(sock);
return;}
else
{
cout<<"connect ok"<<endl;}
char buff[20];
char buf[400];
int shu;
for(;;)
{cout<<"请输入一个整数:"<<endl;
cin>>shu;
int i=0;
while(pow(2,i)<=shu) i++;
send(sock,(char*)&shu,sizeof(shu),0);
memset(buf,0,400);
recv(sock,buf,400,0);
cout<<"接收到的转换为二进制数为: ";
for(int j=0;j<i;j++)
{
int num;
num=*((int*)(buf+j*4));
cout<<num;
}
cout<<endl;
}
}
这个程序没问题我想问的是客户端接收转换好的二进制数时输出时为什么要用这条语句num=*((int*)(buf+j*4))为什么j要乘以4!还请各位高手指点啊!!
#if !defined _INITSOCK
#define _INITSOCK
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
class CInitSock
{
public:
CInitSock()
{
WSAData wsaData;
if(WSAStartup(0x0202, &wsaData) != 0)
exit(0);
}
~CInitSock()
{
WSACleanup();
}
};
#endif
服务器端:
#include "iostream.h"
#include "InitSock.h"
#include"math.h"
CInitSock initSock;
void main()
{
SOCKET sock,sock1;
sockaddr_in ServerAddr;
sockaddr_in ClientAddr;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==SOCKET_ERROR)
{
cout<<"creat sock error"<<endl;
return;}
ServerAddr.sin_family=AF_INET;
ServerAddr.sin_addr.s_addr=htonl(INADDR_ANY);
ServerAddr.sin_port=htons(10003);
if(bind(sock,(struct sockaddr FAR*)&ServerAddr,sizeof(ServerAddr))==SOCKET_ERROR)
{cout<<"bind error"<<endl;
return;
}
if(listen(sock,5)<0)
{cout<<"listen error"<<endl;
return;
}
else
{cout<<"listening......."<<endl;
}
char buff[20];
int buf[400];
int len=sizeof(sockaddr_in);
int shu;
for(;;)
{sock1=accept(sock,(struct sockaddr FAR*)&ClientAddr,&len);
for(;;)
{memset(buff,0,20);
if(recv(sock1,buff,20,0)<=0)
{break;}
shu=*(int*)buff;
cout<<"收到的整数为:"<<shu<<endl;
int i=0;
while(pow(2,i)<=shu) i++;
for(int j=i-1;j>=0;j--)
{int b;
b=shu%2;
shu=shu/2;
buf[j]=b;}
cout<<"将转换好的二进制数发送给客户端 "<<endl;
send(sock1,(char*)buf,sizeof(int)*i,0);
//send(sock1,(char*)&i,20,0);
}
}
}
客户端:
#include "iostream.h"
#include "InitSock.h"
#include"math.h"
CInitSock initSock;
void main()
{
SOCKET sock,sock1;
sockaddr_in ServerAddr;
sockaddr_in ClientAddr;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==SOCKET_ERROR)
{
cout<<"creat sock error"<<endl;
return;}
ServerAddr.sin_family=AF_INET;
ServerAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
ServerAddr.sin_port=htons(10003);
if(connect(sock,(struct sockaddr FAR*)&ServerAddr,sizeof(ServerAddr))==SOCKET_ERROR)
{cout<<"connect error"<<endl;
//WSACleanup();
closesocket(sock);
return;}
else
{
cout<<"connect ok"<<endl;}
char buff[20];
char buf[400];
int shu;
for(;;)
{cout<<"请输入一个整数:"<<endl;
cin>>shu;
int i=0;
while(pow(2,i)<=shu) i++;
send(sock,(char*)&shu,sizeof(shu),0);
memset(buf,0,400);
recv(sock,buf,400,0);
cout<<"接收到的转换为二进制数为: ";
for(int j=0;j<i;j++)
{
int num;
num=*((int*)(buf+j*4));
cout<<num;
}
cout<<endl;
}
}
这个程序没问题我想问的是客户端接收转换好的二进制数时输出时为什么要用这条语句num=*((int*)(buf+j*4))为什么j要乘以4!还请各位高手指点啊!!