WinSock之簡單的TCP通訊程式設計

2021-10-02 09:33:22 字數 3909 閱讀 6606

編寫客戶端與服務端程式,客戶端與服務端建立tcp連線後:

(1)服務端向客戶端傳送一條內容為"connect succeed.please send a message to me."然後等待接收客戶端傳送來的一條資訊,收到後顯示該資訊並關閉連線,繼續等待其他客戶的鏈結請求。

(2)客戶端在與伺服器的連線寄建立成功後接收並顯示從伺服器收到的資訊,然後從鍵盤接收一行資訊傳送給伺服器。

#define _winsock_deprecated_no_warnings

#include "winsock2.h"

#include "iostream"

#pragma comment(lib,"ws2_32.lib") //鏈結winsock導入庫

#define port 65432

using namespace std;

int main(int argc, char **ar**)

else if (size == 0)

else

size = recv(newsock, msgbuffer, sizeof(msgbuffer), 0);

if (size == 0)

else

} closesocket(sock_server);

wsacleanup();

return 0;

}

#define _winsock_deprecated_no_warnings

#include "winsock2.h"

#include "iostream"

#pragma comment(lib,"ws2_32.lib") //鏈結winsock導入庫

#define port 65432

using namespace std;

int main(int argc, char **ar**)

else

//傳送

cout << "從鍵盤輸入發給伺服器的資訊!\n";

cin >> msgbuffer;

size = send(sock_client, msgbuffer, sizeof(msgbuffer), 0);

if (size == 0)

else

closesocket(sock_client);

wsacleanup();

return 0;

}

服務端:

客戶端:

/*****服務端*****/

#define _winsock_deprecated_no_warnings

#include "winsock2.h"

#include "iostream"

#pragma comment(lib,"ws2_32.lib") //鏈結winsock導入庫

#define port 65432

using namespace std;

int main(int argc, char **ar**)

int sock_client;

struct sockaddr_in server_addr;

int addr_len = sizeof(struct sockaddr_in);

char msgbuffer[1000];

//建立套接字

if ((sock_client = socket(af_inet, sock_stream, 0)) < 0)

//填寫伺服器位址

char ip[20];

cin >> ip;

memset((void *)&server_addr, 0, addr_len);

server_addr.sin_family = af_inet;

server_addr.sin_port = htons(port);

server_addr.sin_addr.s_addr = inet_addr(ip); //填寫伺服器ip位址

if (connect(sock_client, (struct sockaddr *)&server_addr, addr_len) != 0)

//接收資訊

int size;

if ((size = recv(sock_client, msgbuffer, sizeof(msgbuffer), 0)) < 0)

else if (size == 0)

else

cout << "從鍵盤輸入發給伺服器的資訊!\n";

cin >> msgbuffer;

if ((size = send(sock_client, msgbuffer, sizeof(msgbuffer), 0)) < 0)

else if (size == 0)

else

closesocket(sock_client);

wsacleanup();

return 0;

}/*****客戶端*****/

#define _winsock_deprecated_no_warnings

#include "winsock2.h"

#include "iostream"

#pragma comment(lib,"ws2_32.lib") //鏈結winsock導入庫

#define port 65432

using namespace std;

int main(int argc, char **ar**)

int sock_client;

struct sockaddr_in server_addr;

int addr_len = sizeof(struct sockaddr_in);

char msgbuffer[1000];

//建立套接字

if ((sock_client = socket(af_inet, sock_stream, 0)) < 0)

//填寫伺服器位址

char ip[20];

cin >> ip;

memset((void *)&server_addr, 0, addr_len);

server_addr.sin_family = af_inet;

server_addr.sin_port = htons(port);

server_addr.sin_addr.s_addr = inet_addr(ip); //填寫伺服器ip位址

if (connect(sock_client, (struct sockaddr *)&server_addr, addr_len) != 0)

//接收資訊

int size;

if ((size = recv(sock_client, msgbuffer, sizeof(msgbuffer), 0)) < 0)

else if (size == 0)

else

cout << "從鍵盤輸入發給伺服器的資訊!\n";

cin >> msgbuffer;

if ((size = send(sock_client, msgbuffer, sizeof(msgbuffer), 0)) < 0)

else if (size == 0)

else

closesocket(sock_client);

wsacleanup();

return 0;

}

winsock之 TCP IP的簡單程式設計

問題記錄 1.寫這個 的時候犯了乙個最低階的問題搞錯了優先順序。的優先順序要大於 的優先順序要大於 的優先順序,最好用括號來避免這種問題。2.在所有tcp伺服器中,在呼叫bind之前設定so reuseaddr套介面選項。一般來說 乙個埠釋放後會等待兩分鐘之後才能再被使用 so reuseaddr ...

winsock實現基於TCP的客戶端 伺服器通訊

一 伺服器端程式 include include pragma comment lib,ws2 32.lib 或者在專案屬性的聯結器的輸入的附加依賴項中新增ws2 32.lib define port 5000 void main slisten socket af inet,sock stream...

最簡單的WINSOCK通訊模型

程式所需控制項 winsock控制項,commandbutton控制項 預設name textbox控制項 乙個名稱為txtmess,另乙個名稱為txtsend 程式 如下 private sub form load dim my as string winsock1.protocol scku r...