C++ 簡單的 Tcp 實現 socket 客戶端與客戶端通訊
阿新 • • 發佈:2018-11-15
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
開發環境 Vs 2008 新建一個 win32 console project
// 伺服器端程式碼
- // Server.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "winsock2.h"
- #pragma comment(lib, "ws2_32.lib")
- #include <iostream>
- using namespace std;
- int _tmain(int argc, char* argv[])
- {
- const
- WSADATA wsd; //WSADATA變數
- SOCKET sServer; //伺服器套接字
- SOCKET sClient; //客戶端套接字
- SOCKADDR_IN addrServ;; //伺服器地址
- char buf[BUF_SIZE]; //接收資料緩衝區
- char sendBuf[BUF_SIZE];//返回給客戶端得資料
- int retVal; //返回值
- //初始化套結字動態庫
- if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
- {
- cout << "WSAStartup failed!" << endl;
- return 1;
- }
- //建立套接字
- sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if(INVALID_SOCKET == sServer)
- {
- cout << "socket failed!" << endl;
- WSACleanup();//釋放套接字資源;
- return -1;
- }
- //伺服器套接字地址
- addrServ.sin_family = AF_INET;
- addrServ.sin_port = htons(4999);
- addrServ.sin_addr.s_addr = INADDR_ANY;
- //繫結套接字
- retVal = bind(sServer, (LPSOCKADDR)&addrServ, sizeof(SOCKADDR_IN));
- if(SOCKET_ERROR == retVal)
- {
- cout << "bind failed!" << endl;
- closesocket(sServer); //關閉套接字
- WSACleanup(); //釋放套接字資源;
- return -1;
- }
- //開始監聽
- retVal = listen(sServer, 1);
- if(SOCKET_ERROR == retVal)
- {
- cout << "listen failed!" << endl;
- closesocket(sServer); //關閉套接字
- WSACleanup(); //釋放套接字資源;
- return -1;
- }
- //接受客戶端請求
- sockaddr_in addrClient;
- int addrClientlen = sizeof(addrClient);
- sClient = accept(sServer,(sockaddr FAR*)&addrClient, &addrClientlen);
- if(INVALID_SOCKET == sClient)
- {
- cout << "accept failed!" << endl;
- closesocket(sServer); //關閉套接字
- WSACleanup(); //釋放套接字資源;
- return -1;
- }
- while(true){
- //接收客戶端資料
- ZeroMemory(buf, BUF_SIZE);
- retVal = recv(sClient, buf, BUF_SIZE, 0);
- if (SOCKET_ERROR == retVal)
- {
- cout << "recv failed!" << endl;
- closesocket(sServer); //關閉套接字
- closesocket(sClient); //關閉套接字
- WSACleanup(); //釋放套接字資源;
- return -1;
- }
- if(buf[0] == '0')
- break;
- cout << "客戶端傳送的資料: " << buf <<endl;
- cout << "向客戶端傳送資料: " ;
- cin >> sendBuf;
- send(sClient, sendBuf, strlen(sendBuf), 0);
- }
- //退出
- closesocket(sServer); //關閉套接字
- closesocket(sClient); //關閉套接字
- WSACleanup(); //釋放套接字資源;
- return 0;
- }
// 客戶端程式碼
[cpp] view plain copy print ?
- // Client.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "winsock2.h"
- #include <iostream>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- BOOL RecvLine(SOCKET s, char* buf); //讀取一行資料
- int main(int argc, char* argv[])
- {
- const int BUF_SIZE = 64;
- WSADATA wsd; //WSADATA變數
- SOCKET sHost; //伺服器套接字
- SOCKADDR_IN servAddr; //伺服器地址
- char buf[BUF_SIZE]; //接收資料緩衝區
- char bufRecv[BUF_SIZE];
- int retVal; //返回值
- //初始化套結字動態庫
- if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
- {
- cout << "WSAStartup failed!" << endl;
- return -1;
- }
- //建立套接字
- sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if(INVALID_SOCKET == sHost)
- {
- cout << "socket failed!" << endl;
- WSACleanup();//釋放套接字資源
- return -1;
- }
- //設定伺服器地址
- servAddr.sin_family =AF_INET;
- servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- servAddr.sin_port = htons((short)4999);
- int nServAddlen = sizeof(servAddr);
- //連線伺服器
- retVal=connect(sHost,(LPSOCKADDR)&servAddr, sizeof(servAddr));
- if(SOCKET_ERROR == retVal)
- {
- cout << "connect failed!" << endl;
- closesocket(sHost); //關閉套接字
- WSACleanup(); //釋放套接字資源
- return -1;
- }
- while(true){
- //向伺服器傳送資料
- ZeroMemory(buf, BUF_SIZE);
- cout << " 向伺服器傳送資料: ";
- cin >> buf;
- retVal = send(sHost, buf, strlen(buf), 0);
- if (SOCKET_ERROR == retVal)
- {
- cout << "send failed!" << endl;
- closesocket(sHost); //關閉套接字
- WSACleanup(); //釋放套接字資源
- return -1;
- }
- //RecvLine(sHost, bufRecv);
- recv(sHost, bufRecv,5 , 0); // 接收伺服器端的資料, 只接收5個字元
- cout << endl <<"從伺服器接收資料:" << bufRecv;
- }
- //退出
- closesocket(sHost); //關閉套接字
- WSACleanup(); //釋放套接字資源
- return 0;
- }