Code:
int _tmain(int argc, _TCHAR* argv[])
{
char wait;
do
{
cout << "Enter 1 for Server or 2 for Client: ";
cin >> x;
if(x==2)
{
do
{
WinMain(NULL, NULL, NULL, NULL);
cout << "\nDo you want to send more data(Y/N): ";
cin >> wait;
}while(wait == 'Y' || wait =='y');
}
else
{
do
{
WinMain(NULL, NULL, NULL, NULL);
cout << "\nDo you want to continue receiving data: ";
cin >> wait;
}while(wait == 'Y' || wait == 'y');
}
cout << "\nDo you want to continue using this program(Y/N): ";
cin >> wait;
}while(wait == 'Y' || wait =='y');
return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
/////////////////////////Client/////////////////////////////
if(x==2)
{
WORD sockVersion;
WSADATA wsaData;
int y;
int z;
string ip;
sockVersion = MAKEWORD(1, 1);
WSAStartup(sockVersion, &wsaData); // Initialize Winsock as before
LPHOSTENT hostEntry; // Store information about the server
char text;
in_addr iaHost;
cout << "Enter IP address: 192.168.10."; //port address of computer we are sending data to
do
{
z=0;
cin >> ip;
if(ip == "20")
iaHost.s_addr = inet_addr("192.168.10.20");
else if(ip == "21")
iaHost.s_addr = inet_addr("192.168.10.21");
else if(ip == "22")
iaHost.s_addr = inet_addr("192.168.10.22");
else
{
cout << "Incorrect IP address!\n";
cout << "Enter IP address: 192.168.10.";
z = 1;
}
}while(z==1);
hostEntry = gethostbyaddr((const char*)&iaHost, sizeof(struct in_addr), AF_INET);
if (!hostEntry)
{
y = WSAGetLastError();
ReportError(y, "gethostbyaddr()"); // Report the error as before
WSACleanup();
return NETWORK_ERROR;
}
SOCKET theSocket; // Create the socket
theSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP
if (theSocket == INVALID_SOCKET) {
y = WSAGetLastError();
ReportError(y, "socket()");
WSACleanup();
return NETWORK_ERROR;
}
SOCKADDR_IN serverInfo; // Fill a SOCKADDR_IN struct with address information
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
serverInfo.sin_port = htons(8888);
y = connect(theSocket, // Connect to the server
(LPSOCKADDR)&serverInfo,
sizeof(struct sockaddr));
if (y == SOCKET_ERROR) {
y = WSAGetLastError();
ReportError(1, "connect()");
WSACleanup();
return NETWORK_ERROR;
}
fstream inFile;
cout << "\nEnter the name of the file you would like to read from: ";
char filename[30];
cin.ignore();
cin.getline(filename,30);
inFile.open(filename, ios::in | ios::binary );
char w[256];
//cout << "\nIR camera information to be sent: ";
char *buffer;
//cin.ignore();
inFile.getline(w,256);
buffer = (char*)w;
y = send(theSocket, buffer, strlen(buffer), 0);
if (y == SOCKET_ERROR) {
return NETWORK_ERROR;
}
else
closesocket(theSocket); // Send/receive, then cleanup:
WSACleanup();
}
/////////////////////////////////server///////////////
else
{
WORD sockVersion;
WSADATA wsaData;
int y;
sockVersion = MAKEWORD(1, 1); // We'd like Winsock version 1.1
WSAStartup(sockVersion, &wsaData); // We begin by initializing Winsock
SOCKET listeningSocket; // Next, create the listening socket
listeningSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP
if (listeningSocket == INVALID_SOCKET) {
y = WSAGetLastError(); // Get a more detailed error
ReportError(y, "socket()"); // Report the error with our custom function
WSACleanup(); // Shutdown Winsock
return NETWORK_ERROR; // Return an error value
}
SOCKADDR_IN serverInfo; // Use a SOCKADDR_IN struct to fill in address information
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY; // Since this socket is listening for connections, any local address will do
serverInfo.sin_port = htons(8888); // Convert integer 8888 to network-byte order and insert into the port field
y = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); // Bind the socket to our local server address
if (y == SOCKET_ERROR) {
y = WSAGetLastError();
ReportError(y, "bind()");
WSACleanup();
return NETWORK_ERROR;
}
// Make the socket listen
y = listen(listeningSocket, 10); // Up to 10 connections may wait at any one time to be accept()'ed
if (y == SOCKET_ERROR) {
y = WSAGetLastError();
ReportError(y, "listen()");
WSACleanup();
return NETWORK_ERROR;
}
SOCKET theClient; // Wait for a client
cout << "Waiting for a client.......\n";
theClient = accept(listeningSocket,
NULL, // Address of a sockaddr structure (see explanation below)
NULL); // Address of a variable containing size of sockaddr struct
if (theClient == INVALID_SOCKET) {
y = WSAGetLastError();
ReportError(y, "accept()");
WSACleanup();
return NETWORK_ERROR;
}
fstream outFile;
outFile.open("tony.tony", ios::out | ios::binary );
int i;
char wait;
char *bufferq = new char[256];
for(i = 0; i < 256; i++)
bufferq[i] = '!';
y = recv(theClient, bufferq, strlen(bufferq), 0);
for(i = 0; i < 256; i++)
{
if (bufferq[i] == '!')
break;
cout << bufferq[i];
outFile << bufferq[i];
}
if(y == SOCKET_ERROR){
return NETWORK_ERROR;
}else{
}
closesocket(theClient); // Send and receive from the client, and finally,
closesocket(listeningSocket);
WSACleanup(); // Shutdown Winsock
return NETWORK_OK;
}
}