gpt4 book ai didi

C++套接字编程死循环

转载 作者:行者123 更新时间:2023-11-28 04:37:41 25 4
gpt4 key购买 nike

我正在尝试使用彼此对应的服务器和客户端制作基于套接字的程序。如果我使用的是本地主机 ( 127.0.0.1 ),它会工作,但如果我尝试使用我的 ip,服务器将因无限循环而失败。我对套接字编程不是很熟悉,所以我想知道是否有人可以帮助我解决这个问题。这是我的服务器代码:

#define WIN32_LEAN_AND_MEAN
//SERVER
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>


// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")


#define DEFAULT_BUFLEN 512
#include"drone.h"


int main()
{
WSADATA Winsockdata;
int iWsaStartup;
int iWsaCleanup;
SOCKET TCPServerSocket;
int isCloseSocket;

struct sockaddr_in TCPServerAdd;
struct sockaddr_in TCPClientAdd;
int iTCPClientAdd = sizeof(TCPClientAdd);

int iBind;

int iListen;

SOCKET sAcceptSocket;
int iSend;
char SenderBuffer[512] = "Hello from server";
int iSenderBuffer = strlen(SenderBuffer) + 1;
int iRecv;
char RecvBuffer[512];
int iRecvBuffer = strlen(RecvBuffer) + 1;

iWsaStartup = WSAStartup(MAKEWORD(2, 2), &Winsockdata);
if (iWsaStartup != 0)
{
cout << "iWsaStartup Failed";
}

TCPServerAdd.sin_family = AF_INET;
TCPServerAdd.sin_addr.s_addr = inet_addr("127.0.0.1");
TCPServerAdd.sin_port = htons(8000);

TCPServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

iBind = bind(TCPServerSocket, (SOCKADDR *)&TCPServerAdd, sizeof(TCPServerAdd));

iListen = listen(TCPServerSocket, 2);

sAcceptSocket = accept(TCPServerSocket, (SOCKADDR *)&TCPClientAdd, &iTCPClientAdd);

iSend = send(sAcceptSocket, SenderBuffer, iSenderBuffer, 0);


while (1)
{
iRecv = recv(sAcceptSocket, RecvBuffer, iRecvBuffer, 0);
cout << iRecv << endl;
cout << RecvBuffer << endl;
}

return 0;
}

这是我的客户端代码:

// Client
#define WIN32_LEAN_AND_MEAN
#include<iostream>
#include<string>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")


#define DEFAULT_BUFLEN 512


int main()
{

WSADATA Winsockdata;
int iWsaStartup;
int iWsaCleanup;
SOCKET TCPClientSocket;
int isCloseSocket;

struct sockaddr_in TCPServerAdd;

int iconnect;

int iSend;
char SenderBuffer[512];


int iRecv;
char RecvBuffer[512];
int iRecvBuffer = strlen(RecvBuffer) + 1;

int iResult;

iWsaStartup = WSAStartup(MAKEWORD(2, 2), &Winsockdata);
if (iWsaStartup != 0)
{
cout << "iWsaStartup Failed";
}

TCPClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

TCPServerAdd.sin_family = AF_INET;
TCPServerAdd.sin_addr.s_addr = inet_addr("91.139.148.178");
TCPServerAdd.sin_port = htons(7000);


iconnect = connect(TCPClientSocket, (SOCKADDR*)&TCPServerAdd, sizeof(TCPServerAdd));

while (1)
{
cin.getline(SenderBuffer, sizeof SenderBuffer);

int iSenderBuffer = strlen(SenderBuffer) + 1;
iResult = send(TCPClientSocket, SenderBuffer, sizeof(SenderBuffer)+1, 0);
}

return 0;
}

最佳答案

循环是无休止的,因为 1 总是评估为真,因此重复循环。把它变成:

iRecv=0;
int failCount =0;

while (iRecv==0 && ++failCount <= 10)
{
iRecv = recv(sAcceptSocket, RecvBuffer, iRecvBuffer, 0);
cout << iRecv << endl;
cout << RecvBuffer << endl;
if(iRecv == SOCKET_ERROR ){
//handle error
iRecv=0; //if you want to retry
}
}

或简写

while (!iRecv)

在第一次成功接收时,iRecv 将包含一个大于 0 的值并退出循环。

我还添加了一个失败计数器,这样程序将在 10 次尝试失败后退出。

还要记住将所有变量初始化为零或默认值。

关于C++套接字编程死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50936412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com