我正在尝试使服务器和客户端相互对应的基于套接字的程序。如果我使用本地主机(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;
}
答案 0 :(得分:1)
循环是无限的,因为1总是求值为true,因此重复循环。变成:
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次尝试失败后退出。
还请记住将所有变量初始化为零或默认值。