Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by jahwanoh ( 11 years ago )
#include <iostream>
#include <windows.h>
using namespace std;
bool serverChoice();//PROTOTYP OF serverChoice
bool SERVER = true;
int main(int argc, char *argv[])
{
WSADATA wsaData;
WORD wsaVersi
if(WSAStartup(wsaVersion, &wsaData;) != 0){
cout << "There was some problem in initializating WSA!\n";
cin.get();
return -1;
}
cout << "The WSA initialization proced successfully!\n";
SERVER = serverChoice();
cin.get();
if(SERVER){
SOCKET serverPC = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
hostent *serverInfo;
//TYPE IN AN ADDRESS OF SERVER
char serverAddrStr[256] = {'\0'};
cout << "Type in an address of the server: ";
cin.getline(serverAddrStr, 256);
if(serverAddrStr[0] == '\0'){
char standardAddr[4] = {'\127','\0','\0','\1'};
serverInfo = gethostbyaddr(standardAddr, 4, AF_INET);
}
else{
unsigned int serverAddrBuf = inet_addr(serverAddrStr);
serverInfo = gethostbyaddr((char*)&serverAddrBuf;, 4, AF_INET);
}
sockaddr_in serverAddr;
sockaddr_in clientAddr;
serverAddr.sin_family = serverInfo->h_addrtype;
serverAddr.sin_port = htons(12345);
serverAddr.sin_addr = *((in_addr*)serverInfo->h_addr_list[0]);
if(bind(serverPC, (sockaddr*)&serverAddr;, sizeof(serverAddr)) != 0){
cout << "There was some problem in binding the server socket!\n";
WSACleanup();
cin.get();
return -1;
}
if(listen(serverPC, 32) != 0){
cout << "There was some problem in listening of the server socket!\n";
WSACleanup();
cin.get();
return -1;
}
cout << "Server is waiting for connection of some client...\n";
int addrlen = sizeof(clientAddr);
SOCKET clientPC = accept(serverPC, (sockaddr*)&clientAddr;, &addrlen;);
if(clientPC == INVALID_SOCKET){
cout << "There was some problem in connecting client to server!\n";
WSACleanup();
cin.get();
return -1;
}
cout << "Some client connect to this server from address: " << inet_ntoa((in_addr)clientAddr.sin_addr) << endl;
//COMMUNICATION START
int sendInt = 0;
int recvInt = 0;
cout << "Server is waiting for incoming data from the client...\n";
recv(clientPC, (char*)&recvInt;, 4, 0);
cout << "Recived data from the client: " << recvInt << endl;
cout << "Send data: ";
cin >> sendInt;
send(clientPC, (char*)&sendInt;, 4, 0);
cout << "Data were successfully sent!\n";
cin.get();
//COMMUNICATION END
closesocket(clientPC);
cout << "Connection with client was destroyed!\n";
closesocket(serverPC);
cout << "Server was destroyed!\n";
}
else{
SOCKET serverPC = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
hostent *serverInfo;
char serverAddrBuf[256] = {'\0'};
cout << "Type in an address of the server which you want to join: ";
cin.getline(serverAddrBuf, 256);
if(serverAddrBuf[0] == '\0'){
char serverAddress[4] = {'\127','\0','\0','\1'};
serverInfo = gethostbyaddr(serverAddress, 4, AF_INET);
}
else{
unsigned int serverAddrBin = inet_addr(serverAddrBuf);
serverInfo = gethostbyaddr((char*)&serverAddrBin;, 4, AF_INET);
}
cout << "Connecting to the server...\n";
sockaddr_in serverAddr;
serverAddr.sin_family = serverInfo->h_addrtype;
serverAddr.sin_port = htons(12345);
serverAddr.sin_addr = *((in_addr*)serverInfo->h_addr_list[0]);
if(connect(serverPC, (sockaddr*)&serverAddr;, sizeof(serverAddr)) != 0){
cout << "There was some problem in connecting to the server!\n";
WSACleanup();
cin.get();
return -1;
}
cout << "The client successfully connected to the server: " << inet_ntoa((in_addr)serverAddr.sin_addr) << endl;
//COMMUNICATION START
int sendInt = 0;
int recvInt = 0;
cout << "Send integer: ";
cin >> sendInt;
send(serverPC, (char*)&sendInt;, 4, 0);
cout << "Data were successfully sent!\nClient is waiting for incoming data from the server...\n";
recv(serverPC, (char*)&recvInt;, 4, 0);
cout << "Recived data from the server: " << recvInt << endl;
cin.get();
//COMMUNICATION END
closesocket(serverPC);
cout << "Connection with server was destroyed!\n";
}
cout << "Everything proced successfully! The program is now ready to exit!\n";
WSACleanup();
cin.get();
return 0;
}
bool serverChoice(){//IMPLEMENTATION OF serverChoice
choice:
char srvChoice = '\0';
cout << "Would you like to create new server or join to already existing server?\n[(C)reate/(J)oin]: ";
cin >> srvChoice;
switch(srvChoice){
case 'C': case 'c':
return true;
break;
case 'J': case 'j':
return false;
break;
default:
cout << "You entered invalid letter!\n";
goto choice;
}
}
Revise this Paste
Parent: 50908