The error indicates that there are multiple definitions of the main function in your project. This typically happens when multiple source files (e.g., .cpp files) in your project each contain a main function.
In a typical Visual Studio project setup, you should have separate projects for the server and the client, so each can have its own main function without causing conflicts.
Here’s a step-by-step approach to handle this in Visual Studio 2019:
Step 1: Set Up Separate Projects for Server and Client
-
Create a solution:
- Open Visual Studio 2019.
- Go to
File -> New -> Project.
- Select
Empty Project and click Next.
- Name your solution (e.g.,
WinsockSolution) and click Create.
-
Add a project for the server:
- In the Solution Explorer, right-click on the solution and select
Add -> New Project.
- Select
Console App and click Next.
- Name the project (e.g.,
WinsockServer) and click Create.
-
Add a project for the client:
- In the Solution Explorer, right-click on the solution and select
Add -> New Project.
- Select
Console App and click Next.
- Name the project (e.g.,
WinsockClient) and click Create.
Step 2: Add the Code to Each Project
Server Code (WinsockServer.cpp)
#define _WINSOCK_DEPRECATED_NO_WARNINGS // Suppress deprecated functions warnings
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
// Link with the Ws2_32.lib library
#pragma comment(lib, "Ws2_32.lib")
const int DEFAULT_PORT = 8080;
int main() {
WSADATA wsaData;
// Initialize Winsock
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cerr << "WSAStartup failed with error: " << iResult << std::endl;
return 1;
}
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo* result = NULL;
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, std::to_string(DEFAULT_PORT).c_str(), &hints, &result);
if (iResult != 0) {
std::cerr << "getaddrinfo failed with error: " << iResult << std::endl;
WSACleanup();
return 1;
}
// Create a SOCKET to listen for incoming connections
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
std::cerr << "socket failed with error: " << WSAGetLastError() << std::endl;
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
std::cerr << "bind failed with error: " << WSAGetLastError() << std::endl;
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
std::cerr << "listen failed with error: " << WSAGetLastError() << std::endl;
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
std::cerr << "accept failed with error: " << WSAGetLastError() << std::endl;
closesocket(ListenSocket);
WSACleanup();
return 1;
}
std::cout << "Client connected" << std::endl;
// No longer need server socket
closesocket(ListenSocket);
char recvbuf[1024];
int recvbuflen = 1024;
// Receive data until the client shuts down the connection
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
std::cout << "Received: " << std::string(recvbuf, recvbuf + iResult) << std::endl;
std::string response = "Echo: " + std::string(recvbuf, recvbuf + iResult);
int sendResult = send(ClientSocket, response.c_str(), response.length(), 0);
if (sendResult == SOCKET_ERROR) {
std::cerr << "send failed with error: " << WSAGetLastError() << std::endl;
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} else if (iResult == 0)
std::cout << "Connection closing..." << std::endl;
else {
std::cerr << "recv failed with error: " << WSAGetLastError() << std::endl;
closesocket(ClientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// shutdown the connection since we're done
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
std::cerr << "shutdown failed with error: " << WSAGetLastError() << std::endl;
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ClientSocket);
WSACleanup();
return 0;
}
Client Code (WinsockClient.cpp)
#define _WINSOCK_DEPRECATED_NO_WARNINGS // Suppress deprecated functions warnings
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
// Link with the Ws2_32.lib library
#pragma comment(lib, "Ws2_32.lib")
const char* DEFAULT_IP = "127.0.0.1";
const int DEFAULT_PORT = 8080;
int main() {
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct sockaddr_in clientService;
// Initialize Winsock
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cerr << "WSAStartup failed with error: " << iResult << std::endl;
return 1;
}
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
std::cerr << "socket failed with error: " << WSAGetLastError() << std::endl;
WSACleanup();
return 1;
}
// Setup the sockaddr_in structure with the server address and port
clientService.sin_family = AF_INET;
clientService.sin_port = htons(DEFAULT_PORT);
inet_pton(AF_INET, DEFAULT_IP, &clientService.sin_addr);
// Connect to server
iResult = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
if (iResult == SOCKET_ERROR) {
std::cerr << "connect failed with error: " << WSAGetLastError() << std::endl;
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
std::cout << "Connected to server." << std::endl;
const int sendbuflen = 512;
char recvbuf[sendbuflen];
int recvbuflen = sendbuflen;
while (true) {
std::string message;
std::cout << "Enter message: ";
std::getline(std::cin, message);
// Send the message
iResult = send(ConnectSocket, message.c_str(), message.length(), 0);
if (iResult == SOCKET_ERROR) {
std::cerr << "send failed with error: " << WSAGetLastError() << std::endl;
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// Receive response from server
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
std::cout << "Server: " << std::string(recvbuf, recvbuf + iResult) << std::endl;
} else if (iResult == 0) {
std::cout << "Connection closed." << std::endl;
break;
} else {
std::cerr << "recv failed with error: " << WSAGetLastError() << std::endl;
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
}
// Cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
Step 3: Build and Run the Projects
-
Build the solution:
- In the Solution Explorer, right-click the solution and select Build Solution.
-
Run the server:
- Right-click the
WinsockServer project, and select Set as Startup Project.
- Press
F5 to run the server.
-
Run the client:
- Right-click the
WinsockClient project, and select Set as Startup Project.
- Open a new instance of Visual Studio (or you can use the same instance but open another console window).
- Press
F5 to run the client.
Following these steps ensures that each project has its own main function, eliminating conflicts and errors related to multiple definitions of main. This approach also allows for easier debugging and management of your client-server application in Visual Studio 2019.