0
我編寫了一個用於聊天的程序,但是當我運行服務器端時,它會在listen函數中產生錯誤。我不知道是什麼原因造成的錯誤!順便說一句,我該如何運行它?第一臺服務器然後客戶端?IP地址怎麼樣?我如何設置他們?我想在同一臺PC上運行它。 '在Visual Studio中使用C++編寫Socket編程
#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.0.0.0」。 –
添加錯誤以提問更快的答案! – AminM
錯誤消息是:偵聽函數失敗,出現錯誤:10093 –