码迷,mamicode.com
首页 > 其他好文 > 详细

[VC]vc中socket编程步骤

时间:2014-05-20 10:31:27      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:blog   c   tar   http   a   int   

sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW); 基于TCP的socket编程是采用的流式套接字。在这个程序中,将两个工程添加到一个工作区。要链接一个ws2_32.lib的库文件。

服务器端编程的步骤:

1:加载套接字库,创建套接字(WSAStartup()/socket());

2:绑定套接字到一个IP地址和一个端口上(bind());

3:将套接字设置为监听模式等待连接请求(listen());

4:请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept());

5:用返回的套接字和客户端进行通信(send()/recv());

6:返回,等待另一连接请求;

7:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

服务器端代码如下:
bubuko.com,布布扣#include <stdio.h>
bubuko.com,布布扣#include <Winsock2.h>
bubuko.com,布布扣void main()
bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣{
bubuko.com,布布扣 WORD wVersionRequested;
bubuko.com,布布扣 WSADATA wsaData;
bubuko.com,布布扣 int err;
bubuko.com,布布扣
bubuko.com,布布扣 wVersionRequested = MAKEWORD( 1, 1 );
bubuko.com,布布扣
bubuko.com,布布扣 err = WSAStartup( wVersionRequested, &wsaData );
bubuko.com,布布扣bubuko.com,布布扣 if ( err != 0 ) bubuko.com,布布扣{
bubuko.com,布布扣  return;
bubuko.com,布布扣 }
bubuko.com,布布扣
bubuko.com,布布扣 if ( LOBYTE( wsaData.wVersion ) != 1 ||
bubuko.com,布布扣bubuko.com,布布扣         HIBYTE( wsaData.wVersion ) != 1 ) bubuko.com,布布扣{
bubuko.com,布布扣   WSACleanup( );
bubuko.com,布布扣  return;
bubuko.com,布布扣 }
bubuko.com,布布扣 SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
bubuko.com,布布扣
bubuko.com,布布扣 SOCKADDR_IN addrSrv;
bubuko.com,布布扣 addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
bubuko.com,布布扣 addrSrv.sin_family=AF_INET;
bubuko.com,布布扣 addrSrv.sin_port=htons(6000);
bubuko.com,布布扣
bubuko.com,布布扣 bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
bubuko.com,布布扣
bubuko.com,布布扣 listen(sockSrv,5);
bubuko.com,布布扣
bubuko.com,布布扣 SOCKADDR_IN addrClient;
bubuko.com,布布扣 int len=sizeof(SOCKADDR);
bubuko.com,布布扣 while(1)
bubuko.com,布布扣bubuko.com,布布扣 bubuko.com,布布扣{
bubuko.com,布布扣   SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
bubuko.com,布布扣  char sendBuf[50];
bubuko.com,布布扣   sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));
bubuko.com,布布扣   send(sockConn,sendBuf,strlen(sendBuf)+1,0);
bubuko.com,布布扣  char recvBuf[50];
bubuko.com,布布扣   recv(sockConn,recvBuf,50,0);
bubuko.com,布布扣   printf("%s\n",recvBuf);
bubuko.com,布布扣   closesocket(sockConn);
bubuko.com,布布扣 }
bubuko.com,布布扣
bubuko.com,布布扣}客户端编程的步骤:

1:加载套接字库,创建套接字(WSAStartup()/socket());

2:向服务器发出连接请求(connect());

3:和服务器端进行通信(send()/recv());

4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

客户端的代码如下:
bubuko.com,布布扣#include <stdio.h>
bubuko.com,布布扣#include <Winsock2.h>
bubuko.com,布布扣void main()
bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣{
bubuko.com,布布扣 WORD wVersionRequested;
bubuko.com,布布扣 WSADATA wsaData;
bubuko.com,布布扣 int err;
bubuko.com,布布扣
bubuko.com,布布扣 wVersionRequested = MAKEWORD( 1, 1 );
bubuko.com,布布扣
bubuko.com,布布扣 err = WSAStartup( wVersionRequested, &wsaData );
bubuko.com,布布扣bubuko.com,布布扣 if ( err != 0 ) bubuko.com,布布扣{
bubuko.com,布布扣  return;
bubuko.com,布布扣 }
bubuko.com,布布扣
bubuko.com,布布扣 if ( LOBYTE( wsaData.wVersion ) != 1 ||
bubuko.com,布布扣bubuko.com,布布扣         HIBYTE( wsaData.wVersion ) != 1 ) bubuko.com,布布扣{
bubuko.com,布布扣   WSACleanup( );
bubuko.com,布布扣  return;
bubuko.com,布布扣 }
bubuko.com,布布扣 SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
bubuko.com,布布扣
bubuko.com,布布扣 SOCKADDR_IN addrSrv;
bubuko.com,布布扣 addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
bubuko.com,布布扣 addrSrv.sin_family=AF_INET;
bubuko.com,布布扣 addrSrv.sin_port=htons(6000);
bubuko.com,布布扣 connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
bubuko.com,布布扣 send(sockClient,"hello",strlen("hello")+1,0);
bubuko.com,布布扣 char recvBuf[50];
bubuko.com,布布扣 recv(sockClient,recvBuf,50,0);
bubuko.com,布布扣 printf("%s\n",recvBuf);
bubuko.com,布布扣
bubuko.com,布布扣 closesocket(sockClient);
bubuko.com,布布扣 WSACleanup();
bubuko.com,布布扣}

sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW); 基于TCP的socket编程是采用的流式套接字。在这个程序中,将两个工程添加到一个工作区。要链接一个ws2_32.lib的库文件。

服务器端编程的步骤:

1:加载套接字库,创建套接字(WSAStartup()/socket());

2:绑定套接字到一个IP地址和一个端口上(bind());

3:将套接字设置为监听模式等待连接请求(listen());

4:请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept());

5:用返回的套接字和客户端进行通信(send()/recv());

6:返回,等待另一连接请求;

7:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

服务器端代码如下:
bubuko.com,布布扣#include <stdio.h>
bubuko.com,布布扣#include <Winsock2.h>
bubuko.com,布布扣void main()
bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣{
bubuko.com,布布扣 WORD wVersionRequested;
bubuko.com,布布扣 WSADATA wsaData;
bubuko.com,布布扣 int err;
bubuko.com,布布扣
bubuko.com,布布扣 wVersionRequested = MAKEWORD( 1, 1 );
bubuko.com,布布扣
bubuko.com,布布扣 err = WSAStartup( wVersionRequested, &wsaData );
bubuko.com,布布扣bubuko.com,布布扣 if ( err != 0 ) bubuko.com,布布扣{
bubuko.com,布布扣  return;
bubuko.com,布布扣 }
bubuko.com,布布扣
bubuko.com,布布扣 if ( LOBYTE( wsaData.wVersion ) != 1 ||
bubuko.com,布布扣bubuko.com,布布扣         HIBYTE( wsaData.wVersion ) != 1 ) bubuko.com,布布扣{
bubuko.com,布布扣   WSACleanup( );
bubuko.com,布布扣  return;
bubuko.com,布布扣 }
bubuko.com,布布扣 SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
bubuko.com,布布扣
bubuko.com,布布扣 SOCKADDR_IN addrSrv;
bubuko.com,布布扣 addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
bubuko.com,布布扣 addrSrv.sin_family=AF_INET;
bubuko.com,布布扣 addrSrv.sin_port=htons(6000);
bubuko.com,布布扣
bubuko.com,布布扣 bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
bubuko.com,布布扣
bubuko.com,布布扣 listen(sockSrv,5);
bubuko.com,布布扣
bubuko.com,布布扣 SOCKADDR_IN addrClient;
bubuko.com,布布扣 int len=sizeof(SOCKADDR);
bubuko.com,布布扣 while(1)
bubuko.com,布布扣bubuko.com,布布扣 bubuko.com,布布扣{
bubuko.com,布布扣   SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
bubuko.com,布布扣  char sendBuf[50];
bubuko.com,布布扣   sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));
bubuko.com,布布扣   send(sockConn,sendBuf,strlen(sendBuf)+1,0);
bubuko.com,布布扣  char recvBuf[50];
bubuko.com,布布扣   recv(sockConn,recvBuf,50,0);
bubuko.com,布布扣   printf("%s\n",recvBuf);
bubuko.com,布布扣   closesocket(sockConn);
bubuko.com,布布扣 }
bubuko.com,布布扣
bubuko.com,布布扣}客户端编程的步骤:

1:加载套接字库,创建套接字(WSAStartup()/socket());

2:向服务器发出连接请求(connect());

3:和服务器端进行通信(send()/recv());

4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

客户端的代码如下:
bubuko.com,布布扣#include <stdio.h>
bubuko.com,布布扣#include <Winsock2.h>
bubuko.com,布布扣void main()
bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣{
bubuko.com,布布扣 WORD wVersionRequested;
bubuko.com,布布扣 WSADATA wsaData;
bubuko.com,布布扣 int err;
bubuko.com,布布扣
bubuko.com,布布扣 wVersionRequested = MAKEWORD( 1, 1 );
bubuko.com,布布扣
bubuko.com,布布扣 err = WSAStartup( wVersionRequested, &wsaData );
bubuko.com,布布扣bubuko.com,布布扣 if ( err != 0 ) bubuko.com,布布扣{
bubuko.com,布布扣  return;
bubuko.com,布布扣 }
bubuko.com,布布扣
bubuko.com,布布扣 if ( LOBYTE( wsaData.wVersion ) != 1 ||
bubuko.com,布布扣bubuko.com,布布扣         HIBYTE( wsaData.wVersion ) != 1 ) bubuko.com,布布扣{
bubuko.com,布布扣   WSACleanup( );
bubuko.com,布布扣  return;
bubuko.com,布布扣 }
bubuko.com,布布扣 SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
bubuko.com,布布扣
bubuko.com,布布扣 SOCKADDR_IN addrSrv;
bubuko.com,布布扣 addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
bubuko.com,布布扣 addrSrv.sin_family=AF_INET;
bubuko.com,布布扣 addrSrv.sin_port=htons(6000);
bubuko.com,布布扣 connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
bubuko.com,布布扣 send(sockClient,"hello",strlen("hello")+1,0);
bubuko.com,布布扣 char recvBuf[50];
bubuko.com,布布扣 recv(sockClient,recvBuf,50,0);
bubuko.com,布布扣 printf("%s\n",recvBuf);
bubuko.com,布布扣
bubuko.com,布布扣 closesocket(sockClient);
bubuko.com,布布扣 WSACleanup();
bubuko.com,布布扣}

[VC]vc中socket编程步骤,布布扣,bubuko.com

[VC]vc中socket编程步骤

标签:blog   c   tar   http   a   int   

原文地址:http://www.cnblogs.com/wdk32/p/vc_steop_for_socket.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!