基于TCP的socket编程
服务器端程序:
1、创建socket
2、将套接字绑定到一个本地地址和端口上bind
3、将套接字设为监听模式,准备接受客户请求listen
4、等待客户请求到来;当请求到来后,结合搜此次连
接的套接字accept
5、用返回的套接字和客户端进行通信send/recv
6、返回,等待另一客户请求。
7、关闭套接字。
源码:
#include <Winsock2.h>
#include <stdio.h>
void main()
{
 WORD wVersionRequested;
 WSADATA wsaData;
 int err;
 
 wVersionRequested = MAKEWORD( 2, 2 );
 
 err = WSAStartup( wVersionRequested, &wsaData );
 if ( err != 0 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL.                                  */
  return;
 }
 
 /* Confirm that the WinSock DLL supports 2.2.*/
 /* Note that if the DLL supports versions greater    */
 /* than 2.2 in addition to 2.2, it will still return */
 /* 2.2 in wVersion since that is the version we      */
 /* requested.                                        */
 
 if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL.                                  */
  WSACleanup( );
  return;
 }
    SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
 SOCKADDR_IN addrSrv;
    addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
 addrSrv.sin_family=AF_INET;
 addrSrv.sin_port=htons(6000);
 bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
 listen(sockSrv,5);
 SOCKADDR_IN addrClient;
 int len=sizeof(SOCKADDR);
 while(1)
 {
  SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
  char sendBuf[100];
  sprintf(sendBuf,"welcome %s to me",
  inet_ntoa(addrClient.sin_addr));
  send(sockConn,sendBuf,strlen(sendBuf)+1,0);
  char recvBuf[100];
  recv(sockConn,recvBuf,100,0);
  printf("%s\n",recvBuf);
  closesocket(sockConn);
 }

}
客户端程序:
1、创建套接字socket
2、想服务器发出连接请求connect
3、和服务器进行通信send/recv
4、关闭套接字。
 
源码:
#include <Winsock2.h>
#include <stdio.h>
void main()
{
 WORD wVersionRequested;
 WSADATA wsaData;
 int err;
 
 wVersionRequested = MAKEWORD( 2, 2 );
 
 err = WSAStartup( wVersionRequested, &wsaData );
 if ( err != 0 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL.                                  */
  return;
 }
 
 /* Confirm that the WinSock DLL supports 2.2.*/
 /* Note that if the DLL supports versions greater    */
 /* than 2.2 in addition to 2.2, it will still return */
 /* 2.2 in wVersion since that is the version we      */
 /* requested.                                        */
 
 if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL.                                  */
  WSACleanup( );
  return;
 }
 SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
    SOCKADDR_IN addrSrv;
 addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
    addrSrv.sin_family=AF_INET;
 addrSrv.sin_port=htons(6000);
 connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
 char recvBuf[100];
    recv(sockClient,recvBuf,100,0);
 printf("%s\n",recvBuf);
 send(sockClient,"this is zhouyang",strlen("this is zhouyang")+1,0);
 closesocket(sockClient);
 WSACleanup();
}
注意:先启动服务器端,在启动客户端,另还需添加ws2_32.lib~