获得本机IP

10 篇文章 0 订阅
4 篇文章 0 订阅

1.简单服务器
//#include <winsock2.h>
//#pragma comment(lib,"WS2_32.lib")
WSADATA wsd;
static UINT port=%%1;
UINT Listen(LPVOID pParam)
{
              SOCKET sServer,sClient;
              char buf[1024];
              int retVal;
              if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
              { 
                            return -1;//失败
              }
              sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
              if(INVALID_SOCKET==sServer)
              {
                            WSACleanup();
                            return -1;//创建套接字失败
              }
              SOCKADDR_IN addrServ;
              addrServ.sin_family=AF_INET;
              addrServ.sin_port=htons((short)pParam);
              addrServ.sin_addr.s_addr=INADDR_ANY;
              retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));
              if(SOCKET_ERROR==retVal)
              {
                            closesocket(sServer);
                            WSACleanup();
                            return -1;//绑定套接字失败
              }
              retVal=listen(sServer,1);
              if(SOCKET_ERROR==retVal)
              {
                            closesocket(sServer);
                            WSACleanup();
                            return -1;//开始监听失败
              }
              sockaddr_in addrClient;
              int addrClientlen=sizeof(addrClient);
              sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
              if(INVALID_SOCKET==sClient)
              {
                            closesocket(sServer);
                            WSACleanup();
                            return -1;//开始接受客户端连接失败
              }
              ZeroMemory(buf,sizeof(buf));
              retVal=recv(sClient,buf,sizeof(buf),0);
              if(SOCKET_ERROR==retVal)
              {
                            closesocket(sServer);
                            closesocket(sClient);
                            WSACleanup();
                            return -1;//接收数据失败
              }
              CString %%2(buf);
              closesocket(sServer);
              closesocket(sClient);
              WSACleanup();
              return 0;
}
CWinThread *pThread=AfxBeginThread(Listen,&port);

2.简单客户端
//#include <winsock2.h>
//#pragma comment(lib,"WS2_32.lib")
WSADATA wsd;
SOCKET sHost;
SOCKADDR_IN servAddr;
char buf[1024];
int retVal;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
              return -1;//失败
}
sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sHost)
{
              WSACleanup();
              return -1;//创建套接字失败
}
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=inet_addr(%%3);
servAddr.sin_port=htons((short)%%2);
int nServAddlen=sizeof(servAddr);
retVal=connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr));
if(SOCKET_ERROR==retVal)

{
              closesocket(sHost);
              WSACleanup();
              return -1;//连接服务器失败
}
ZeroMemory(buf,sizeof(buf));
strcpy(buf,%%3);
retVal=send(sHost,buf,sizeof(buf),0);
if(SOCKET_ERROR==retVal)
{
              closesocket(sHost);
              WSACleanup();
              return -1;//向服务器发送数据失败
}
closesocket(sHost);
WSACleanup();

3.获得本机IP
//#include <winsock2.h>
//#pragma comment(lib,"WS2_32.lib")
WSADATA wsd;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
              return -1;//失败
}
char szHostname[100],szHostaddress[200];
if(gethostname(szHostname,sizeof(szHostname))!=SOCKET_ERROR)
{
              HOSTENT *pHostEnt=gethostbyname(szHostname);
              if(pHostEnt!=NULL)

              {
                            sprintf(szHostaddress,"%d.%d.%d.%d",
                            ( pHostEnt->h_addr_list[0][0]&0x00ff ),
                            ( pHostEnt->h_addr_list[0][1]&0x00ff ),
                            ( pHostEnt->h_addr_list[0][2]&0x00ff ),
                            ( pHostEnt->h_addr_list[0][3]&0x00ff ));
              }
}
else
              return;
CString %%1(szHostaddress);

 

 

 

最简单的socket 例子

 

http://www.wangchao.net.cn/bbsdetail_70576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值