Rijesenje za drugi zadatak - prvi dio:
Evo prvi dio zadatka, ako niko ne napise drugi dio (server) za sedmicu dana napisat cu i njega. Ako neko ipak napise moze uzeti sve bodove :)
Komentari (iako ih nema mnogo), nazivi varijabli su pisani na engleskom jeziku jer kada pokusam da to sve pisem na nasem onda na kraju dobijem mijesani koji estetski nije prihvatljiv.
!! Dodajte slijedece reference u opcijama za linker: Ws2_32.lib Mswsock.lib Advapi32.lib
Code:
// Winsock application
// Date: August 25, 2008
// Author: Amir Hadzic (schmrz)
// Usage: Sending files using winsock.
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#define DEFAULT_PORT "1234"
#define DEFAULT_BUF 512
using namespace std;
void reportError(char *e_function, int errorCode);
int sendData(SOCKET *aSocket, char *path);
int recvData(SOCKET *aSocket);
int main(int argc, char *argv[]){
WSADATA wsaData;
int iResult;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
// argument check
if (argc < 3) {
cout << "Usage: " << argv[0] << " server-name path-to-file\n";
return 1;
}
// Init winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0){
reportError("WSAStartup()", iResult);
WSACleanup();
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
reportError("getaddrinfo()", iResult);
WSACleanup();
return 1;
}
// create socket
SOCKET connSocket = INVALID_SOCKET;
ptr = result;
connSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (connSocket == INVALID_SOCKET){
reportError("socket()", WSAGetLastError());
WSACleanup();
return 1;
}
// connect to server
iResult = connect(connSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR){
closesocket(connSocket);
connSocket = INVALID_SOCKET;
}
if (connSocket == INVALID_SOCKET){
reportError("connect()", WSAGetLastError());
WSACleanup();
return 1;
}
// send data to server
cout << "Sending data..." << endl;
iResult = sendData(&connSocket, argv[2]);
if (iResult != 0){
closesocket(connSocket);
WSACleanup();
return 1;
}
// shutdown the sending part of the socket
iResult = shutdown(connSocket, SD_SEND);
if (iResult == SOCKET_ERROR){
reportError("shutdown(send)", WSAGetLastError());
closesocket(connSocket);
WSACleanup();
return 1;
}
// receive data
iResult = recvData(&connSocket);
if (iResult != 0){
closesocket(connSocket);
WSACleanup();
return 1;
}
// close socket
closesocket(connSocket);
// cleanup
WSACleanup();
return 0;
}
void reportError(char *e_function, int errorCode){
cout << "Error when calling " << e_function << ". Error code: " << errorCode << endl;
}
int sendData(SOCKET *aSocket, char *path){
char *datablock;
ifstream::pos_type size;
ifstream file(path, ios::in|ios::binary|ios::ate);
int iResult;
if (file.is_open()){
size = file.tellg();
datablock = new char[size];
// read file
file.seekg(ios::beg);
file.read(datablock, size);
file.close();
iResult = send(*aSocket, datablock, size, 0);
if (iResult == SOCKET_ERROR){
reportError("send()", WSAGetLastError());
return WSAGetLastError();
}
delete datablock;
cout << "Bytes sent: " << iResult << endl;
} else
{
reportError("is_open()", 1);
return 1;
}
return 0;
}
int recvData(SOCKET *aSocket){
char recvbuf[DEFAULT_BUF];
int recvbuf_len = DEFAULT_BUF;
int iResult;
int bytes = 0;
cout << "Receiving: \n";
do
{
iResult = recv(*aSocket, recvbuf, recvbuf_len, 0);
if (iResult > 0){
bytes = bytes + iResult;
for (int i = 0; i < strlen(recvbuff), i++){
cout << recvbuff[i] << endl;
}
}
else if(iResult == 0){
cout << "\nConnection closed!" << endl;
cout << "Bytes received: " << bytes << endl;
}
else {
cout << endl;
reportError("recv()", WSAGetLastError());
return WSAGetLastError();
}
} while (iResult > 0);
return 0;
}
Ovo je vrlo jednostavan program i moze primiti samo jednu vezu. Ako neko unaprijedi program neka ga ponovo postavi ovdje, mozda dobije i kakvih bodova od gogy-a. :-) A za one koji budu posebno marljivi saljemo cokoladu ili pivu, po izboru ucesnika