앞선 글에 대한 해답은 역시 구글에서 찾았다. 그동안 코딩했던 내용과 크게 다르지 않은데, 놓치고 있던 부분이 있었던 것 같다.
참고사이트 http://www.progtown.com/topic1077981-unsolved-outer-object.html
main.cpp
#include "wcomm.h"
#include <string>
#include "winsock2.h"
#include <clocale>
#define QUIT 9
#define IP "127.0.0.1"
using namespace std;
void runclient (char *ip);
void runserver ();
WComm w;
#include <gdiplus.h>
#pragma comment (lib, "GdiPlus.lib")/* our distressful lib-file */
using namespace Gdiplus;/* as you want, but to me not to a high permanently to write Gdiplus:: */
//7bdc1c72-01bb-4802-9998-6a5cb402006b
static const GUID bmp =//{0x7bdc1c72, 0x01bb, 0x4802, {0x99,0x98,0x6a, 0x5c, 0xb4,0x02,0x00,0x6b}};
{0x557cf406, 0x1a04, 0x11d3, {0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e}};
void runclient (char *ip);
void runserver ();
char SCREENPATH [50] = "c: \\screen \\screen.bmp";
#define SCREENPATHL L "c: \\screen \\screen.bmp"
void createscreen ()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup (&gdiplusToken, &gdiplusStartupInput, NULL);
HDC scrdc, memdc;
HBITMAP membit;
scrdc = GetDC (0);
int Height, Width;
Height = GetSystemMetrics (SM_CYSCREEN);
Width = GetSystemMetrics (SM_CXSCREEN);
memdc = CreateCompatibleDC (scrdc);
membit = CreateCompatibleBitmap (scrdc, Width, Height);
SelectObject (memdc, membit);
BitBlt (memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
HBITMAP hBitmap;
hBitmap = (HBITMAP) SelectObject (memdc, membit);
Gdiplus:: Bitmap bitmap (hBitmap, NULL);
bitmap. Save (SCREENPATHL, &bmp);
DeleteObject (hBitmap);
}
void main (/*int argc, char *argv [] */)
{
setlocale (LC_ALL, "rus");
int t, quit, i;
char s [1024], s1 [1024];
cout <<"type:\n1-run server\n2-run client" <<endl;
cin>> t;
strcpy (s, IP);
if (t == 1)
runserver ();
else
{
runclient (s);
}
}
void runserver ()
{
//Start Server Daemon
w.startServer (27015);
printf ("Server Started........\n");
while (TRUE)
{
//Wait until a client connects
w.waitForClient ();
printf ("Client Connected......\n");
//Work with client
while (TRUE)
{
char rec [50] = "";//it is accepted the data from the client
w.recvData (rec, 32); w.sendData ("OK");//it is sent that accepted
if (strcmp (rec, "FileSend") == 0)
{
char fname [32] = "";
wcom.cpp
#include "wcomm.h"
WSADATA wsaData;
SOCKET m_socket;
SOCKET m_backup;
sockaddr_in con;
SOCKET AcceptSocket;
using namespace std;
BOOL GetLastWriteTime (HANDLE hFile, LPTSTR lpszString, DWORD dwSize);
//all
WComm:: WComm ()
{
//Initialize Winsock.
int iResult = WSAStartup (MAKEWORD (2,2), &wsaData);
if (iResult! = NO_ERROR)
printf ("Error at WSAStartup () \n");
//Create a socket.
//AF_INET - the Internet socket
//SOCK_STREAM - a stream socket (with connection setting)
//0 - it is by default selected TCP the protocol
m_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET) {
printf ("Error at socket (): %ld\n", WSAGetLastError ());
WSACleanup ();
return;
}
//we copy a socket
m_backup = m_socket;
}
//Good Programmer do not comment this
//WComm:: ~WComm () {WSACleanup ();}
void WComm:: connectServer (char *ip, int port)
{
//Connect to a server.
con.sin_family = AF_INET;
con.sin_addr.s_addr = inet_addr (ip);
con.sin_port = htons (port);
if (connect (m_socket, (SOCKADDR *) &con, sizeof (con)) == SOCKET_ERROR) {
printf ("Failed to connect.\n");
WSACleanup ();
return;
}
}
void WComm:: startServer (int port)
{
//Connect to a server binding of a socket with the local address.
con.sin_family = AF_INET;//
con.sin_addr.s_addr = inet_addr ("0.0.0.0");
con.sin_port = htons (port);
//it is caused bind for binding
if (bind (m_socket, (SOCKADDR *) &con, sizeof (con)) == SOCKET_ERROR) {
printf ("Failed to connect.\n");
WSACleanup ();
return;
}
//Listen on the socket.
if (listen (m_socket, 1) == SOCKET_ERROR)
printf ("Error listening on socket.\n");
}
void WComm:: waitForClient ()
{
AcceptSocket = INVALID_SOCKET;
//we resolve attempt of entering connection on the server
while (AcceptSocket == INVALID_SOCKET) {
AcceptSocket = accept (m_backup, NULL, NULL);
}
m_socket = AcceptSocket;
//a m_socket-new socket
}
int WComm:: sendData (char *sendbuf)
{
return send (m_socket, sendbuf, strlen (sendbuf), 0);
}
int WComm:: recvData (char *recvbuf, int size)
{
int sz = recv (m_socket, recvbuf, size, 0);
recvbuf [sz] = '\0 ';
return sz;
}
void WComm:: closeConnection ()
{
closesocket (m_socket);
m_socket = m_backup;
}
void WComm:: fileReceive (char *filename)
{
char rec [50] = "";
HANDLE hFile;
TCHAR szBuf [MAX_PATH];
string s1;
recv (m_socket, filename, 32, 0);
send (m_socket, "OK", strlen ("OK"), 0);
hFile = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
GetLastWriteTime (hFile, szBuf, MAX_PATH);
CloseHandle (hFile);
FILE *fw = fopen (filename, "wb");
int recs = recv (m_socket, rec, 32, 0);//accepted file size
send (m_socket, "OK", strlen ("OK"), 0);
rec [recs] = '\0 ';
int size = atoi (rec);
while (size> 0)
{
char buffer [1030];
if (size> =1024)
{
recv (m_socket, buffer, 1024, 0);
send (m_socket, "OK", strlen ("OK"), 0);
fwrite (buffer, 1024, 1, fw);
}
else
{
recv (m_socket, buffer, size, 0);
send (m_socket, "OK", strlen ("OK"), 0);
buffer [size] = '\0 ';
fwrite (buffer, size, 1, fw);
}
size - = 1024;
}
char fn [MAX_PATH], fn1 [MAX_PATH];
int i=0;
//strcpy (s, szBuf);
strcpy (fn, filename);
string s_tmp;
for (int i=strlen (filename)-1; fn [i+1]! = '. '; i-)
{
s_tmp=fn [i];
s1.insert (0, s_tmp);
}
s1.resize (s1.length ());
string s2;
for (int i=0; i <MAX_PATH; i ++)
{
s_tmp=szBuf [i];
s2.push_back (szBuf [i]);
}
s2.resize (strlen (szBuf));
string s3;
s3=s2+s1;
fclose (fw);
rename (fn, s3.c_str ());
}
void WComm:: fileSend (char *fpath)
{
//Extract only filename from given path.
char filename [50];
int i=strlen (fpath);
for (; i> 0; i-) if (fpath [i-1] == ' \\') break;
for (int j=0; i <= (int) strlen (fpath); i ++) filename [j ++] =fpath [i];
////////////////////////////////////////
ifstream myFile (fpath, ios:: in|ios:: binary|ios:: ate);
int size = (int) myFile.tellg ();//current position in a flow
myFile.close ();
char filesize [10]; itoa (size, filesize, 10);
send (m_socket, filename, strlen (filename), 0);//we send a name
char rec [32] = ""; recv (m_socket, rec, 32, 0);
send (m_socket, filesize, strlen (filesize), 0);//we send file size
recv (m_socket, rec, 32, 0);//we accept acknowledgement
FILE *fr = fopen (fpath, "rb");
while (size> 0)
{
char buffer [1030];
if (size> =1024)
{
fread (buffer, 1024, 1, fr);
send (m_socket, buffer, 1024, 0);
recv (m_socket, rec, 32, 0);
}
else
{
fread (buffer, size, 1, fr);
buffer [size] = '\0 ';
send (m_socket, buffer, size, 0);
recv (m_socket, rec, 32, 0);
}
size - = 1024;
}
fclose (fr);
}
BOOL GetLastWriteTime (HANDLE hFile, LPTSTR lpszString, DWORD dwSize)
{
FILETIME ftCreate, ftAccess, ftWrite;
SYSTEMTIME stUTC, stLocal;
DWORD dwRet;
//Retrieve the file times for the file.
if (! GetFileTime (hFile, &ftCreate, &ftAccess, &ftWrite))
return FALSE;
//Convert the last-write time to local time.
FileTimeToSystemTime (&ftWrite, &stUTC);
SystemTimeToTzSpecificLocalTime (NULL, &stUTC, &stLocal);
//Build a string showing the date and time.
dwRet = StringCchPrintf (lpszString, dwSize,
TEXT ("%02d- % 02D - % 02D - % 02D - % 02D - % 02d");
stLocal.wDay,stLocal.wMonth,stLocal.wYear;
stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
if (S_OK == dwRet)
return TRUE;
else return FALSE;
}
wcom.h
#include "winsock2.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include <tchar.h>
#include <strsafe.h>
#include <string>
#include <cstring>
#include <windows.h>
#ifndef WCOME_H
#define WCOME_H
class WComm
{
public:
WComm ();
void connectServer (char *, int);
int sendData (char *);
int recvData (char *, int);
void fileSend (char *);
void fileReceive (char *);
void startServer (int);
void waitForClient ();
void closeConnection ();
};
#endif
'c++' 카테고리의 다른 글
[openCV] image resize (0) | 2016.01.13 |
---|---|
java server c++ client socket 통신 관련 문제 (0) | 2016.01.06 |