select網路通訊02

2021-10-05 11:49:59 字數 4336 閱讀 2589

client02

#define win32_lean_and_mean

#define _crt_secure_no_warnings

#include

#include

#include

#include

using namespace std;

#pragma comment(lib,"ws2_32.lib")//動態鏈結庫

//可以在屬性的鏈結庫中新增

enum cmd;

struct dataheader

char username[32];

char password[32];

};struct loginresult :public dataheader

int result;

struct logout :public dataheader

char username[32];

};struct logoutresult :public dataheader

int result;

struct newuserjoin :public dataheader

int scok;

int processor(socket _csocket);

//5、接收客戶端請求資料

int nlen = recv(_csocket, szrecv, sizeof(dataheader), 0);

dataheader* header = (dataheader*)szrecv;

if (nlen <= 0)

//6、處理請求

switch (header->cmd)

case cmd_logout_result:

case cmd_new_user_join:

default:

;send(_csocket, (char*)&header, sizeof(header), 0);

}break;

std::printf("傳送成功\n");

return 1;

int main();

_sin.sin_family = af_inet;

_sin.sin_port = htons(4567);//host to net unsigned short

_sin.sin_addr.s_un.s_addr = inet_addr("127.0.0.1");

int ret = connect(sock, (sockaddr*)&_sin, sizeof(sockaddr_in));

if (socket_error == ret)

printf("繫結網路埠失敗。。。");

while (true);

int ret = select(sock, &fdreader, 0, 0, &t);

if (ret < 0)

if (fd_isset(sock, &fdreader))

}printf("處理其他業務。。\n");

login login;

strcpy(login.username, "wwy");

strcpy(login.password, "123");

send(sock, (const char*)&login, sizeof(login), 0);

sleep(4000);//

}// 7、關閉套接字

closesocket(sock);

wsacleanup();

printf("已退出\n");

system("pause");

return 0;

}server02

#define win32_lean_and_mean

#define _crt_secure_no_warnings

#include

#include

#include

#include

#pragma comment(lib,"ws2_32.lib")//動態鏈結庫

//可以在屬性的鏈結庫中新增

using namespace std;

enum cmd;

struct dataheader

char username[32];

char password[32];

};struct loginresult :public dataheader

int result;

struct logout :public dataheader

char username[32];

};struct logoutresult :public dataheader

int result;

struct newuserjoin:public dataheader

int scok;

vectorg_clients;

int processor(socket _csocket);

//5、接收客戶端請求資料

int nlen = recv(_csocket, szrecv, sizeof(dataheader), 0);

dataheader* header = (dataheader*)szrecv;

if (nlen <= 0)

std::printf("收到客戶端的資訊為:%d ,資料長度為%d\n", header->cmd, header->datalength);

//6、處理請求

switch (header->cmd)

case cmd_logout:;

send(_csocket, (char*)&header, sizeof(header), 0);

}break;

std::printf("傳送成功\n");

return 1;

int main();

_sin.sin_family = af_inet;

_sin.sin_port = htons(4567);//host to net unsigned short

_sin.sin_addr.s_un.s_addr = inet_addr("127.0.0.1");

if (socket_error == bind(sock, (sockaddr*)&_sin, sizeof(sockaddr_in)))

printf("繫結網路埠失敗。。。");

//3、listen 監聽埠

if (socket_error == listen(sock, 5))

printf("監聽網路埠失敗。。。");

while (true) fd_set;

fd_set fdread;

fd_set fdwrite;

fd_set fdexp;

//清理集合

fd_zero(&fdread);

fd_zero(&fdwrite);

fd_zero(&fdexp);

//加入集合

fd_set(sock, &fdread);

fd_set(sock, &fdwrite);

fd_set(sock, &fdexp);

for (int n = (int)g_clients.size() - 1; n >= 0; n--);

timeval t = ;

int ret = select(sock + 1, &fdread, &fdwrite, &fdexp, null);

if (ret < 0)

//if (fd_isset(sock, &fdread));

int naddrlen = sizeof(sockaddr_in);

socket _csocket = invalid_socket;

_csocket = accept(sock, (sockaddr*)&clientaddr, &naddrlen);

printf("接收到客戶端。。。\n");

if (invalid_socket == _csocket)

for (int n = (int)g_clients.size() - 1; n >= 0; n--)

g_clients.push_back(_csocket);

printf("新客戶端加入:socket=%d,ip=%s\n", (int)_csocket, inet_ntoa(clientaddr.sin_addr));

}for (int n = 0; n < fdread.fd_count; n++)}}

// 6、關閉客戶端套接字

for (size_t n = g_clients.size() - 1; n >= 0; n--)

網路通訊 select 用法

select 用法 標頭檔案 include include include 定義函式 select 用來等待檔案描述詞狀態的改變。int select int n,fd set readfds,fd set writefds,fd set exceptfds,struct timeval time...

C 網路通訊

c 網路通訊 一 伺服器端程式 10.17 1.建立伺服器端socket 1 使用socket類 建立伺服器socket物件 socket objs new socket 引數 使用ipendpoint類設定伺服器ip位址和埠號 或使用dns類 ipaddress serip ipaddress.p...

Winsock網路通訊

winsock網路通訊 這裡我希望和大家 一下winsock網上通訊的程式,由於網際網路的普及,越來越多的程式具有了在網上與其他程式通訊的功能.其實所有這些功能都建立在winsock介面之上,應用程式執行大多數網路通訊的基本物件稱為套介面或軟插口 socket 使用mfc winsocket類新增網...