注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

dingchaoqun12 的博客

上海股票期货开户

 
 
 

日志

 
 
 
 

最新分享_分享_百度空间  

2012-03-17 00:40:22|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
找到了关于win32 socket的一个简洁的例子,

最新分享_分享_百度空间 - dingchaoqun12 - dingchaoqun12 的博客
http://hi.baidu.com/lewutian

找了3天了.

在visual c++里建两个win32控制台工程,分别编译下面的服务端和客户端

服务端:

#include <windows.h>
#include <iostream.h>
#include <winsock.h>
#pragma comment(lib,"ws2_32.lib")

#define NO_FLAGS_SET 0
#define PORT (u_short)44965
#define MAXBUFLEN 256

//服务器 -- VC++ Socket编程 简单的Tcp/ip服务器
int main()
{
WSADATA Data;
SOCKADDR_IN serverSockAddr;
SOCKADDR_IN clientSockAddr;
SOCKET serverSocket;
SOCKET clientSocket;
int addrLen=sizeof(SOCKADDR_IN);
int status;
int numrcv;
char buffer[MAXBUFLEN];

/* initialize the Windows Socket DLL */
status=WSAStartup(MAKEWORD(1, 1), &Data);
if (status != 0)
   cerr << "ERROR: WSAStartup unsuccessful" << endl;

/* zero the sockaddr_in structure */
memset(&serverSockAddr, 0, sizeof(serverSockAddr));

/* specify the port portion of the address */
serverSockAddr.sin_port=htons(PORT);
/* specify the address family as Internet */
serverSockAddr.sin_family=AF_INET;
/* specify that the address does not matter */
serverSockAddr.sin_addr.s_addr=htonl(INADDR_ANY);

/* create a socket socket(通信发生的区域,套接字的类型,套接字使用的特定协议) */
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET)
   cerr << "ERROR: socket unsuccessful" << endl;

/* associate the socket with the address */
status=bind(serverSocket, (LPSOCKADDR)&serverSockAddr, sizeof(serverSockAddr));
if (status == SOCKET_ERROR)
   cerr << "ERROR: bind unsuccessful" << endl;

/* allow the socket to take connections listen(已建立、尚未连接的套接字号,连接队列的最大长度) */
status=listen(serverSocket, 1);
if (status == SOCKET_ERROR)
   cerr << "ERROR: listen unsuccessful" << endl;

/* accept the connection request when one is received */
clientSocket=accept(serverSocket, (LPSOCKADDR)&clientSockAddr, &addrLen);

cout << "Got the connection..." << endl;

while(1)
{
   /* recv(已连接的套接字, 接收输入数据缓冲区的指针, 缓冲区大小, 传输控制方式) */
   numrcv=recv(clientSocket, buffer, MAXBUFLEN, NO_FLAGS_SET);
   if ((numrcv == 0) || (numrcv == SOCKET_ERROR))
   {
    cout << "Connection terminated." << endl;
   
    status=closesocket(clientSocket);
    if (status == SOCKET_ERROR)
     cerr << "ERROR: closesocket unsuccessful" << endl;
   
    status=WSACleanup();
    if (status == SOCKET_ERROR)
     cerr << "ERROR: WSACleanup unsuccessful" << endl;
    return(1);
   }
   cout << buffer << endl;
} /* while */
}

客户端:

#include <windows.h>
#include <winsock.h>
#include <iostream.h>
#pragma comment(lib,"ws2_32.lib")

#define NO_FLAGS_SET 0
#define PORT (u_short)44965
#define DEST_IP_ADDR "192.168.0.7" //Server address

//客户端 --VC++ Socket编程 简单的Tcp/ip客户端
int main()
{
WSADATA Data;
SOCKADDR_IN destSockAddr;
SOCKET destSocket;
unsigned long destAddr;
int status;
int numsnt;
char *toSendtxt="Test String";

/* initialize the Windows Socket DLL */
status=WSAStartup(MAKEWORD(1, 1), &Data);
if (status != 0)
   cerr << "ERROR: WSAStartup unsuccessful" << endl;

/* convert IP address into in_addr form */
destAddr=inet_addr(DEST_IP_ADDR);
/* copy destAddr into sockaddr_in structure */
memcpy(&destSockAddr.sin_addr, &destAddr, sizeof(destAddr));
/* specify the port portion of the address */
destSockAddr.sin_port=htons(PORT);
/* specify the address family as Internet */
destSockAddr.sin_family=AF_INET;

/* create a socket */
destSocket=socket(AF_INET, SOCK_STREAM, 0);
if (destSocket == INVALID_SOCKET)
{
   cerr << "ERROR: socket unsuccessful" << endl;
  
   status=WSACleanup();
   if (status == SOCKET_ERROR)
    cerr << "ERROR: WSACleanup unsuccessful" << endl;
   return(1);
}

cout << "Trying to connect to IP Address: " << DEST_IP_ADDR << endl;

/* connect to the server */
status=connect(destSocket, (LPSOCKADDR)&destSockAddr, sizeof(destSockAddr));
if (status == SOCKET_ERROR)
{
   cerr << "ERROR: connect unsuccessful" << endl;
  
   status=closesocket(destSocket);
   if (status == SOCKET_ERROR)
    cerr << "ERROR: closesocket unsuccessful"
    << endl;
   status=WSACleanup();
   if (status == SOCKET_ERROR)
    cerr << "ERROR: WSACleanup unsuccessful"
    << endl;
   return(1);
}

cout << "Connected..." << endl;

while(1)
{
   cout << "Sending..." << endl;
   numsnt=send(destSocket, toSendtxt, strlen(toSendtxt) + 1, NO_FLAGS_SET);
   if (numsnt != (int)strlen(toSendtxt) + 1)
   {
    cout << "Connection terminated" << endl;
   
    status=closesocket(destSocket);
    if (status == SOCKET_ERROR)
     cerr << "ERROR: closesocket unsuccessful" << endl;
    status=WSACleanup();
    if (status == SOCKET_ERROR)
     cerr << "ERROR: WSACleanup unsuccessful" << endl;
    return(1);
   }
  
   /* Wait before sending the message again */
   Sleep(1000);
} /* while */
}

把编译生成的exe文件运行,两个都要运行,就可以看到通信的结果了。

希望这对我一样的初学者有所帮助。






引文来源  最新分享_分享_百度空间
  评论这张
 
阅读(361)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018