#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <winsock2.h> //Windows Sockets函数定义
#include <stdlib.h>
using namespace std;
bool InitSocket(); //初始化Windows Sockets动态连接库
#pragma comment(lib, "Ws2_32.lib")
void GetIP(char ip[32])
{
struct hostent *hp;
struct in_addr in;
char hostname[32];
int id=0;// 用于网卡计数
*ip=0;
gethostname(hostname,31);//获得主机名
cout<<hostname<<endl;
if((hp=gethostbyname(hostname))==NULL)
{
printf("ERROR: unable get host '%s' ip\n",hostname);
return;
};
while(hp->h_addr_list[id]!=NULL)
{
memcpy(&in.s_addr,hp->h_addr_list[id],sizeof(in.s_addr));
//cout<<inet_ntoa(in)<<endl;
strcpy(ip,inet_ntoa(in));
// printf("ip is %s\n",ip);
id++;
}
};
如果在内网中,只能获得内网的IP地址~
下面是可用的服务器端程序,连接后会显示对方的IP地址,并将数据储存到txt中:
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <winsock2.h>
#include <time.h>
#include<ctime>
bool InitSocket();
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
char ChangeIntToChar(int num)
{
char ch;
if (num<10&&num>=0)
ch=char(num+48);
if (num<=15&&num>=10)
ch=char(num+55);
return ch;
}
int main(int argc, char *argv[])
{
struct sockaddr addr;
int len,off;
bool over;
char msg[255];
SOCKET sock ,sersock;
if ( !InitSocket() ) return 0; //初始化Window Sockets DLL(ws2_32.dll)
//创建流Socket:即传输层使用TCP
sock = socket( AF_INET,SOCK_STREAM,0 );
if ( sock==INVALID_SOCKET ) //不能创建,返回
{
printf("不能创建Socket!");
getch();
WSACleanup( ); //卸载Window Sockets DLL
return 0;
}
//设置IP地址和端口号
((sockaddr_in*)&addr)->sin_family = AF_INET; //AF_INET:使用Internet 协议
((sockaddr_in*)&addr)->sin_port = htons(8080); //服务器端口号3000
((sockaddr_in*)&addr)->sin_addr.s_addr = inet_addr("192.168.1.14");//主机地址
//把套接字与地址绑定
bind(sock,&addr,sizeof(addr));
//监听网络连接
listen(sock,1); //监听连接:1--允许等待队列的长度
printf("等待客户连接!\n");
SOCKADDR_IN addrClient;
len = sizeof(addrClient);
//接受网络连接,生成新的套接字sersock标识这一连接
sersock = accept( sock,(SOCKADDR*)&addrClient,&len );
//从等待队列中检取客户,如队列空则进程挂起
//如不空,则接受并生成新Socket以表示此连接,而原Socket继续等待新客户
if (sersock==INVALID_SOCKET)
{
DWORD err = WSAGetLastError();
char txt[100];
sprintf(txt,"error when accept!---errno:%d",err);
printf(txt);
getch();
WSACleanup( ); //卸载Window Sockets DLL
return 0;
}
printf("有客户连接!\n");
cout<<"对方的地址为: "<<inet_ntoa(addrClient.sin_addr)<<endl;
cout<<endl;
int i=0;
char temp_L=0;
char temp_H=0;
char a,b;
int j;
char temp_data[300];
time_t now_time;
time_t last_time;
char Datashow[128];
char Timeshow[128];
time_t ltime;
FILE *fp;
fp=fopen("D://text.txt","w");
while (1)
{
cout<<"等待数据..."<<endl;
memset(msg,0,sizeof(msg));
memset(temp_data,0,sizeof(temp_data));
last_time= time(NULL);
len = recv ( sersock,msg,200,0 ); //接收用户信息
for ( i=0,j=0;i<len;i++)
{
temp_L=msg[i]&0x0f;
temp_H=msg[i]>>4;
temp_H=temp_H&0x0f;
a=ChangeIntToChar((int)temp_H);
b=ChangeIntToChar((int)temp_L);
temp_data[j++]=a;
temp_data[j++]=b;
cout<<a<<b<<" ";
}
cout<<endl;
cout<<temp_data<<endl;
now_time=time(NULL);
if ((now_time-last_time)<=0)
break;
_strdate( Datashow );
printf( "OS date:%s\n", Datashow ); //打印当前日期
fputs(Datashow,fp);
fputs(" ",fp);
_strtime( Timeshow );
printf( "OS time:%s\n", Timeshow ); //打印当前时间
fputs(Timeshow,fp);
fputs(" : \n",fp);
fputs("\n当前数据是:",fp);
for (int m=0;m<j;m++)
{
fprintf(fp, "%c", temp_data[m]);
if (m%2!=0)
{
fputs(" ",fp);
}
}
fputs("\n\n",fp);
}
fclose(fp);
//关闭套接字
closesocket(sersock);
closesocket(sock);
WSACleanup( ); //卸载Window Sockets DLL
return 0;
}
bool InitSocket()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );
//初始化Windows Sockets DLL,
err = WSAStartup( wVersionRequested, &wsaData );//加载Winsock DLL到内存
if ( err != 0 )
{
printf("没有Windows Socket动态库!\n");
getch();
return false;
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
{
printf("需要Windows Socket 2!\n");
getch();
WSACleanup( ); //非winsock 2.0时,卸载Window Sockets DLL
return false;
}
return true;
}