#include <iostream>
#pragma comment(lib,"ws2_32.lib"); //library for the socket
using namespace std;
#incluse<tchar.h>
//Socket Programming
/* Server side
-Initialize the socket
-create the socket
-get the ip and the port
-bind the ip and port with the socket
-listen the socket
-accept
-recev and send
-close the connection
- clean the library
//using the socket library
bool initialize()
{
WSADATA data;
return WSAstartup(MAKEWORD(2,2),&data);
}
int main() {
if(!intitialize())
{
cout<<"failed in initialization\n";
return 1;
}
//creation of socket
SOCKET create_socket = socket(AF_INET, SOCK_STREAM,0);//first parameter is address ipv4 adresss, 2nd parameter is stream what kind of stream tcp or udp,
// Third parameter is protocol , protocol is decided on what kind of adress and stream, if we put 0 then any adress or stram is acceptted
if(create_socket == INVALID_SOCKET)
{
cout<<"socket creation failed\n";
}
// we always need to check the return type of api cz this helps in identufying where the program failed
//create the address structure
Sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(12345);
//convert the ip adress(0.0.0.0) and put it inside the sin_family in binary form
if(InetPton(AF_INET, _T(0.0.0.0),&serveraddr.sin_addr) != 1)
{
cout<<"setting address structure failed"<<endl;
closesocket(create_socket);
WSACleanup();
return 1;
}
//bind the socket
bind(create_socket, re_interpret_cast<sockaddr*>&serveraddr,sizeof(serveraddr) == SOCKET_ERROR) // it will take server address in strcuture . it will cast using reinterpret cast to server address in strcurure
{
cout<<"bind failed\n";
closesocket(create_socket);
WSACleanup();
return 1;
}
or
bind(create_socket, (struct sockaddr*)(&serveraddr),sizeof(serveraddr) == SOCKET_ERROR);
//listen the socket
if(listen(create_socket, SOMAXCONN) == SOCKET_ERROR) //SOMAXCONN this is quivalent to max number of clinet connection it can listen to . we can give 10 20 liket that.. it sthe no of quees
{
cout<<"listen failed"<<endl;
closesocket(create_socket);
WSACleanup();
return 1;
}
//accept the socket
SOCKET connected_socket = accept(create_socket, nullptr,nullptr); // in accept the second paratmer is the ip address of the socket address and len
// if we want to get the socket addressa and len of the client where we can connect to we can put it
// over here , but if not required we can put nullptr. now here the create_socket is the obj of the strcuture
// socket which it listen to the client . but once it is accepted then it use the connected_socket obj variable further
// and the create_socket still listen to the other client until it hgets coneceted
// the one which get connected use the connected socket variable further.
if(connected_socket == SOCKET_ERROR)
{
cout<<"invalid socket "
}
//Send and Recieve - here we reveive the bufffer from the cleint via socket
char buffer[4096];
int byterecv = recv(connected_socket, buffer,sizeof(buffer) , 0); // the variable which is connected , buffer , size and last paraeter is the flag
string message(buffer,bytesrecv);
cout<<"message from clent"<<message<<endl;
closesocket(connected_socket);
closesocket(create_socket);
WSACleanup();
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: