Программирование сокетов в визуальной студии с переполнением стека

Я пишу программу для чата, но когда я запускаю серверную часть, она выдает ошибку в функции прослушивания. Я не знаю, в чем причина ошибки!
Кстати, как я должен запустить этот «первый сервер, а затем клиент», как насчет IP-адрес? как я их установил? Я хочу запустить его только на одном компьютере.
`

    #include "stdafx.h"#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include "iostream"#include "conio.h"using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#define WIN32_LEAN_AND_MEAN
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 27015

int main()
{

WSADATA wsaData;
int iResult = 0;
int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "Server: sending data test";
char recvbuf[DEFAULT_BUFLEN] = "";SOCKET ListenSocket = INVALID_SOCKET;sockaddr_in service;iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
cout<<"Error at WSAStartup()\n";

}

ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
cout<<"socket function failed with error: %u\n"<< WSAGetLastError();
WSACleanup();
getch();

}

service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("192.168.1.52");
service.sin_port = htons(27015);

iResult = bind(ListenSocket, (SOCKADDR *) &service, sizeof (service));
if (iResult == SOCKET_ERROR) {
cout<<"bind failed with error \n"<< WSAGetLastError();
getch();
closesocket(ListenSocket);
WSACleanup();

}
else{

wprintf(L"bind returned success\n");
WSACleanup();
getch();
}

if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR){
cout<<"listen function failed with error: "<< WSAGetLastError();
iResult = closesocket(ListenSocket);
WSACleanup();
getch();
}cout<<"Waiting for client to connect...\n";

ListenSocket = accept(ListenSocket, NULL, NULL);
if (ListenSocket == INVALID_SOCKET) {
cout<<"accept failed with error: \n"<< WSAGetLastError();
closesocket(ListenSocket);
WSACleanup();
getch();
} else
cout<<"Client connected.\n";

iResult = send( ListenSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
cout<<"send failed with error: "<< WSAGetLastError();
closesocket(ListenSocket);
getch();
WSACleanup();

}else
cout<<"Bytes Sent: "<< iResult;do {

iResult = recv(ListenSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
cout<<"Bytes received: "<< iResult;
else if ( iResult == 0 ){
cout<<"Connection closed";
getch();}
else{
cout<<"recv failed with error: "<< WSAGetLastError();
getch();
}
} while( iResult > 0 );iResult = closesocket(ListenSocket);
if (iResult == SOCKET_ERROR) {
cout<<"close failed with error: \n"<< WSAGetLastError();
WSACleanup();
getch();
}return 0;
}

`

#include "stdafx.h"#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include "iostream"#include "conio.h"using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#define WIN32_LEAN_AND_MEAN
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 27015

int main() {

int Result;
WSADATA wsaData;

SOCKET SocketNumber = INVALID_SOCKET;
struct sockaddr_in clientService;

int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "Client: sending data test";
char recvbuf[DEFAULT_BUFLEN] = "";

Result = WSAStartup(MAKEWORD(2,2), &wsaData);
if (Result != NO_ERROR) {
cout<<"WSAStartup failed with error: "<< Result;
getch();
}

SocketNumber = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (SocketNumber == INVALID_SOCKET) {
cout<<"socket failed with error: "<< WSAGetLastError();
getch();
WSACleanup();
}

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( DEFAULT_PORT );

Result = connect( SocketNumber, (SOCKADDR*) &clientService, sizeof(clientService) );
if (Result == SOCKET_ERROR) {
cout<<"connect failed with error: "<< WSAGetLastError();
closesocket(SocketNumber);
getch();
WSACleanup();

}
Result = send( SocketNumber, sendbuf, (int)strlen(sendbuf), 0 );
if (Result == SOCKET_ERROR) {
cout<<"send failed with error: "<< WSAGetLastError();
closesocket(SocketNumber);
getch();
WSACleanup();

}

cout<<"Bytes Sent: "<< Result;

Result = shutdown(SocketNumber, SD_SEND);
if (Result == SOCKET_ERROR) {
cout<<"shutdown failed with error: "<< WSAGetLastError();
closesocket(SocketNumber);
getch();
WSACleanup();

}
do {

Result = recv(SocketNumber, recvbuf, recvbuflen, 0);
if ( Result > 0 )
cout<<"Bytes received: "<< Result;
else if ( Result == 0 )
cout<<"Connection closed";
else
cout<<"recv failed with error: "<< WSAGetLastError();
getch();
} while( Result > 0 );Result = closesocket(SocketNumber);
if (Result == SOCKET_ERROR) {
wprintf(L"close failed with error: %d\n", WSAGetLastError());
getch();
WSACleanup();
return 1;
}

WSACleanup();

return 0;
}

0

Решение

удалить две строки после «wprintf (L» bind return success \ n «);» на сервере я имею в виду эти строки:

WSACleanup();
getch();

и все будет хорошо!

1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]