0% found this document useful (0 votes)
11 views3 pages

Server Side Code

This document is a C++ program that sets up a simple TCP server using the Winsock API on Windows. It initializes the Winsock DLL, creates a server socket, binds it to a local address, listens for incoming connections, and accepts a connection to receive data. The program includes error handling for each step of the socket setup and operation.

Uploaded by

Omar Khaled
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Server Side Code

This document is a C++ program that sets up a simple TCP server using the Winsock API on Windows. It initializes the Winsock DLL, creates a server socket, binds it to a local address, listens for incoming connections, and accepts a connection to receive data. The program includes error handling for each step of the socket setup and operation.

Uploaded by

Omar Khaled
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#define _WIN32_WINNT 0x0600

#define DEFAULT_BUFLEN 512


#include <iostream>
#include <winsock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;

int main() {
cout<<"[*] W11 Sockets\n";
cout<<"[*] SERVER\n";
cout<<"\n[*] Step 1 - Setup DLL\n";

SOCKET serverSocket, acceptSocket;


int port = 55555;
WSADATA wsaData;
int wsaerr;
WORD wVersionRequested = MAKEWORD(2, 2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
if (wsaerr != 0) {
cout <<"[*] The Winsock DLL not found!\n";
return 0;
} else {
cout<<"[*] The Winsock dll found!\n";
cout<<"[*] Status: " << [Link] << "\n";
}

cout<<"\n[*] Step 2 - Set up server socket\n";


serverSocket = INVALID_SOCKET;
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(serverSocket==INVALID_SOCKET) {
cout<<"[*] Socket creation failed!\n";
WSACleanup();
return 0;
} else {
cout<<"[*] socket() is ok!\n";
}

cout<<"\n[*] Step 3 - Bind the server socket\n";


sockaddr_in service;
service.sin_family = AF_INET;
//InetPton(AF_INET, ("[Link]"), &service.sin_addr.s_addr);
service.sin_addr.s_addr = inet_addr("[Link]");
service.sin_port = htons(port);
if(bind(serverSocket, (SOCKADDR*)&service,
sizeof(service))==SOCKET_ERROR) {
cout<<"[*] bind() failed: "<<WSAGetLastError()<<"\n";
closesocket(serverSocket);
WSACleanup();
return 0;
} else {
cout<<"[*] bind() is OK!\n";
}

cout<<"\n[*] Step 4 - initiate listen()\n";


if(listen(serverSocket, 1)==SOCKET_ERROR) {
cout<<"[*] listen() failed: "<<WSAGetLastError()<<"\n";
} else {
cout<<"[*] listen() is OK, waiting for connections...\n";
}

cout<<"\n[*] Step 5 - accept connection\n";


acceptSocket = accept(serverSocket, NULL, NULL);
if(acceptSocket==INVALID_SOCKET) {
cout<<"[*] accept() failed: "<<WSAGetLastError()<<"\n";
WSACleanup();
return -1;
} else {
cout<<"[*] accept() is OK!\n";
}

cout<<"\n[*] Step 6 - receive data\n";


char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;

do{
wsaerr = recv(acceptSocket, recvbuf, recvbuflen, 0);
if(wsaerr>0){
cout<<"Data Received: "<<recvbuf<<"\n";
} else if (wsaerr == 0) {
cout<<"Connection Closed\n";
} else {
cout<<"[*] Error: "<<WSAGetLastError()<<"\n";
}
} while(wsaerr>0);

system("pause");
WSACleanup();
return 0;
}

You might also like