TCPServer TCPClient三次握手模擬編程

套接字建立和關閉:socket

SOCKET socket(int af,int type,int protocal); 套接字類型: SOCK_STREAM 流套接字,TCP提供有鏈接的可靠傳輸 SOCK_DGRAM 數據包套接字,UDP提供無鏈接的不可靠傳輸 SOCK_RAW 原始套接字

綁定套接字到指定的IP地址和端口號spa

int bind( SOCKET s,//套接字句柄
const struct sockaddr * name,//關聯的本地地址
int namelen//地址的長度
);

監聽code

int listen( SOCKET s,//套接字
int backlog//最大鏈接數
)

鏈接blog

int accept( SOCKET s, struct sockaddr* addr,//取得對方的地址信息
int* addrlen//指向地址長度
); int connect( SOCKET s, const struct sockaddr FAR* name,//鏈接的地址信息
  int namelen  //結構長度
);

收發數據rem

int send( SOCKET s, const char FAR* buf,//要發送數據的緩衝區地址
int len,//緩衝區長度
int flags//指定調用方式,一般爲0
); int recv( SOCKET s, char FAR* buf, int len, int );

 

TCPSERVER端代碼:it

 1 // TCP.cpp : 定義控制檯應用程序的入口點。  2 //  3 
 4 #include "stdafx.h"
 5 #include <winsock2.h>
 6 #include <stdio.h>
 7 
 8 #pragma comment(lib,"WS2_32")
 9 class CInitSock 10 { 11 public: 12     CInitSock(BYTE minorVer=2,BYTE majorVer=2) 13  { 14  WSADATA wsaData; 15         WORD sockVersion = MAKEWORD(minorVer,majorVer); 16         if(::WSAStartup(sockVersion,&wsaData)!=0) 17  { 18             exit(0); 19  } 20  } 21     ~CInitSock() 22  { 23  ::WSACleanup(); 24  } 25 }; 26 CInitSock initSock; 27 int _tmain(int argc, _TCHAR* argv[]) 28 { 29     SOCKET sListen = ::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 30     if(sListen == INVALID_SOCKET) 31  { 32         printf("Failed socket()\n"); 33         return 0; 34  } 35     //填充sockaddr_in結構
36  sockaddr_in sin; 37     sin.sin_family = AF_INET; 38     sin.sin_port = htons(4567); 39     sin.sin_addr.S_un.S_addr = INADDR_ANY; 40     //綁定這個套接字的一個本地地址
41     if(::bind(sListen,(LPSOCKADDR)&sin,sizeof(sin)) == SOCKET_ERROR) 42  { 43         printf("Failed bind()\n"); 44         return 0; 45  } 46     //進入監聽模式
47     if(::listen(sListen,2) == SOCKET_ERROR) 48  { 49         printf("Failed listen()\n"); 50         return 0; 51  } 52     //循環接受客戶的鏈接請求
53  sockaddr_in remoteAddr; 54     int nAddrLen = sizeof(remoteAddr); 55  SOCKET sClient; 56     char szText[] = "TCP Server Demo!\r\n"; 57     while(TRUE) 58  { 59         //接受新鏈接
60         sClient=::accept(sListen,(SOCKADDR*)&remoteAddr,&nAddrLen); 61         if(sClient == INVALID_SOCKET) 62  { 63             printf("Failed accept()"); 64             continue; 65  } 66         printf("接受到一個鏈接:%s\r\n",inet_ntoa(remoteAddr.sin_addr)); 67         //向客戶端發送數據
68         ::send(sClient,szText,strlen(szText),0); 69         //關閉同客戶端的鏈接
70  ::closesocket(sClient); 71  } 72  ::closesocket(sListen); 73     return 0; 74 }

TCPClient 端代碼:io

 1 // TCPClient.cpp : 定義控制檯應用程序的入口點。  2 //  3 
 4 #include "stdafx.h"
 5 #include <winsock2.h>
 6 #include <stdio.h>
 7 #pragma comment(lib,"WS2_32")
 8 class CInitSock  9 { 10 public: 11     CInitSock(BYTE minorVer=2,BYTE majorVer=2) 12  { 13  WSADATA wsaData; 14         WORD sockVersion = MAKEWORD(minorVer,majorVer); 15         if(::WSAStartup(sockVersion,&wsaData)!=0) 16  { 17             exit(0); 18  } 19  } 20     ~CInitSock() 21  { 22  ::WSACleanup(); 23  } 24 }; 25 CInitSock initSock; 26 
27 int _tmain(int argc, _TCHAR* argv[]) 28 { 29     SOCKET s= ::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 30     if(s==INVALID_SOCKET) 31  { 32         printf("Failed socket()\n"); 33         return 0; 34  } 35  sockaddr_in servAddr; 36     servAddr.sin_family=AF_INET; 37     servAddr.sin_port=htons(4567); 38     servAddr.sin_addr.S_un.S_addr=inet_addr("49.140.16.244"); 39     if(::connect(s,(sockaddr*)&servAddr,sizeof(servAddr))==-1) 40  { 41         printf("Failed connect()\n"); 42         return 0; 43  } 44     //接受數據
45     char buff[256]; 46     int nRecv=::recv(s,buff,256,0); 47     if(nRecv>0) 48  { 49         buff[nRecv] = '\0'; 50         printf("接受到數據:%s",buff); 51  } 52  ::closesocket(s); 53     return 0; 54 }

結果:class

相關文章
相關標籤/搜索